How to serialize BigInteger and BigDouble?
See original GitHub issueCurrently I’m in the need of serializing numbers with many decimal digits (30+ decimal digits to be more precise). To deserialzie a JSON file with this many decimal points I used Java’s BigIntegers and BigDecimal types which works fine. The problem that arises is when I want to serialize that value. It will be serialized as an Integer or Double respectively which cuts and rounds the actual value.
One example value is this, received in a JSON file.
0.083532162010669708251953125000
After deserializing I will have exactly a value of that above as a JsonLiteral. But when I serialize this value I will get a result of:
0.08353216201066971
Which is not my desired result.
A snippet from my unit tests showing the differences between expected and actual:
My questions are now how should I serialized BigIntegers and BigDoubles and will there be support for this kind of data types in the future?
Issue Analytics
- State:
- Created 3 years ago
- Reactions:14
- Comments:13 (4 by maintainers)
Top GitHub Comments
As it is, there is no way to encode a JSON number outside of Kotlin’s primitive types, which means that it is impossible to encode a decimal value without inducing precision loss.
encodeString
,StreamingJsonEncoder
will quote the value, regardless of the descriptor kind.encodeJsonElement
withJsonPrimitive
, the value’s string representation will be converted usingString.toDoubleOrNull()
, not only inducing precision loss but formatting the value using engineering notation (e.g.1.11222333444E11
).Bear in mind that ECMA-404 does not specify that JSON numbers must represent IEEE-754 values; they are simply strings of digits with optional fraction and exponent parts. From json.org:
JsonEncoder should expose a mechanism to write an unquoted JSON number, which would solve this particular issue and allow for the use of non-
Number
types.Yes, in fact, directly converting them was my initial plan until I saw that you cannot directly serialize Any types. As you answered in some old issue that we should use JsonObject to serialize types of Map<String, Any> I then did the same thing for every other type that had Any in it, i.e. JsonArray for List<Any> and so on.
I also fideled around with custom serializers but I gave up eventually since I couldn’t get it to work. The easiest solution was for me to simply let kotlinx do the deserialization and then convert it with simple methods to standard kotlin types. Same goes for serialization.
Thanks for the answer, so I need a custom serializer for that.
Is it possible for the deserialization/serialization of BigInts/Doubles to become a feature in the future so we no longer need a custom serializer for very long numbers?