-
Notifications
You must be signed in to change notification settings - Fork 61
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
Number serialization of java types outside of IEEE754 double precisio… #176
Conversation
…n range. Signed-off-by: Roman Grigoriadi <[email protected]>
Fixes #160 |
I think that this functionality should be implemented on a higher level (JSONB). JSONP is a pure parser and should not contain this kind of logic. |
@m0mus JSONP is a Java API to serialize Java to JSON. This API includes types such as BigDecimal, Long and BigInteger and should be aware of how to handle them properly. Otherwise its clients (which are not using JSONB) would have no control over it. |
@bravehorsie JSONP is not an API to serialize Java Objects to JSON, it's JSONB. JSONP is a low level parser. It's used to write and read JSON data. If you need to write a number as string you should convert it to String and use JSONP methods to write strings. |
Well, this issue is still open and JsonGenerator inconsistent.
So "JSON number value" is the result of "toString()" and "is used as the text value". Means the generator is designed to write not JSON portable outputs. Just to comment on your last message @m0mus , issue is not on parsing side (we support more, it is great) but on writing side (we write things consumers can't read). Being said both yasson and johnzon write BigDecimal as string, I guess the spec (javadoc here) can be clarified with "The specified value is written as a JSON string value.". Then there is no more issue since shape of the output json is constant for the same input types so consumers can deal with it. Hope it makes sense. |
The last communication was a little while ago, but unless @m0mus objects, do you @rmannibucau already have a working committer agreement for Eclipse or is there still a problem with your employer, because if you have an ECA then why not propose a PR for the spec? |
@keilw it is ok now, I'm fine doing the PR once we decide if we
I'm for 2 since both impl picked the same behavior. |
I agree with @rmannibucau . We should fix the javadoc. I would appreciate if you provide a PR. |
Many people are confusing JSON and JavaScript or ECMAScript. |
@leadpony well you can also consider that:
edit: here is the PR #237 |
@rmannibucau
I am not a native English speaker, but I believe allow means may, it does not mean must or should. |
@leadpony "allow implementations" means "outside of the spec" so yes it is not forbidden, all implementations can have a toogle to enable that (passed in JsonGeneratorFactory properties) but spec can't specify that as a supported behavior. Rephrased: implementations are allowed to not be spec compliant with some specific configuration while they respect the spec by default. Here to embrace the best we can RFC7159 and encourage interoperability, default must be a deterministic typing with a good interoperability level, i.e. String cause all others options have more pitfalls than this one (not saying I'm happy with that btw, but it is the "least worse" I see in our current ecosystem). |
@leadpony @rmannibucau I re-read the javadoc and think that it may be OK. It means that the written value is JSONNumber (it means without quotes) and it's text value is the same as output of |
@m0mus hmm, then we are back to the fact it is polymorphic and not as deterministic for consumers as expected so then this issue (and its friends) must be reopened. |
@m0mus |
@leadpony @rmannibucau Why you don't accept the "BigDecimal.toString()" workaround if |
@leadpony Are you saying that the current Jakarta JSONP implementation (this project) serializes BigDecimals as Strings? |
@m0mus I accepted the workaround but it does not solve the issue that we have an API which does not do what the users expect, or rephrased, it is an API user must not use generally. As mentioned, 80% of the time you will have this interoperability requirement so by default it should not overflow as a number making current API almost unusable. Another way to see it is to follow you reasonning: why don't we only have write([String,],JsonValue) method? So if we type we must enable the user to have his most common use cases working at least IMHO. This is super impacting for JSON-B because users start to share JSON-P components and JSON-B (to save some memory when there are pooling strategies or just reuse some global configs) and if JSON-B (or any layer on top of JSON-P) use that API then the behavior is encapsulated for users making it quite hard to change. Since all impl use a String since 1.0 I think it makes sense to keep it specified as writing it as a String (and from an API point of view it is consistent with what the Java Big wrappers are so it is user friendly). About the remark on the naming, everything is named |
@m0mus try (JsonGenerator g = Json.createGenerator(out)) {
g.writeStartObject();
g.write("amount", new BigDecimal("123.456"));
g.writeEnd();
} outputted the following JSON: {"amount":123.456} |
Logic to write Java numbers as a JSON string does not belong inside the serializer. |
@jjspiegel we don't speak of the parser but generator. Question is really should BigDecimal (BigInteger) be represented in JSON as they are in java (by strings) and enabled a stable representation type or should they violate their contract and try to be represented as number (no quote) and break most JSON parsers out there which will try to parse it as a double (including in java ecosystem, there are a tons of minijson/simple-json forks doing that). |
@rmannibucau I agree that we should not introduce any backwards incompatible changes. What do you want to change in javadoc? Do you want to have a note there that for better compatibility user may consider writing it as Sting using toStong() method? |
@m0mus originally did the same error than @leadpony and through RI and other impls were writing real JSON string values so wanted to change JSON number to JSON string, but since they all write the string not quoted (seems it is validated by TCKs ;)), we should document at least that this method is not portable and should be used with caution at least. I agree that pointing out the write(decimal.toString()) is a solution we must explicit probably. |
…n range.
Signed-off-by: Roman Grigoriadi [email protected]