Search in sources :

Example 6 with JsonSerialize

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

the class InclusionLevelIT method Jackson1InclusionLevelAlways.

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

Example 7 with JsonSerialize

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

the class InclusionLevelIT method Jackson1InclusionLevelNotSet.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void Jackson1InclusionLevelNotSet() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("annotationStyle", "jackson1"));
    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 8 with JsonSerialize

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

the class InclusionLevelIT method Jackson1InclusionLevelNonEmpty.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void Jackson1InclusionLevelNonEmpty() throws ClassNotFoundException, SecurityException, NoSuchMethodException {
    ClassLoader resultsClassLoader = schemaRule.generateAndCompile("/schema/properties/primitiveProperties.json", "com.example", config("annotationStyle", "jackson1", "inclusionLevel", "NON_EMPTY"));
    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_EMPTY));
}
Also used : JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) Test(org.junit.Test)

Example 9 with JsonSerialize

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

the class JsonTypeFactory method findSpecifiedType.

/**
 * Find the specified type of the given adaptable element, if it exists.
 *
 * @param adaptable The adaptable element for which to find the specified type.
 * @param context The context
 * @return The specified JSON type, or null if it doesn't exist.
 */
public static JsonType findSpecifiedType(Adaptable adaptable, EnunciateJackson1Context context) {
    JsonType jsonType = null;
    if (adaptable instanceof Accessor) {
        Accessor accessor = (Accessor) adaptable;
        TypeHint typeHint = accessor.getAnnotation(TypeHint.class);
        if (typeHint != null) {
            TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getProcessingEnvironment(), null);
            if (hint != null) {
                return getJsonType(hint, context);
            }
        }
        final JsonSerialize serializeInfo = accessor.getAnnotation(JsonSerialize.class);
        if (serializeInfo != null) {
            DecoratedProcessingEnvironment env = context.getContext().getProcessingEnvironment();
            DecoratedTypeMirror using = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return serializeInfo.using();
                }
            }, env, JsonSerializer.None.class);
            if (using != null) {
                // we're using some custom serialization, so we just have to return a generic object.
                return KnownJsonType.OBJECT;
            } else {
                DecoratedTypeMirror as = Annotations.mirrorOf(new Callable<Class<?>>() {

                    @Override
                    public Class<?> call() throws Exception {
                        return serializeInfo.as();
                    }
                }, env, Void.class);
                if (as != null) {
                    return getJsonType(as, context);
                } else {
                    DecoratedTypeMirror contentAs = Annotations.mirrorOf(new Callable<Class<?>>() {

                        @Override
                        public Class<?> call() throws Exception {
                            return serializeInfo.contentAs();
                        }
                    }, env, Void.class);
                    DecoratedTypeMirror contentUsing = Annotations.mirrorOf(new Callable<Class<?>>() {

                        @Override
                        public Class<?> call() throws Exception {
                            return serializeInfo.contentUsing();
                        }
                    }, env, JsonSerializer.None.class);
                    DecoratedTypeMirror accessorType = (DecoratedTypeMirror) accessor.asType();
                    if (accessorType.isCollection() || accessorType.isArray() || accessorType.isStream()) {
                        if (contentUsing != null) {
                            // the json type has to be just a list of object.
                            return new JsonArrayType(KnownJsonType.OBJECT);
                        } else if (contentAs != null) {
                            return new JsonArrayType(getJsonType(contentAs, context));
                        }
                    } else {
                        MapType mapType = MapType.findMapType(accessorType, context);
                        if (mapType != null) {
                            DecoratedTypeMirror keyAs = Annotations.mirrorOf(new Callable<Class<?>>() {

                                @Override
                                public Class<?> call() throws Exception {
                                    return serializeInfo.keyAs();
                                }
                            }, env, Void.class);
                            DecoratedTypeMirror keyUsing = Annotations.mirrorOf(new Callable<Class<?>>() {

                                @Override
                                public Class<?> call() throws Exception {
                                    return serializeInfo.keyUsing();
                                }
                            }, env, JsonSerializer.None.class);
                            if (keyAs != null || contentAs != null) {
                                JsonType keyType = keyUsing == null ? getJsonType(keyAs == null ? (DecoratedTypeMirror) mapType.getKeyType() : keyAs, context) : KnownJsonType.OBJECT;
                                JsonType valueType = contentUsing == null ? getJsonType(contentAs == null ? (DecoratedTypeMirror) mapType.getValueType() : contentAs, context) : KnownJsonType.OBJECT;
                                return new JsonMapType(keyType, valueType);
                            }
                        }
                    }
                }
            }
        }
    }
    if (adaptable.isAdapted()) {
        jsonType = getJsonType(adaptable.getAdapterType().getAdaptingType(), context);
    }
    return jsonType;
}
Also used : TypeHint(com.webcohesion.enunciate.metadata.rs.TypeHint) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) JsonSerializer(org.codehaus.jackson.map.JsonSerializer) Accessor(com.webcohesion.enunciate.modules.jackson1.model.Accessor) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) MapType(com.webcohesion.enunciate.modules.jackson1.model.util.MapType) JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) TypeMirror(javax.lang.model.type.TypeMirror) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

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