com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer Java Examples

The following examples show how to use com.fasterxml.jackson.datatype.jsr310.ser.ZonedDateTimeSerializer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ZonedDateTimeObjectMapperCustomizer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void customize(ObjectMapper objectMapper) {
    JavaTimeModule customDateModule = new JavaTimeModule();
    customDateModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(
            new DateTimeFormatterBuilder().appendInstant(0).toFormatter().withZone(ZoneId.of("Z"))));
    customDateModule.addDeserializer(ZonedDateTime.class, new ZonedDateTimeEuropeLondonDeserializer());
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .registerModule(customDateModule);
}
 
Example #2
Source File: TimeCustomizer.java    From quarkus with Apache License 2.0 5 votes vote down vote up
@Override
public void customize(ObjectMapper objectMapper) {
    JavaTimeModule customDateModule = new JavaTimeModule();
    customDateModule.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(
            new DateTimeFormatterBuilder().appendInstant(0).toFormatter().withZone(ZoneId.of("Z"))));
    objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .registerModule(customDateModule);
}
 
Example #3
Source File: WebMvcConfig.java    From spring-http-patch-example with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {

    Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
    builder
            .serializerByType(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE)
            .deserializerByType(ZonedDateTime.class, ZONED_DATE_TIME)
            .applicationContext(applicationContext);
    return builder;
}
 
Example #4
Source File: TestZonedDateTimeSerializationWithCustomFormatter.java    From jackson-modules-java8 with Apache License 2.0 5 votes vote down vote up
private String serializeWith(ZonedDateTime zonedDateTime, DateTimeFormatter f) throws Exception {
    ObjectMapper mapper = JsonMapper.builder()
            .addModule(new SimpleModule().addSerializer(
                    new ZonedDateTimeSerializer(f)))
            .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
            .build();
    return mapper.writeValueAsString(zonedDateTime);
}
 
Example #5
Source File: JSONMapper.java    From core-ng-project with Apache License 2.0 5 votes vote down vote up
private static JavaTimeModule timeModule() {
    var module = new JavaTimeModule();

    // redefine date time formatter to output nano seconds in at least 3 digits, which inline with ISO standard and ES standard
    DateTimeFormatter localTimeFormatter = new DateTimeFormatterBuilder()
            .parseStrict()
            .appendValue(HOUR_OF_DAY, 2)
            .appendLiteral(':')
            .appendValue(MINUTE_OF_HOUR, 2)
            .appendLiteral(':')
            .appendValue(SECOND_OF_MINUTE, 2)
            .appendFraction(NANO_OF_SECOND, 3, 9, true) // always output 3 digits of nano seconds (iso date format doesn't specify how many digits it should present, here always keep 3)
            .toFormatter();

    module.addSerializer(ZonedDateTime.class, new ZonedDateTimeSerializer(ISO_INSTANT));
    module.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(new DateTimeFormatterBuilder()
            .parseStrict()
            .append(ISO_LOCAL_DATE)
            .appendLiteral('T')
            .append(localTimeFormatter)
            .toFormatter()));
    module.addSerializer(LocalTime.class, new LocalTimeSerializer(new DateTimeFormatterBuilder()
            .parseStrict()
            .append(localTimeFormatter)
            .toFormatter()));
    return module;
}