-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DurationSerializer
ignores format pattern if nano-second serialization enabled
#224
Comments
Actually same happens to some other classes like Instant |
Yes, looks like One thing that would help would be to show a test case: description is helpful (and should be quite easy to create a test), but often the original use case is a very good test case on its own. |
This is my primary issue: class MyDto {
@JsonFormat(pattern = "MINUTES")
@JsonProperty("durationInMins")
private final Duration duration;
public MyDto(Duration d) { duration = d; }
public Duration getDuration() { return duration; }
}
var mapper = new ObjectMapper().findAndRegisterModules();
Map<String, Object> map = mapper.convertValue(new MyDto(Duration.ofHours(2)), new TypeReference<>(){});
assertEqual(map.get("durationInMins"), 120); This will fail since the serialized value is the number of nanoseconds, so the format is totally ignored, even though the duration converter under the hood is correct, it's just not used as I explained earlier. Fixing the shape not being used would allow me to pass the test case by adding P.S. wrote the test case by hand |
I added a modified, failing test to reproduce the issue. Unfortunately I don't have time to pursue this further at this point, but I hope someone else can -- I will make time to review PRs and so on, just not actively write a patch myself. @kupci Not sure what you think -- might be able to add in a 2.13.x patch although I am also worried that a change might be needed in construction/contextualization. |
DurationSerializer
ignores format pattern if nano-second serialization enabled
@Sam-Kruglov FINALLY found time to get back to this and hope I fixed it correctly -- trivially simple change but I'm slightly worried about regression. Still, as things are tests pass and it'll go in 2.14.0 unless something is found. But I was wondering wrt |
jackson-modules-java8/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerializer.java
Lines 84 to 88 in 3075bfe
If I do this, I expect an integer number in JSON representing the number minutes:
But it serializes it as nanoseconds. After some debugging, I decided to add
, shape = NUMBER_INT
in order to forcecom.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase#useNanoseconds
respondfalse
, but it has no effect because shape is not used in the factory method I linked above.Found a workaround: adding
, without = JsonFormat.Feature.WRITE_DATE_TIMESTAMPS_AS_NANOSECONDS
to@JsonFormat
. However, it only works if I add it to each property, adding it on class level has no effect.The text was updated successfully, but these errors were encountered: