Search in sources :

Example 66 with JavaTimeModule

use of com.fasterxml.jackson.datatype.jsr310.JavaTimeModule in project ma-core-public by infiniteautomation.

the class MangoRestSpringConfiguration method createNewObjectMapper.

/**
 * Create an instance of the Object Mapper.
 * Used locally when starting Spring but may also be used for testing.
 *
 * Note: This is NOT the same Object Mapper instance used within a running Mango.
 * XXX J.W. Seems to be used inside the REST controller to me?
 *
 * @return
 */
public static ObjectMapper createNewObjectMapper() {
    // For raw Jackson
    ObjectMapper objectMapper = new ObjectMapper();
    if (Common.envProps.getBoolean("rest.indentJSON", false))
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
    // JScience
    JScienceModule jScienceModule = new JScienceModule();
    objectMapper.registerModule(jScienceModule);
    // Mango Core JSON Modules
    MangoCoreModule mangoCore = new MangoCoreModule();
    objectMapper.registerModule(mangoCore);
    MangoRestV2JacksonModule mangoCoreV2 = new MangoRestV2JacksonModule();
    objectMapper.registerModule(mangoCoreV2);
    // Setup Module Defined JSON Modules
    List<JacksonModuleDefinition> defs = ModuleRegistry.getDefinitions(JacksonModuleDefinition.class);
    for (JacksonModuleDefinition def : defs) {
        if (def.getSourceMapperType() == JacksonModuleDefinition.ObjectMapperSource.REST)
            objectMapper.registerModule(def.getJacksonModule());
    }
    // Always output dates in ISO 8601
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
    objectMapper.setDateFormat(dateFormat);
    objectMapper.registerModule(new JavaTimeModule());
    // Set to system tz
    objectMapper.setTimeZone(TimeZone.getDefault());
    // This will allow messy JSON to be imported even if all the properties in it are part of the POJOs
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return objectMapper;
}
Also used : JScienceModule(com.serotonin.m2m2.web.mvc.rest.v1.mapping.JScienceModule) MangoCoreModule(com.serotonin.m2m2.web.mvc.rest.v1.mapping.MangoCoreModule) JacksonModuleDefinition(com.serotonin.m2m2.module.JacksonModuleDefinition) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) MangoRestV2JacksonModule(com.infiniteautomation.mango.rest.v2.mapping.MangoRestV2JacksonModule) SimpleDateFormat(java.text.SimpleDateFormat) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 67 with JavaTimeModule

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

the class JacksonConfig method objectMapper.

@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
    ObjectMapper objectMapper = builder.createXmlMapper(false).build();
    objectMapper.registerModule(javaTimeModule);
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return objectMapper;
}
Also used : LocalTimeSerializer(com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) LocalDateSerializer(com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer) LocalDateTimeSerializer(com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 68 with JavaTimeModule

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

the class JacksonConfig method objectMapper.

@Bean
@Primary
public ObjectMapper objectMapper(Jackson2ObjectMapperBuilder builder) {
    JavaTimeModule javaTimeModule = new JavaTimeModule();
    javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
    javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
    javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
    ObjectMapper objectMapper = builder.createXmlMapper(false).build();
    objectMapper.registerModule(javaTimeModule);
    objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
    objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    return objectMapper;
}
Also used : LocalTimeSerializer(com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) LocalDateSerializer(com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer) LocalDateTimeSerializer(com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Primary(org.springframework.context.annotation.Primary) Bean(org.springframework.context.annotation.Bean)

Example 69 with JavaTimeModule

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

the class JCacheTest method testJson.

@Test
public void testJson() throws InterruptedException, IllegalArgumentException, URISyntaxException, IOException {
    RedisProcess runner = new RedisRunner().nosave().randomDir().port(6311).run();
    URL configUrl = getClass().getResource("redisson-jcache.yaml");
    Config cfg = Config.fromYAML(configUrl);
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JavaTimeModule());
    cfg.setCodec(new TypedJsonJacksonCodec(String.class, LocalDateTime.class, objectMapper));
    Configuration<String, LocalDateTime> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, LocalDateTime> cache = Caching.getCachingProvider().getCacheManager().createCache("test", config);
    LocalDateTime t = LocalDateTime.now();
    cache.put("1", t);
    Assertions.assertEquals(t, cache.get("1"));
    cache.close();
    runner.stop();
}
Also used : LocalDateTime(java.time.LocalDateTime) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) RedisRunner(org.redisson.RedisRunner) URL(java.net.URL) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) TypedJsonJacksonCodec(org.redisson.codec.TypedJsonJacksonCodec) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 70 with JavaTimeModule

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

the class Jackson method configure.

private static ObjectMapper configure(ObjectMapper mapper) {
    mapper.registerModule(new GuavaModule());
    mapper.registerModule(new GuavaExtrasModule());
    mapper.registerModule(new CaffeineModule());
    mapper.registerModule(new JodaModule());
    mapper.registerModule(new BlackbirdModule());
    mapper.registerModule(new FuzzyEnumModule());
    mapper.registerModule(new ParameterNamesModule());
    mapper.registerModule(new Jdk8Module());
    mapper.registerModule(new JavaTimeModule());
    mapper.setPropertyNamingStrategy(new AnnotationSensitivePropertyNamingStrategy());
    mapper.setSubtypeResolver(new DiscoverableSubtypeResolver());
    mapper.disable(FAIL_ON_UNKNOWN_PROPERTIES);
    return mapper;
}
Also used : BlackbirdModule(com.fasterxml.jackson.module.blackbird.BlackbirdModule) Jdk8Module(com.fasterxml.jackson.datatype.jdk8.Jdk8Module) ParameterNamesModule(com.fasterxml.jackson.module.paramnames.ParameterNamesModule) JodaModule(com.fasterxml.jackson.datatype.joda.JodaModule) JavaTimeModule(com.fasterxml.jackson.datatype.jsr310.JavaTimeModule) GuavaModule(com.fasterxml.jackson.datatype.guava.GuavaModule)

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