Details
-
Type: New Feature
-
Status: Closed
-
Priority: Major
-
Resolution: Fixed
-
Affects Version/s: 1.3
-
Fix Version/s: 1.4
-
Component/s: Converters
-
Labels:None
Description
There's a "feature"/issue (http://jira.codehaus.org/browse/JETTISON-36) that I introduced into Jettison project version 1.0.1 > where the JSON converter will attempt to convert numeric values in Java to numeric literals in JSON. The trouble is JSON uses 64 bit floating point values to represent numeric values. So if you have a very large long value, when that "literal" is converted into Javascript it will lose precision.
To fix this Jettison allows you to specify a SimpleConverter that will quote numeric values. The trouble is the JettisonMappedXMLDriver passes in an empty Configuration so I needed to add a constructor that passes in a Configuration so that I could specify SimpleConverter. (I included the patch)
Here's what I changed in JettisonMappedXMLDriver (I've also included the SVN patch text file):
public JettisonMappedXmlDriver()
public JettisonMappedXmlDriver(final Configuration config)
{ mof = new MappedXMLOutputFactory(config); mif = new MappedXMLInputFactory(config); convention = new MappedNamespaceConvention(config); }Here's how I'm using it:
private static XStream getXstream()
{ //return new XStream(new JettisonMappedXmlDriver()); StringWriter strWriter = new StringWriter(); Configuration config = new Configuration(); config.setTypeConverter(new SimpleConverter()); JettisonMappedXmlDriver xmlDriver = new JettisonMappedXmlDriver(config); return new XStream(xmlDriver); }My pom.xml is as follows:
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<version>1.3.2-SNAPSHOT</version>
<artifactId>xstream</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.1-SNAPSHOT</version>
</dependency>
I've also attached a unit test to validate that the JettisonMappedXMLDriver will surround numeric literals with quotes when using the SimpleConverter otherwise not. Jettison may want to reevaluate how the type conversion works, possibly force it to be surrounded by quotes if it's a 64 bit long value, or if it's guranteed that the value is larger than the largest integer value that can be represented by a 64 bit float without possibly losing precision (which I believe is anything past the 2^31 -1).
The patch for the driver has been applied. The test is different for now, since the SimpleConverter is not yet in a released version of Jettison and we do not want to depend on a 3rd party SNAPSHOT version. You may give the head revision a try.