question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`DurationSerializer` ignores format pattern if nano-second serialization enabled

See original GitHub issue

https://github.com/FasterXML/jackson-modules-java8/blob/3075bfe40669fd1853407c2f949a63d1f1f1fa8b/datetime/src/main/java/com/fasterxml/jackson/datatype/jsr310/ser/DurationSerializer.java#L84-L88

If I do this, I expect an integer number in JSON representing the number minutes:

@JsonFormat(pattern = "MINUTES")
@JsonProperty("durationInMins")
Duration duration;

But it serializes it as nanoseconds. After some debugging, I decided to add , shape = NUMBER_INT in order to force com.fasterxml.jackson.datatype.jsr310.ser.JSR310FormattedSerializerBase#useNanoseconds respond false, 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.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
cowtowncodercommented, Aug 3, 2022

@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 Instant you mentioned has similar issues: would it be possible to show similar unit test? If so, could you please file a separate new issue for type(s) and I should be able to see if I could do more fixes before 2.14.0 release (planning to start release candidates soon).

1reaction
cowtowncodercommented, Sep 26, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson Serialize Instant to Nanosecond Issue - Stack Overflow
When we are working with Java 8 Time package and Jackson good idea is to use jackson-modules-java8 project which serves many serialisers and ......
Read more >
Jackson Date - Baeldung
In this tutorial, we'll serialize dates with Jackson. We'll start by serializing a simple java.util.Date, then Joda-Time, and finally, ...
Read more >
SerializationFeature (jackson-databind 2.13.0 API) - FasterXML
Feature that controls whether numeric timestamp values are to be written using nanosecond timestamps (enabled) or not (disabled); if and only if datatype ......
Read more >
Formatting Java Time with Spring Boot using JSON - TouK
This will change formatting of LocalDate in every endpoint of your API. We have to define: – DateTimeFormatter with our pattern. – Serializer...
Read more >
Jackson JSON - Using @JsonFormat to format Date and Enum
TimeZone to use for serialization (if needed) public String timezone() ... assigned to a desired SimpleDateFormat-compatible pattern.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found