Search in sources :

Example 71 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project xm-ms-entity by xm-online.

the class XmEntityResourceExtendedIntTest method convertObjectToJsonBytesByFields.

/**
 * Convert an object to JSON byte array.
 *
 * @param object the object to convert
 * @return the JSON byte array
 */
public static byte[] convertObjectToJsonBytesByFields(Object object) throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    mapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE).setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
    JavaTimeModule module = new JavaTimeModule();
    mapper.registerModule(module);
    return mapper.writeValueAsBytes(object);
}
Also used : JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 72 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project xm-ms-entity by xm-online.

the class JsonDataValidator method initialize.

@Override
public void initialize(JsonData constraintAnnotation) {
    log.trace("Json data validator inited");
    objectMapper.registerModule(new JavaTimeModule());
}
Also used : JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)

Example 73 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project spring-framework by spring-projects.

the class Jackson2ObjectMapperBuilderTests method overrideWellKnownModuleWithModule.

// gh-22576
@Test
void overrideWellKnownModuleWithModule() throws IOException {
    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addDeserializer(OffsetDateTime.class, new OffsetDateTimeDeserializer());
    builder.modulesToInstall(javaTimeModule);
    builder.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    ObjectMapper objectMapper = builder.build();
    DemoPojo demoPojo = objectMapper.readValue(DATA, DemoPojo.class);
    assertThat(demoPojo.getOffsetDateTime()).isNotNull();
}
Also used : JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.jupiter.api.Test)

Example 74 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project dhis2-core by dhis2.

the class JacksonObjectMapperConfig method configureMapper.

/**
 * Shared configuration for all Jackson mappers
 *
 * @param objectMapper an {@see ObjectMapper}
 * @param autoDetectGetters if true, enable `autoDetectGetters`
 * @return a configured {@see ObjectMapper}
 */
private static ObjectMapper configureMapper(ObjectMapper objectMapper, boolean autoDetectGetters) {
    SimpleModule module = new SimpleModule();
    module.addDeserializer(String.class, new EmptyStringToNullStdDeserializer());
    module.addDeserializer(Date.class, new ParseDateStdDeserializer());
    module.addDeserializer(JsonPointer.class, new JsonPointerStdDeserializer());
    module.addSerializer(Date.class, new WriteDateStdSerializer());
    module.addSerializer(JsonPointer.class, new JsonPointerStdSerializer());
    // Registering a custom Instant serializer/deserializer for DTOs using
    // Instant
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(Instant.class, new WriteInstantStdSerializer());
    javaTimeModule.addDeserializer(Instant.class, new ParseInstantStdDeserializer());
    objectMapper.registerModules(module, javaTimeModule, new Jdk8Module());
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.setDefaultPropertyInclusion(JsonInclude.Include.NON_NULL);
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
    objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    objectMapper.enable(SerializationFeature.WRAP_EXCEPTIONS);
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    objectMapper.disable(DeserializationFeature.FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY);
    objectMapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
    objectMapper.enable(DeserializationFeature.WRAP_EXCEPTIONS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_FIELDS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_CREATORS);
    if (!autoDetectGetters) {
        objectMapper.disable(MapperFeature.AUTO_DETECT_GETTERS);
    }
    objectMapper.disable(MapperFeature.AUTO_DETECT_SETTERS);
    objectMapper.disable(MapperFeature.AUTO_DETECT_IS_GETTERS);
    return objectMapper;
}
Also used : Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule)

Example 75 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project graylog2-server by Graylog2.

the class QueryTest method setup.

@Before
public void setup() throws Exception {
    final ObjectMapper mapper = new ObjectMapper();
    final TypeFactory typeFactory = mapper.getTypeFactory().withClassLoader(this.getClass().getClassLoader());
    this.objectMapper = mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS).disable(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE).setPropertyNamingStrategy(new PropertyNamingStrategy.SnakeCaseStrategy()).setTypeFactory(typeFactory).registerModule(new GuavaModule()).registerModule(new JodaModule()).registerModule(new Jdk8Module()).registerModule(new JavaTimeModule()).registerModule(new MetricsModule(TimeUnit.SECONDS, TimeUnit.SECONDS, false)).registerModule(new SimpleModule("Graylog").addKeyDeserializer(Period.class, new JodaTimePeriodKeyDeserializer()).addSerializer(new RangeJsonSerializer()).addSerializer(new SizeSerializer()).addSerializer(new ObjectIdSerializer()));
    // kludge because we don't have an injector in tests
    ImmutableMap<String, Class> subtypes = ImmutableMap.<String, Class>builder().put(StreamFilter.NAME, StreamFilter.class).put(ElasticsearchQueryString.NAME, ElasticsearchQueryString.class).put(MessageList.NAME, MessageList.class).build();
    subtypes.forEach((name, klass) -> objectMapper.registerSubtypes(new NamedType(klass, name)));
}
Also used : SizeSerializer(org.graylog2.shared.jackson.SizeSerializer) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) Period(org.joda.time.Period) ElasticsearchQueryString(org.graylog.plugins.views.search.elasticsearch.ElasticsearchQueryString) StreamFilter(org.graylog.plugins.views.search.filter.StreamFilter) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) JodaTimePeriodKeyDeserializer(org.graylog2.jackson.JodaTimePeriodKeyDeserializer) MetricsModule(com.codahale.metrics.json.MetricsModule) PropertyNamingStrategy(com.fasterxml.jackson.databind.PropertyNamingStrategy) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) ObjectIdSerializer(org.graylog2.database.ObjectIdSerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SimpleModule(com.fasterxml.jackson.databind.module.SimpleModule) RangeJsonSerializer(org.graylog2.shared.rest.RangeJsonSerializer) MessageList(org.graylog.plugins.views.search.searchtypes.MessageList) Before(org.junit.Before)

Aggregations

JavaTimeModule (com.fasterxml.jackson.datatype.jsr310.JavaTimeModule)81 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)65 Jdk8Module (com.fasterxml.jackson.datatype.jdk8.Jdk8Module)17 Test (org.junit.Test)9 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)7 ParameterNamesModule (com.fasterxml.jackson.module.paramnames.ParameterNamesModule)7 Bean (org.springframework.context.annotation.Bean)7 GuavaModule (com.fasterxml.jackson.datatype.guava.GuavaModule)6 JodaModule (com.fasterxml.jackson.datatype.joda.JodaModule)5 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 AfterburnerModule (com.fasterxml.jackson.module.afterburner.AfterburnerModule)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 PropertyNamingStrategy (com.fasterxml.jackson.databind.PropertyNamingStrategy)2 JsonMapper (com.fasterxml.jackson.databind.json.JsonMapper)2