-
-
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 @JsonFormat
for String
, numbers, using String.format()
#1114
Comments
@JsonFormat
for String
, numbers, using String.format()@JsonFormat
for String
, numbers, using String.format()
I was about to open an issue suggesting adding support for |
@mjustin Probably not. The main question is, I think, two-fold:
|
Note: |
Hi @cowtowncoder , does this issue mean supporting the use-case below, where static class IntAsThousandSeparatedString {
@JsonFormat(shape=Shape.STRING, pattern="%,d")
@JsonProperty("value")
public int foo = 3_000;
} And it should be serialized as follow: { "value" : "3,000" } I understand that it should support other numeric types and different locales as well. I would like to contribute on this one. If you could give me some hints about where to get started, that will be great. |
@mincong-h Yes, this is correct. As to supported types: it is ok to implement support for smaller set of types, for example just for On hints, here are some thoughts:
Handling of various |
Thanks for your hints, @cowtowncoder. I had a draft here to modify the ToStringSerializer master...mincong-h:iss1114 But more I think about this issue, more I feel this feature might not be that easy. Serialization is completely fine, using At a high-level overview, I believe users expect Jackson annotations to work for both serialization and deserialization. Meaning that when declaring a
I think it's better to have the following alternatives:
Annotation Another alternative I see is @JsonNumberFormat(thousands=" ", decimal=",")
public double frenchValue = 1_234.56; // "1 234,56" In this way, each parameter seems easier to parse compared to the "generic" approach of String.format(). Also supporting the deserialization could be easier than |
I don't like the idea of datatype-specific formatters, mostly because that leads to complexity on handling side as well as on number of special-purpose annotations. One really nice feature of At the same time it is true that there are issues with simple textual format definition: not so much performance since we never process format String more than once (it is done when constructing (de)serializer and resulting format object used afterwards), but it may be confusing to try to figure supported formats. As to serialization-only vs read-write: it is difficult to say what users expect, in a way, since existing format string is only used with date/time values... and yes, they can be read and written. So... I don't know what would be a good way forward. Here are some thoughts:
From this, I guess earlier comment by @mjustin -- about whether to split issue(s) -- makes sense again: yes, I think there are separate concerns of number-specific possibility, and then more general-purpose "String decoration" capability. |
Hope this feature can be implement, spring framework has a |
My suggestion: #4273 |
@hurelhuyag #4273 doesn't appear to be anything like what is described here. Try writing a test with a JsonFormat annotation in it and showing how that the test can be made to work with the changes you are suggesting. Adding new constructors doesn't help when the code that calls the constructors is done deep within the jackson-databind code. Any PR that doesn't attempt to prove that the code does something useful with a test case - this is not a good start. |
@pjfanning Everyone here wants to have number formatting instead of just calling There are 2 ways to do it.
I suggest using the second method directly in It is not a time to write tests. It is time, we have to decide which method to use and where to put the feature. After that, tests can be written. |
We'll need deserializer support too. Serializer support on its own is not enough. |
@hurelhuyag do you come from a coding culture where devs only write tests to capture regression? The tests you're being asked here is to demonstrate usage so people can easily see (from a user's perspective) what you have proposed/enabled/implemented/fixed. |
I 100% think that it is exactly time to write tests to show how things work (and IF they work) :) I added some notes on PR itself; but there are fairly big questions to resolve:
On (2), |
@yihtserns @pjfanning You may want to have a look at PR. I think we can make this work, but I have one design concern wrt
I can see both use cases useful for some usage. To me, use of Other would be to default to one style or other. My initial leaning was that since we are dealing with Numbers, default should be to WDYT? |
Formatted numbers should be rendered as JSON strings. Spec compliant JSON parsers will not parse the formatted numbers if they are rendered as JSON numbers (without quotes). If we really want to support rendering them as JSON numbers (and users are happy that the consumers have JSON parsers that will accept them), then maybe we could add an extra param on |
Thank you @pjfanning. I think it depends: I think it'd be possible someone wanted to specify precision and produce compliant number output. In that case it'd seem wrong to quote value. But yes, it depends on intent which is difficult to deduce. However, I realized that maybe As to overriding number, no need for a new setting, just use
But I guess assuming One final possibility is that there is new |
Overview of current Pattern & Shape support by various data typesThis is just so we're on the same page regarding the current state (actually mainly for my personal reference, but I thought I should just share here). Be warned that this was made from a quick check so it is likely not exhaustive - I'm sure I missed some things.
|
@yihtserns is there written test code used to validate the research above? Asking this so that we may have process of expanding support/non-support |
Nope: it was generated using the help of IDE+👀. 😅 |
I was under the impression that this is about Perhaps question about generating non-String formatted value should be separated into another issue/ticket.
So far everything that supports ...waitaminute is there an API to parse a number-string using
I'm ashamed to say that I'm not terribly familiar with
|
@yihtserns Right, most things try to support serialization and deserialization. But there is no hard limit of this having to be the case. In case of Numbers I suspect supporting round-tripping may well be difficult. But if we are to support deserialization, I think that:
As to configuration of Still, I think there are many "write-only" use cases where either:
|
Seems like it would be nice to just support formats available via format Strings that
String.format()
uses.Note that existing support for date/time values use different formatting; this should be fine, and if we want to for some reason support something from
java.text.Format
it should be possible to use heuristics to determine intended type of format. However that may not be necessary.Initially we should just support simple types:
java.lang.String
int
,long
,short
,byte
,float
,double
and matching wrapper typesBigDecimal
,BigInteger
The text was updated successfully, but these errors were encountered: