Search in sources :

Example 1 with BeanSerializerModifier

use of com.fasterxml.jackson.databind.ser.BeanSerializerModifier in project swagger-core by swagger-api.

the class ObjectMapperFactory method create.

private static ObjectMapper create(JsonFactory jsonFactory, boolean openapi31) {
    ObjectMapper mapper = jsonFactory == null ? new ObjectMapper() : new ObjectMapper(jsonFactory);
    if (!openapi31) {
        // handle ref schema serialization skipping all other props
        mapper.registerModule(new SimpleModule() {

            @Override
            public void setupModule(SetupContext context) {
                super.setupModule(context);
                context.addBeanSerializerModifier(new BeanSerializerModifier() {

                    @Override
                    public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription desc, JsonSerializer<?> serializer) {
                        if (Schema.class.isAssignableFrom(desc.getBeanClass())) {
                            return new SchemaSerializer((JsonSerializer<Object>) serializer);
                        } else if (MediaType.class.isAssignableFrom(desc.getBeanClass())) {
                            return new MediaTypeSerializer((JsonSerializer<Object>) serializer);
                        }
                        return serializer;
                    }
                });
            }
        });
    } else {
        mapper.registerModule(new SimpleModule() {

            @Override
            public void setupModule(SetupContext context) {
                super.setupModule(context);
                context.addBeanSerializerModifier(new BeanSerializerModifier() {

                    @Override
                    public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription desc, JsonSerializer<?> serializer) {
                        if (Schema.class.isAssignableFrom(desc.getBeanClass())) {
                            return new Schema31Serializer((JsonSerializer<Object>) serializer);
                        } else if (MediaType.class.isAssignableFrom(desc.getBeanClass())) {
                            return new MediaTypeSerializer((JsonSerializer<Object>) serializer);
                        }
                        return serializer;
                    }
                });
            }
        });
    }
    if (!openapi31) {
        Module deserializerModule = new DeserializationModule();
        mapper.registerModule(deserializerModule);
    } else {
        Module deserializerModule = new DeserializationModule31();
        mapper.registerModule(deserializerModule);
    }
    mapper.registerModule(new JavaTimeModule());
    Map<Class<?>, Class<?>> sourceMixins = new LinkedHashMap<>();
    sourceMixins.put(ApiResponses.class, ExtensionsMixin.class);
    sourceMixins.put(Contact.class, ExtensionsMixin.class);
    sourceMixins.put(Encoding.class, ExtensionsMixin.class);
    sourceMixins.put(EncodingProperty.class, ExtensionsMixin.class);
    sourceMixins.put(Example.class, ExampleMixin.class);
    sourceMixins.put(ExternalDocumentation.class, ExtensionsMixin.class);
    sourceMixins.put(Link.class, ExtensionsMixin.class);
    sourceMixins.put(LinkParameter.class, ExtensionsMixin.class);
    sourceMixins.put(MediaType.class, MediaTypeMixin.class);
    sourceMixins.put(OAuthFlow.class, ExtensionsMixin.class);
    sourceMixins.put(OAuthFlows.class, ExtensionsMixin.class);
    sourceMixins.put(Operation.class, OperationMixin.class);
    sourceMixins.put(PathItem.class, ExtensionsMixin.class);
    sourceMixins.put(Paths.class, ExtensionsMixin.class);
    sourceMixins.put(Scopes.class, ExtensionsMixin.class);
    sourceMixins.put(Server.class, ExtensionsMixin.class);
    sourceMixins.put(ServerVariable.class, ExtensionsMixin.class);
    sourceMixins.put(ServerVariables.class, ExtensionsMixin.class);
    sourceMixins.put(Tag.class, ExtensionsMixin.class);
    sourceMixins.put(XML.class, ExtensionsMixin.class);
    sourceMixins.put(ApiResponse.class, ExtensionsMixin.class);
    sourceMixins.put(Parameter.class, ExtensionsMixin.class);
    sourceMixins.put(RequestBody.class, ExtensionsMixin.class);
    sourceMixins.put(Header.class, ExtensionsMixin.class);
    sourceMixins.put(SecurityScheme.class, ExtensionsMixin.class);
    sourceMixins.put(Callback.class, ExtensionsMixin.class);
    if (!openapi31) {
        sourceMixins.put(Schema.class, SchemaMixin.class);
        sourceMixins.put(DateSchema.class, DateSchemaMixin.class);
        sourceMixins.put(Components.class, ComponentsMixin.class);
        sourceMixins.put(Info.class, InfoMixin.class);
        sourceMixins.put(License.class, LicenseMixin.class);
        sourceMixins.put(OpenAPI.class, OpenAPIMixin.class);
        sourceMixins.put(Discriminator.class, DiscriminatorMixin.class);
    } else {
        sourceMixins.put(Info.class, ExtensionsMixin.class);
        sourceMixins.put(Schema.class, Schema31Mixin.class);
        sourceMixins.put(Components.class, Components31Mixin.class);
        sourceMixins.put(OpenAPI.class, OpenAPI31Mixin.class);
        sourceMixins.put(DateSchema.class, DateSchemaMixin.class);
        sourceMixins.put(Discriminator.class, Discriminator31Mixin.class);
    }
    mapper.setMixIns(sourceMixins);
    mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
    mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
    mapper.configure(SerializationFeature.WRITE_BIGDECIMAL_AS_PLAIN, true);
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return mapper;
}
Also used : MediaTypeSerializer(io.swagger.v3.core.jackson.MediaTypeSerializer) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) Schema31Serializer(io.swagger.v3.core.jackson.Schema31Serializer) LinkedHashMap(java.util.LinkedHashMap) MediaType(io.swagger.v3.oas.models.media.MediaType) SchemaSerializer(io.swagger.v3.core.jackson.SchemaSerializer) Module(com.fasterxml.jackson.databind.Module) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier)

Example 2 with BeanSerializerModifier

use of com.fasterxml.jackson.databind.ser.BeanSerializerModifier in project graphhopper by graphhopper.

the class GraphHopperServletModule method createObjectMapper.

@Provides
@Singleton
ObjectMapper createObjectMapper() {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.setDateFormat(new ISO8601DateFormat());
    objectMapper.registerModule(new JtsModule());
    SimpleModule pathDetailModule = new SimpleModule();
    pathDetailModule.addSerializer(PathDetail.class, new PathDetailSerializer());
    pathDetailModule.addDeserializer(PathDetail.class, new PathDetailDeserializer());
    objectMapper.registerModule(pathDetailModule);
    // Because VirtualEdgeIteratorState has getters which throw Exceptions.
    // http://stackoverflow.com/questions/35359430/how-to-make-jackson-ignore-properties-if-the-getters-throw-exceptions
    objectMapper.registerModule(new SimpleModule().setSerializerModifier(new BeanSerializerModifier() {

        @Override
        public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc, List<BeanPropertyWriter> beanProperties) {
            return beanProperties.stream().map(bpw -> new BeanPropertyWriter(bpw) {

                @Override
                public void serializeAsField(Object bean, JsonGenerator gen, SerializerProvider prov) throws Exception {
                    try {
                        super.serializeAsField(bean, gen, prov);
                    } catch (Exception e) {
                    // Ignoring expected exception, see above.
                    }
                }
            }).collect(Collectors.toList());
        }
    }));
    return objectMapper;
}
Also used : JsonParser(com.fasterxml.jackson.core.JsonParser) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) CmdArgs(com.graphhopper.util.CmdArgs) IOException(java.io.IOException) HashMap(java.util.HashMap) Singleton(javax.inject.Singleton) ServletModule(com.google.inject.servlet.ServletModule) Collectors(java.util.stream.Collectors) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) List(java.util.List) Provides(com.google.inject.Provides) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) Map(java.util.Map) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) com.fasterxml.jackson.databind(com.fasterxml.jackson.databind) PathDetail(com.graphhopper.util.details.PathDetail) ISO8601DateFormat(com.fasterxml.jackson.databind.util.ISO8601DateFormat) IOException(java.io.IOException) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) JtsModule(com.bedatadriven.jackson.datatype.jts.JtsModule) JsonGenerator(com.fasterxml.jackson.core.JsonGenerator) List(java.util.List) BeanPropertyWriter(com.fasterxml.jackson.databind.ser.BeanPropertyWriter) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) Singleton(javax.inject.Singleton) Provides(com.google.inject.Provides)

Example 3 with BeanSerializerModifier

use of com.fasterxml.jackson.databind.ser.BeanSerializerModifier in project autorest-clientruntime-for-java by Azure.

the class FlatteningSerializer method getModule.

/**
 * Gets a module wrapping this serializer as an adapter for the Jackson
 * ObjectMapper.
 *
 * @param mapper the object mapper for default serializations
 * @return a simple module to be plugged onto Jackson ObjectMapper.
 */
public static SimpleModule getModule(final ObjectMapper mapper) {
    SimpleModule module = new SimpleModule();
    module.setSerializerModifier(new BeanSerializerModifier() {

        @Override
        public JsonSerializer<?> modifySerializer(SerializationConfig config, BeanDescription beanDesc, JsonSerializer<?> serializer) {
            if (beanDesc.getBeanClass().getAnnotation(JsonFlatten.class) != null) {
                return new FlatteningSerializer(beanDesc.getBeanClass(), serializer, mapper);
            }
            return serializer;
        }
    });
    return module;
}
Also used : SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier)

Example 4 with BeanSerializerModifier

use of com.fasterxml.jackson.databind.ser.BeanSerializerModifier in project tutorials by eugenp.

the class JacksonDynamicIgnoreUnitTest method setUp.

@Before
public void setUp() {
    mapper.setSerializationInclusion(Include.NON_EMPTY);
    mapper.registerModule(new SimpleModule() {

        @Override
        public void setupModule(final SetupContext context) {
            super.setupModule(context);
            context.addBeanSerializerModifier(new BeanSerializerModifier() {

                @Override
                public JsonSerializer<?> modifySerializer(final SerializationConfig config, final BeanDescription beanDesc, final JsonSerializer<?> serializer) {
                    if (Hidable.class.isAssignableFrom(beanDesc.getBeanClass())) {
                        return new HidableSerializer((JsonSerializer<Object>) serializer);
                    }
                    return serializer;
                }
            });
        }
    });
}
Also used : HidableSerializer(com.baeldung.jackson.dynamicIgnore.HidableSerializer) SerializationConfig(com.fasterxml.jackson.databind.SerializationConfig) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) JsonSerializer(com.fasterxml.jackson.databind.JsonSerializer) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) BeanSerializerModifier(com.fasterxml.jackson.databind.ser.BeanSerializerModifier) Before(org.junit.Before)

Aggregations

SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)4 BeanSerializerModifier (com.fasterxml.jackson.databind.ser.BeanSerializerModifier)4 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)3 JsonSerializer (com.fasterxml.jackson.databind.JsonSerializer)3 SerializationConfig (com.fasterxml.jackson.databind.SerializationConfig)3 HidableSerializer (com.baeldung.jackson.dynamicIgnore.HidableSerializer)1 JtsModule (com.bedatadriven.jackson.datatype.jts.JtsModule)1 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonGenerator (com.fasterxml.jackson.core.JsonGenerator)1 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 JsonParser (com.fasterxml.jackson.core.JsonParser)1 com.fasterxml.jackson.databind (com.fasterxml.jackson.databind)1 Module (com.fasterxml.jackson.databind.Module)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 BeanPropertyWriter (com.fasterxml.jackson.databind.ser.BeanPropertyWriter)1 ISO8601DateFormat (com.fasterxml.jackson.databind.util.ISO8601DateFormat)1 JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)1 Provides (com.google.inject.Provides)1 ServletModule (com.google.inject.servlet.ServletModule)1 CmdArgs (com.graphhopper.util.CmdArgs)1