Details
-
Type: Bug
-
Status: Closed
-
Priority: Major
-
Resolution: Fixed
-
Affects Version/s: None
-
Fix Version/s: 1.4.5
-
Component/s: None
-
Labels:None
Description
The normal conversion of string values to JSON attempts to convert values to numeric values, but this is problematic for numeric values like 64 bit Long's when converted to Javascript, they are converted to 64 bit floating point values, so values larger than I believe 2^51 or something like that can lose precision.
We can use the SimpleConverter and I put in an issue into XStream to ensure that users can configure the JettisonMappedXMLDriver, but even in the default behavior of Jettison we probably shouldn't allow numeric values that are larger than can be converted into 64 bit double precision floating point (although I guess that's a javascript specific limitation, and maybe Javascript can check that when it's evaluating and if the numeric value doesn't match what the string field was it would fail, but I'm not sure that's really easy to change).
Here's the XStream patch with the issue, the solution right now is to simply use the SimpleConverter.
http://jira.codehaus.org/browse/XSTR-540
There is actually no easy answer to this. The JSON specification itself refers only the JavaScript specification as base. I am quite sure that there are a lot Java (and JavaScript) programmers that are not aware that JavaScript uses 64-bit double precision values only and that it is not possible to map the complete range of a 64-bit integer. Any integer value greater than 2^53 or lower than -2^53 can actually no longer be represented in JSON without a possible interoperability problem.
XStream supports currently two JSON implementations writing JSON, a native one and Jettison version 1.2.
For Jettison it is possible to set an alternative TypeConverter, e.g. Jettison's simple converter will write any value as String. Even Jettison's default converter supports a system property jettison.mapped.typeconverter.enforce_32bit_integer to write only 32-bit integer values as numbers and bigger ones as strings.
XStream's JsonWriter has now a new mode IEEE_754_MODE to write any 64-bit integer that exceeds the range mentioned above as string.
However, anything works as expected, if client and server use JSON implementations that can and will represent 64-bit integer values. Therefore the conversion of big 64-bit integers is not done by default.