Skip to content
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

Allow more precise representation of numeric types in Document #1579

Open
ghostbuster91 opened this issue Sep 5, 2024 · 2 comments
Open
Milestone

Comments

@ghostbuster91
Copy link
Contributor

For some values following doesn't hold

Json.payloadCodecs.encoders.fromSchema[MyStructure].apply(MyStructure(...)) 
=== 
Json.payloadCodecs.encoders.fromSchema[Document].apply(Document.encode(MyStructure(...))) 

The reasons is that Document has only one type for all numeric values - DNumber that is backed by BigDecimal.
Some json encoders including the one from jsonitter perform a clever optimization in order to render float values in an optimal way. That can't done on the right side because we lost the information that it was a single precision floating point number.
The left side will encode 123.4 float value as 123.4 json number literal. On the right hand side we will instead get 123.4000015258789 which is the closest number that can be represented in binary format when using IEEE 754 encoding.

While this is not an issue for IEE 754 compatible parsers (so almost everyone), extending the Document ADT with more granular types for numeric values will allow greater control over how are they encoded when going through the Document type.

@plokhotnyuk
Copy link
Contributor

plokhotnyuk commented Nov 25, 2024

If you consider adding more granularity for numbers, then I can add a parsing method in jsoniter-scala-core that will parse JSON numbers to the most suitable implementation of java.lang.Number that can hold it without rounding (or some subset of implementations, like: Int, Long, scala.math.BigInteger, Float, Double, scala.math.BigDecimal).

@kubukoz
Copy link
Member

kubukoz commented Nov 26, 2024

that sounds nice :)

Looks like we've agreed that this should be done as part of the 0.19 release, so such a function in jsoniter would be great to have sometime in the next 2 months (I expect to have more time to work/organize on smithy4s starting from the next year)

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

3 participants