jackson-modules-java8 icon indicating copy to clipboard operation
jackson-modules-java8 copied to clipboard

Add 2 new `JavaTimeFeature`s: for truncating Time values before serialization, after deserialization

Open cowtowncoder opened this issue 2 months ago • 2 comments

(note: continuation of #374)

Existing features

  • DeserializationFeature.READ_DATE_TIMESTAMPS_AS_NANOSECONDS
  • SerializationFeature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS

allow changing unit-of-time associated with numeric (JSON Number) timestamps b/w milliseconds (false) and nanoseconds (true), in cases where Time value has granularity beyond milliseconds. As a consequence, when disabled, precision is truncated to millisecond resolution.

These settings have no effect on textual (JSON String) representations, however.

It would be useful to have explicit features -- for this module, that'd be JavaTimeFeature (in 2.x -- in 3.x it'd be databind's new DateTimeFeature) -- like:

  • JavaTimeFeature.TRUNCATE_TO_MSECS_ON_READ
  • JavaTimeFeature.TRUNCATE_TO_MSECS_ON_WRITE

which would:

  1. TRUNCATE_TO_MSECS_ON_READ enabled: after reading Time value, clear nanoseconds part before returning to caller (affects Numeric timestamps too)
  2. TRUNCATE_TO_MSECS_ON_WRITE enabled: before writing Time value, clear nanoseconds part

These would alongside but independent @JsonFormat(pattern = ) configuration.

Open questions:

  • Should this feature be added in 2.x (2.21) or only 3.x?

cowtowncoder avatar Oct 14 '25 00:10 cowtowncoder

Given there is already way to handle expected behavior I think 3.x?

JooHyukKim avatar Oct 14 '25 00:10 JooHyukKim

@JooHyukKim No, there is no truncation logic in 3.x either (aside from using @JsonFormat(pattern = ...) for serialization which is also available on 2.x).

cowtowncoder avatar Oct 14 '25 02:10 cowtowncoder