Skip to content

*Serializer ignores format pattern if nano-second serialization enabled #248

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

Closed
Sam-Kruglov opened this issue Aug 3, 2022 · 4 comments
Closed

Comments

@Sam-Kruglov
Copy link

Copying from the previous issue #224

The following example has been fixed but you can try replacing Duration with Instant or any other temporal type to check if the issue reproduces or not:

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);

Originally posted by @Sam-Kruglov in #224 (comment)

@Sam-Kruglov Sam-Kruglov changed the title #Serializer ignores format pattern if nano-second serialization enabled *Serializer ignores format pattern if nano-second serialization enabled Aug 3, 2022
@cowtowncoder
Copy link
Member

cowtowncoder commented Aug 3, 2022

Thank you @Sam-Kruglov. Actually I was looking for specific cases. I think I can leave this open as a placeholder to include known cases to fix.

But one quick note: handling of Duration (and other types) as Map keys requires more work since key serializers are different from value serializers -- and there is no current support for configuration via @JsonFormat.

Actually since the test above specifically tests as-Map-key serialization, I will change the title as well.

@cowtowncoder
Copy link
Member

cowtowncoder commented Aug 3, 2022

Hmmh. Does that code actually work? This:

new TypeReference<>(){}

should be illegal as it has no type information.

EDIT: I was mistaken, see Sam's comment below.

@Sam-Kruglov
Copy link
Author

Sam-Kruglov commented Aug 3, 2022

@cowtowncoder hey, I don't actually remember what I meant by Instant having similar issues, I just tried to poke around it and couldn't find anything, the pattern seems to work fine.

Also, the above example compiles and runs on JDK 11 with no problems, maybe you tried JDK 8? Yes, I can use diamond notation in the type reference as it fills the type in from the declaration. And the example is serializing duration in values, not in keys.

Thanks for looking into it!

@cowtowncoder
Copy link
Member

@Sam-Kruglov Ok yes, I do build with Java 8. Good to know it might actually work later on. Should have thought this might be it. Learned something new :-)

Also yeah, I can see that Map was solely for convenience of testing. But it did get me thinking into "Duration as Map key" which is actually another missing piece of functionality. But you are right, that's not what test did.

And I think there may well still be issues for maybe other types so we can open issues once they are encountered.

Same goes for Duration-as-Map-key (and others).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants