Search in sources :

Example 21 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project ninja by ninjaframework.

the class ObjectMapperProvider method get.

@Override
public ObjectMapper get() {
    ObjectMapper objectMapper = new ObjectMapper();
    // Afterburner optimizes performance of Pojo to Json mapper
    objectMapper.registerModule(new AfterburnerModule());
    // Java 8 data type
    objectMapper.registerModule(new JavaTimeModule());
    objectMapper.registerModule(new Jdk8Module());
    // Joda
    objectMapper.registerModule(new JodaModule());
    return objectMapper;
}
Also used : AfterburnerModule(com.fasterxml.jackson.module.afterburner.AfterburnerModule) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 22 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project webpieces by deanhiller.

the class ObjectMapperProvider method get.

public ObjectMapper get() {
    ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false).configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false).configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false).registerModule(new JavaTimeModule()).registerModule(new ParameterNamesModule(JsonCreator.Mode.PROPERTIES));
    if (config.isConvertNullToEmptyStr()) {
        mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        mapper.configOverride(String.class).setSetterInfo(JsonSetter.Value.forValueNulls(Nulls.AS_EMPTY));
    }
    return mapper;
}
Also used : ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 23 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project immutables by immutables.

the class IdAnnotationModuleTest method mappers.

static List<ObjectMapper> mappers() {
    ObjectMapper template = new ObjectMapper().registerModule(new Jdk8Module()).registerModule(new GuavaModule()).registerModule(new JavaTimeModule());
    ObjectMapper mapper1 = template.copy().registerModule(new IdAnnotationModule());
    ObjectMapper mapper2 = template.copy().registerModule(IdAnnotationModule.fromPredicate(m -> ((AnnotatedElement) m).isAnnotationPresent(Criteria.Id.class)));
    ObjectMapper mapper3 = template.copy().registerModule(IdAnnotationModule.fromAnnotation(Criteria.Id.class));
    return Arrays.asList(mapper1, mapper2, mapper3);
}
Also used : Person(org.immutables.criteria.personmodel.Person) Arrays(java.util.Arrays) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) Criteria(org.immutables.criteria.Criteria) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Checkers.check(org.immutables.check.Checkers.check) PersonGenerator(org.immutables.criteria.personmodel.PersonGenerator) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) JavaBean1(org.immutables.criteria.javabean.JavaBean1) ImmutablePerson(org.immutables.criteria.personmodel.ImmutablePerson) MethodSource(org.junit.jupiter.params.provider.MethodSource) AnnotatedElement(java.lang.reflect.AnnotatedElement) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) AnnotatedElement(java.lang.reflect.AnnotatedElement) Criteria(org.immutables.criteria.Criteria) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule)

Example 24 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule 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 25 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project azure-iot-sdk-java by Azure.

the class MainApiVerticle method start.

@SuppressWarnings("deprecation")
@Override
public void start(Future<Void> startFuture) throws Exception {
    Json.mapper.registerModule(new JavaTimeModule());
    FileSystem vertxFileSystem = vertx.fileSystem();
    vertxFileSystem.readFile("swagger.json", readFile -> {
        if (readFile.succeeded()) {
            Swagger swagger = new SwaggerParser().parse(readFile.result().toString(StandardCharsets.UTF_8));
            Router swaggerRouter = SwaggerRouter.swaggerRouter(router, swagger, vertx.eventBus(), new OperationIdServiceIdResolver(), t -> new DeliveryOptions().setSendTimeout(90000));
            deployVerticles(startFuture);
            vertx.createHttpServer().requestHandler(swaggerRouter::accept).listen(8080);
            startFuture.complete();
        } else {
            startFuture.fail(readFile.cause());
        }
    });
}
Also used : SwaggerParser(io.swagger.parser.SwaggerParser) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) FileSystem(io.vertx.core.file.FileSystem) Swagger(io.swagger.models.Swagger) SwaggerRouter(com.github.phiz71.vertx.swagger.router.SwaggerRouter) Router(io.vertx.ext.web.Router) DeliveryOptions(io.vertx.core.eventbus.DeliveryOptions) OperationIdServiceIdResolver(com.github.phiz71.vertx.swagger.router.OperationIdServiceIdResolver)

Aggregations

JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)75 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)59 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)16 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)7 ParameterNamesModule (com.fasterxml.jackson.module.paramnames.ParameterNamesModule)7 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)6 Test (org.junit.Test)6 Bean (org.springframework.context.annotation.Bean)6 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)5 AfterburnerModule (com.fasterxml.jackson.module.afterburner.AfterburnerModule)4 Before (org.junit.Before)4 Primary (org.springframework.context.annotation.Primary)4 StdDateFormat (com.fasterxml.jackson.databind.util.StdDateFormat)3 LocalDateSerializer (com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer)3 LocalDateTimeSerializer (com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer)3 LocalTimeSerializer (com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer)3 IOException (java.io.IOException)3 MetricsModule (com.codahale.metrics.json.MetricsModule)2 JsonFactory (com.fasterxml.jackson.core.JsonFactory)2 Module (com.fasterxml.jackson.databind.Module)2