smithy4s icon indicating copy to clipboard operation
smithy4s copied to clipboard

Allow more precise representation of numeric types in Document

Open ghostbuster91 opened this issue 1 year ago • 3 comments

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.

ghostbuster91 avatar Sep 05 '24 20:09 ghostbuster91

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

plokhotnyuk avatar Nov 25 '24 15:11 plokhotnyuk

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)

kubukoz avatar Nov 26 '24 13:11 kubukoz

@plokhotnyuk I think that'd be really valuable :+1:

Baccata avatar Dec 03 '24 09:12 Baccata