-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Support annotation override for WRITE_DATES_AS_TIMESTAMPS #4278
Comments
The proposed use case seems to fit into ObjectWriter usage. Have you considered using ObjectWriter w = GLOBAL_MAPPER
.writer()
.without(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS); |
Hi @JooHyukKim. Honestly I did not and I thank you for your suggestion, but that doesn't really give the flexibility I was looking for. Most of the times I don't deal directly with The ability to disable that feature on the Let me know if I made it clear enough, please 🙂 Thanks |
Coincidentally, I did a quick study on @JsonFormat yesterday, which made me think your problem can be solved with this: If the default is to serialize dates as timestamp (i.e. numbers), but you want to serialize it as
If the default is to serialize dates as
|
Hi @yihtserns . Thank you so much, that is really helpful. I gave a try to your suggestion and it does seem to work: @JsonFormat(shape = JsonFormat.Shape.NUMBER)
private Instant timestamp; The only flaw I can see with this approach, is that you lose the flexibility to also use Not saying your suggestion isn't great - honestly I didn't thought of it and probably will solve most of the cases. But I still think there is merit to implement my suggestion, as it will result in a clearer and also more flexible contract. I should also add that it feels very counterintuitive to me that Please let me know your thoughts 🙂 |
On JDK 11, this:
...resulted in: {
"instantAsString" : "2023-12-23T11:28:20.493370400Z",
"instantAsNumber" : 1703330900.493370400,
"instantAsNumberWithoutNanos" : 1703330900493,
"instantAsNumberWithNanos" : 1703330900.493370400
}
I'm guessing having |
@yihtserns you are absolutely right. I just tried it again and it behaved exactly as you stated. What mislead me was that I was testing it on a unit test with a mocked clock and the value being passed didn't have enough precision for the decimal part to kick in 😅 What you described seems like a working solution, but not a obvious one for most people. Maybe it should be clearer on documentation (at least I didn't find it) or, perhaps even better, have a I would like to hear more thoughts on this 🙂 Again, thank you so much for pointing a working solution! |
I think And even though I knew about the existence of |
Sorry for late response but as the background, I have been trying to move away from datatype-specific But at the same time, there is indeed the problem of "subtype" beyond There is also yet another possibility I had been developing:
and originally I thought we would also want I am not sure what the best way forward is, but I would ideally avoid addition of |
Is your feature request related to a problem? Please describe.
Not a problem, just lack of flexibility.
Describe the solution you'd like
It is quite normal for someone to create an
ObjectMapper
and configure it with reasonable defaults that apply to the majority of serializations/deserializations done within a project. For anything that deviates from these defaults, we can annotate and override the defaults. This mechanism is great, since it allows to follow the principle of "convention over configuration", but still allows flexibility when needed.It seems however that there is no feature in
JsonFormat.Feature
allowing to override, for an individual case, the value chosen bySerializationFeature.WRITE_DATES_AS_TIMESTAMPS
.This would be great to have, as most of the times people have a particular preference, and that is what they use on their projects, but they need to deal with 3rd parties which JSON contract differs in approach. Would be great to be able to just annotate on those specific objects.
Usage example
Potential use, if that
JsonFormat.Feature
was added:Additional context
No response
The text was updated successfully, but these errors were encountered: