Friday, September 20, 2013

JSON discrepancy in gwt-dev.jar vs. json-20090211.jar

I spent much of yesterday moving an old GWT project from Ant to Maven. Much of that time was spent running down a serious discrepancy in the server-side conversion of XML to JSON, specifically the call org.json.XML.toJSONObject(java.lang.String string). (I'm thinking this needs to be filed as an issue.)

The project was originally built with Ant. Following the build.xml in the GWT samples, it had gwt-dev.jar in the project classpath (see, for example, Showcase's build.xml). When run in devmode, this version of JSON will convert

<width>448</width>
<height>293</height>

to 

{"height":"293", "width":"448"}

When this arrives at the client-side, in terms of the com.google.gwt.json.client package we have a JSONString vs. a JSONNumber.

In my Maven build, the pom.xml declares dependencies only on gwt-servlet.jar and gwt-user.jar, neither of which includes org.json (though gwt-servlet-deps.jar has the same org.json as gwt-dev.jar). JSON is being provided by json-20090211.jar from mavenrepository.com. Now that same XML converts to

{"height":293, "width":448}

JSONNumber vs JSONString! This makes a huge difference in how the data is handled:

   JSONString: int width = Integer.parseInt(jsonObject.get("width").isString().stringValue());

   JSONNumber: int width = (int)jsonObject.get("width").isNumber().doubleValue();

Use the wrong one and an exception is thrown.

I think that json-20090211.jar is the correct approach. Certainly AutoBean would fail if it got a string vs a number for an integer field (or maybe just a zero value?).

One can get the same results with the Ant in devmode by removing <fileset dir="${gwt.sdk}" includes="gwt-dev*.jar"/> from project.class.path and adding it only to the gwtc and devmode targets (and any target using GWTTestCase). However I think that GWT should be brought in line with json-20090211.jar. (Looking at the tools directory for building GWT, it seems the JSON used (r2_20080312) is not a version available from mavenrepository.com.)

Has anyone else seen this? I don't see an issue filed on it. I'm happy to put one in, but I thought I'd raise it here first.


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment