Search in sources :

Example 1 with JsonSerialize

use of org.codehaus.jackson.map.annotate.JsonSerialize in project enunciate by stoicflame.

the class JsonTypeVisitor method visitDeclared.

@Override
public JsonType visitDeclared(DeclaredType declaredType, Context context) {
    JsonType jsonType = null;
    Element declaredElement = declaredType.asElement();
    DecoratedProcessingEnvironment env = context.getContext().getContext().getProcessingEnvironment();
    DecoratedTypeMirror decoratedTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(declaredType, env);
    String fqn = declaredElement instanceof TypeElement ? ((TypeElement) declaredElement).getQualifiedName().toString() : declaredType.toString();
    if (context.getStack().contains(fqn) && !decoratedTypeMirror.isCollection() && !decoratedTypeMirror.isStream()) {
        // break out of recursive loop.
        return wrapAsNeeded(KnownJsonType.OBJECT, context);
    }
    context.getStack().push(fqn);
    try {
        TypeHint typeHint = declaredElement.getAnnotation(TypeHint.class);
        if (typeHint != null) {
            TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getContext().getProcessingEnvironment(), null);
            if (hint != null) {
                jsonType = hint.accept(this, new Context(context.context, false, false, context.stack));
            }
        }
        final JsonSerialize serializeInfo = declaredElement.getAnnotation(JsonSerialize.class);
        if (serializeInfo != null) {
            DecoratedTypeMirror using = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return serializeInfo.using();
                }
            }, env, JsonSerializer.None.class);
            if (using != null) {
                // custom serializer; just say it's an object.
                jsonType = KnownJsonType.OBJECT;
            }
            DecoratedTypeMirror as = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return serializeInfo.as();
                }
            }, env, Void.class);
            if (as != null) {
                jsonType = (JsonType) as.accept(this, new Context(context.context, false, false, context.stack));
            }
        }
        AdapterType adapterType = JacksonUtil.findAdapterType(declaredElement, context.getContext());
        if (adapterType != null) {
            adapterType.getAdaptingType().accept(this, new Context(context.context, false, false, context.stack));
        } else {
            MapType mapType = MapType.findMapType(declaredType, context.getContext());
            if (mapType != null) {
                JsonType keyType = mapType.getKeyType().accept(this, new Context(context.getContext(), false, false, context.getStack()));
                JsonType valueType = mapType.getValueType().accept(this, new Context(context.getContext(), false, false, context.getStack()));
                jsonType = new JsonMapType(keyType, valueType);
            } else {
                TypeMirror componentType = getComponentType(decoratedTypeMirror, env);
                if (componentType != null) {
                    return wrapAsNeeded(componentType.accept(this, new Context(context.context, false, true, context.stack)), context);
                } else {
                    switch(declaredElement.getKind()) {
                        case ENUM:
                        case CLASS:
                        case INTERFACE:
                            JsonType knownType = context.getContext().getKnownType(declaredElement);
                            if (knownType != null) {
                                jsonType = knownType;
                            } else {
                                // type not known, not specified.  Last chance: look for the type definition.
                                TypeDefinition typeDefinition = context.getContext().findTypeDefinition(declaredElement);
                                if (typeDefinition != null) {
                                    jsonType = new JsonClassType(typeDefinition);
                                }
                            }
                            break;
                    }
                }
            }
        }
        if (jsonType == null) {
            jsonType = super.visitDeclared(declaredType, context);
        }
        return wrapAsNeeded(jsonType, context);
    } finally {
        context.getStack().pop();
    }
}
Also used : EnunciateJackson1Context(com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context) TypeHint(com.webcohesion.enunciate.metadata.rs.TypeHint) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) AdapterType(com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType) JsonSerializer(org.codehaus.jackson.map.JsonSerializer) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) MapType(com.webcohesion.enunciate.modules.jackson1.model.util.MapType) TypeDefinition(com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition) JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 2 with JsonSerialize

use of org.codehaus.jackson.map.annotate.JsonSerialize in project jsonschema2pojo by joelittlejohn.

the class InclusionLevelIT method Jackson1InclusionLevelNonAbsent.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void Jackson1InclusionLevelNonAbsent() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("annotationStyle", "jackson1", "inclusionLevel", "NON_ABSENT"));
    Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties");
    JsonSerialize jsonSerialize = (JsonSerialize) generatedType.getAnnotation(JsonSerialize.class);
    assertThat(jsonSerialize, is(notNullValue()));
    assertThat(jsonSerialize.include(), is(JsonSerialize.Inclusion.NON_NULL));
}
Also used : JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) Test(org.junit.Test)

Example 3 with JsonSerialize

use of org.codehaus.jackson.map.annotate.JsonSerialize in project jsonschema2pojo by joelittlejohn.

the class InclusionLevelIT method Jackson1InclusionLevelNonNull.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void Jackson1InclusionLevelNonNull() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("annotationStyle", "jackson1", "inclusionLevel", "NON_NULL"));
    Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties");
    JsonSerialize jsonSerialize = (JsonSerialize) generatedType.getAnnotation(JsonSerialize.class);
    assertThat(jsonSerialize, is(notNullValue()));
    assertThat(jsonSerialize.include(), is(JsonSerialize.Inclusion.NON_NULL));
}
Also used : JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) Test(org.junit.Test)

Example 4 with JsonSerialize

use of org.codehaus.jackson.map.annotate.JsonSerialize in project jsonschema2pojo by joelittlejohn.

the class InclusionLevelIT method Jackson1InclusionLevelUseDefault.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void Jackson1InclusionLevelUseDefault() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("annotationStyle", "jackson1", "inclusionLevel", "USE_DEFAULTS"));
    Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties");
    JsonSerialize jsonSerialize = (JsonSerialize) generatedType.getAnnotation(JsonSerialize.class);
    assertThat(jsonSerialize, is(notNullValue()));
    assertThat(jsonSerialize.include(), is(JsonSerialize.Inclusion.NON_NULL));
}
Also used : JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) Test(org.junit.Test)

Example 5 with JsonSerialize

use of org.codehaus.jackson.map.annotate.JsonSerialize in project jsonschema2pojo by joelittlejohn.

the class InclusionLevelIT method Jackson1InclusionLevelNonDefault.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void Jackson1InclusionLevelNonDefault() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("annotationStyle", "jackson1", "inclusionLevel", "NON_DEFAULT"));
    Class generatedType = resultsClassLoader.loadClass("com.example.PrimitiveProperties");
    JsonSerialize jsonSerialize = (JsonSerialize) generatedType.getAnnotation(JsonSerialize.class);
    assertThat(jsonSerialize, is(notNullValue()));
    assertThat(jsonSerialize.include(), is(JsonSerialize.Inclusion.NON_DEFAULT));
}
Also used : JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) Test(org.junit.Test)

Aggregations

JsonSerialize (org.codehaus.jackson.map.annotate.JsonSerialize)9 Test (org.junit.Test)7 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)2 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)2 TypeHint (com.webcohesion.enunciate.metadata.rs.TypeHint)2 MapType (com.webcohesion.enunciate.modules.jackson1.model.util.MapType)2 JsonSerializer (org.codehaus.jackson.map.JsonSerializer)2 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)1 Accessor (com.webcohesion.enunciate.modules.jackson1.model.Accessor)1 TypeDefinition (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition)1 AdapterType (com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType)1 Element (javax.lang.model.element.Element)1 TypeElement (javax.lang.model.element.TypeElement)1 TypeMirror (javax.lang.model.type.TypeMirror)1