r/java
•
u/loicmathieu
•
Jul 23 '26
•
1
JEP 540: Simple JSON API (Incubator)
https://openjdk.org/jeps/540
79
Upvotes
View all comments
Show parent comment
15
u/s888marks Jul 23 '26
See also my other reply.
I agree that handling numbers in JSON requires care. The design is that JsonNumber represents a number in a JSON document; it's not a numeric type. If you want to perform numeric operations on it, you need to convert it into an actual Java numeric type first. Why not make JsonNumber a numeric type? Because that would require making a lot of numeric decisions that really have nothing to do with JSON. For example, does "0.0" equal "0.00"? (Discuss amongst yourselves.)
The difficulty is that there is no Java numeric type that can losslessly represent any JSON number. BigDecimal comes close in that it can represent arbitrary precision decimals, though it has a limited exponent range of about +/- 231. However, BigDecimal doesn't support negative zero, which can be written in a JSON document and which is converted properly by
asDouble. So, we don't have any value-comparison operations on JsonNumber itself. Instead, convert to the numeric type that does what you want, and proceed from there.I don't know what you mean by "disparate number types". There's only JsonNumber, which is the decimal digits in the JSON document, and then there are Java numeric types. That's it.