@Bean @ConditionalOnMissingBean(ThreeTenModule.class) ThreeTenModule threeTenModule() { ThreeTenModule module = new ThreeTenModule(); module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT); module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME); module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME); return module; } }
protected T _fromDecimal(DeserializationContext context, BigDecimal value) { long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return fromNanoseconds.apply(new FromDecimalArguments( seconds, nanoseconds, getZone(context))); }
/** * @since 2.7.3 */ public static BigDecimal toBigDecimal(long seconds, int nanoseconds) { if (nanoseconds == 0L) { // 14-Mar-2015, tatu: Let's retain one zero to avoid interpretation // as integral number if (seconds == 0L) { // except for "0.0" where it can not be done without scientific notation return BigDecimal.ZERO.setScale(1); } return BigDecimal.valueOf(seconds).setScale(9); } return new BigDecimal(toDecimal(seconds, nanoseconds)); }
BigDecimal value = parser.getDecimalValue(); long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return fromNanoseconds.apply(new FromDecimalArguments( seconds, nanoseconds, getZone(context))); long timestamp = parser.getLongValue(); if (context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) { return this.fromNanoseconds.apply(new FromDecimalArguments( timestamp, 0, this.getZone(context) )); return this.fromMilliseconds.apply(new FromIntegerArguments( timestamp, this.getZone(context) )); try { TemporalAccessor acc = _formatter.parse(string); value = parsedToValue.apply(acc); if (context.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)) { return adjust.apply(value, this.getZone(context));
addDeserializer(Instant.class, InstantDeserializer.INSTANT); addDeserializer(OffsetDateTime.class, InstantDeserializer.OFFSET_DATE_TIME); addDeserializer(ZonedDateTime.class, InstantDeserializer.ZONED_DATE_TIME); addDeserializer(Duration.class, DurationDeserializer.INSTANCE); addDeserializer(LocalDateTime.class, LocalDateTimeDeserializer.INSTANCE); addDeserializer(LocalDate.class, LocalDateDeserializer.INSTANCE); addDeserializer(LocalTime.class, LocalTimeDeserializer.INSTANCE); addDeserializer(MonthDay.class, MonthDayDeserializer.INSTANCE); addDeserializer(OffsetTime.class, OffsetTimeDeserializer.INSTANCE); addDeserializer(Period.class, ThreeTenStringParsableDeserializer.PERIOD); addDeserializer(Year.class, YearDeserializer.INSTANCE); addDeserializer(YearMonth.class, YearMonthDeserializer.INSTANCE); addDeserializer(ZoneId.class, ThreeTenStringParsableDeserializer.ZONE_ID); addDeserializer(ZoneOffset.class, ThreeTenStringParsableDeserializer.ZONE_OFFSET); addSerializer(Duration.class, DurationSerializer.INSTANCE); addSerializer(Instant.class, InstantSerializer.INSTANCE); addSerializer(LocalDateTime.class, LocalDateTimeSerializer.INSTANCE); addSerializer(LocalDate.class, LocalDateSerializer.INSTANCE); addSerializer(LocalTime.class, LocalTimeSerializer.INSTANCE); addSerializer(MonthDay.class, MonthDaySerializer.INSTANCE); addSerializer(OffsetDateTime.class, OffsetDateTimeSerializer.INSTANCE); addSerializer(OffsetTime.class, OffsetTimeSerializer.INSTANCE); addSerializer(Period.class, new ToStringSerializer(Period.class)); addSerializer(Year.class, YearSerializer.INSTANCE); addSerializer(YearMonth.class, YearMonthSerializer.INSTANCE); addSerializer(ZonedDateTime.class, ZonedDateTimeSerializer.INSTANCE);
BigDecimal value = parser.getDecimalValue(); long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return Duration.ofSeconds(seconds, nanoseconds); return Duration.parse(string); } catch (DateTimeException e) { return _rethrowDateTimeException(parser, context, e, string);
private ZoneId getZone(DeserializationContext context) { // Instants are always in UTC, so don't waste compute cycles return (_valueClass == Instant.class) ? null : DateTimeUtils.timeZoneToZoneId(context.getTimeZone()); }
@Override public void serialize(T value, JsonGenerator generator, SerializerProvider provider) throws IOException { if (useTimestamp(provider)) { if (provider.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)) { generator.writeNumber(DecimalUtils.toBigDecimal( getEpochSeconds.applyAsLong(value), getNanoseconds.applyAsInt(value) )); return; } generator.writeNumber(getEpochMillis.applyAsLong(value)); return; } String str; if (_formatter != null) { str = _formatter.format(value);; } else if (defaultFormat != null) { str = defaultFormat.format(value);; } else { str = value.toString(); } generator.writeString(str); }
@Override public void serialize(Duration duration, JsonGenerator generator, SerializerProvider provider) throws IOException { if (useTimestamp(provider)) { if (provider.isEnabled(SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS)) { generator.writeNumber(DecimalUtils.toBigDecimal( duration.getSeconds(), duration.getNano() )); } else { generator.writeNumber(duration.toMillis()); } } else { // Does not look like we can make any use of DateTimeFormatter here? generator.writeString(duration.toString()); } }
AnnotatedMethod factory = _findFactory(ac, "of", String.class); if (factory != null) { inst.configureFromStringCreator(factory);
BigDecimal value = parser.getDecimalValue(); long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return fromNanoseconds.apply(new FromDecimalArguments( seconds, nanoseconds, getZone(context))); long timestamp = parser.getLongValue(); if (context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) { return this.fromNanoseconds.apply(new FromDecimalArguments( timestamp, 0, this.getZone(context) )); return this.fromMilliseconds.apply(new FromIntegerArguments( timestamp, this.getZone(context) )); try { TemporalAccessor acc = _formatter.parse(string); value = parsedToValue.apply(acc); if (context.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)) { return adjust.apply(value, this.getZone(context));
private static ObjectMapper createDefaultObjectMapper() { ObjectMapper objectMapper = new ObjectMapper() .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS) .setDateFormat(new RFC3339DateFormat()) .setSerializationInclusion(Include.NON_NULL); objectMapper.configure(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL, true); ThreeTenModule module = new ThreeTenModule(); module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT); module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME); module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME); objectMapper.registerModule(module); return objectMapper; }
private ZoneId getZone(DeserializationContext context) { // Instants are always in UTC, so don't waste compute cycles return (_valueClass == Instant.class) ? null : DateTimeUtils.timeZoneToZoneId(context.getTimeZone()); }
BigDecimal value = parser.getDecimalValue(); long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return fromNanoseconds.apply(new FromDecimalArguments( seconds, nanoseconds, getZone(context))); long timestamp = parser.getLongValue(); if (context.isEnabled(DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS)) { return this.fromNanoseconds.apply(new FromDecimalArguments( timestamp, 0, this.getZone(context) )); return this.fromMilliseconds.apply(new FromIntegerArguments( timestamp, this.getZone(context) )); try { TemporalAccessor acc = _formatter.parse(string); value = parsedToValue.apply(acc); if (context.isEnabled(DeserializationFeature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)) { return adjust.apply(value, this.getZone(context));
public JSON() { mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false); mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); mapper.enable(SerializationFeature.WRITE_ENUMS_USING_TO_STRING); mapper.enable(DeserializationFeature.READ_ENUMS_USING_TO_STRING); mapper.setDateFormat(new RFC3339DateFormat()); ThreeTenModule module = new ThreeTenModule(); module.addDeserializer(Instant.class, CustomInstantDeserializer.INSTANT); module.addDeserializer(OffsetDateTime.class, CustomInstantDeserializer.OFFSET_DATE_TIME); module.addDeserializer(ZonedDateTime.class, CustomInstantDeserializer.ZONED_DATE_TIME); mapper.registerModule(module); }
private ZoneId getZone(DeserializationContext context) { // Instants are always in UTC, so don't waste compute cycles return (_valueClass == Instant.class) ? null : DateTimeUtils.timeZoneToZoneId(context.getTimeZone()); }