Skip to content

Commit e13de0b

Browse files
number
1 parent 5a4fb65 commit e13de0b

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

JSONML.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ of this software and associated documentation files (the "Software"), to deal
3232
* JSONObject, and to covert a JSONArray or JSONObject into an XML text using
3333
* the JsonML transform.
3434
* @author JSON.org
35-
* @version 2010-12-23
35+
* @version 2011-10-03
3636
*/
3737
public class JSONML {
3838

@@ -45,8 +45,11 @@ public class JSONML {
4545
* @return A JSONArray if the value is the outermost tag, otherwise null.
4646
* @throws JSONException
4747
*/
48-
private static Object parse(XMLTokener x, boolean arrayForm,
49-
JSONArray ja) throws JSONException {
48+
private static Object parse(
49+
XMLTokener x,
50+
boolean arrayForm,
51+
JSONArray ja
52+
) throws JSONException {
5053
String attribute;
5154
char c;
5255
String closeTag = null;
@@ -439,8 +442,10 @@ public static String toString(JSONObject jo) throws JSONException {
439442
if (object != null) {
440443
if (object instanceof String) {
441444
sb.append(XML.escape(object.toString()));
442-
} else if (object instanceof JSONObject) {
443-
sb.append(toString((JSONObject)object));
445+
} else if (object instanceof Number) {
446+
sb.append(object.toString());
447+
} else if (object instanceof JSONObject) {
448+
sb.append(toString((JSONObject)object));
444449
} else if (object instanceof JSONArray) {
445450
sb.append(toString((JSONArray)object));
446451
}

Test.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ of this software and associated documentation files (the "Software"), to deal
4040
* comparisons of .toString to a string literal are likely to fail.
4141
*
4242
* @author JSON.org
43-
* @version 2011-05-22
43+
* @version 2011-10-03
4444
*/
4545
public class Test extends TestCase {
4646
public Test(String name) {
@@ -91,7 +91,11 @@ public void testJSON() throws Exception {
9191

9292
Beany beanie = new Beany("A beany object", 42, true);
9393

94-
string = "[0.1]";
94+
string = "[001122334455]";
95+
jsonarray = new JSONArray(string);
96+
assertEquals("[1122334455]", jsonarray.toString());
97+
98+
string = "[00.10]";
9599
jsonarray = new JSONArray(string);
96100
assertEquals("[0.1]", jsonarray.toString());
97101

@@ -154,16 +158,17 @@ public void testJSON() throws Exception {
154158
jsonarray.toString(4));
155159
assertEquals("<div id=\"demo\" class=\"JSONML\"><p>JSONML is a transformation between<b>JSON</b>and<b>XML</b>that preserves ordering of document features.</p><p>JSONML can work with JSON arrays or JSON objects.</p><p>Three<br/>little<br/>words</p></div>",
156160
JSONML.toString(jsonarray));
161+
162+
string = "{\"xmlns:soap\":\"http://www.w3.org/2003/05/soap-envelope\",\"tagName\":\"soap:Envelope\",\"childNodes\":[{\"tagName\":\"soap:Header\"},{\"tagName\":\"soap:Body\",\"childNodes\":[{\"tagName\":\"ws:listProducts\",\"childNodes\":[{\"tagName\":\"ws:delay\",\"childNodes\":[1]}]}]}],\"xmlns:ws\":\"http://warehouse.acme.com/ws\"}";
163+
jsonobject = new JSONObject(string);
164+
assertEquals("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ws=\"http://warehouse.acme.com/ws\"><soap:Header/><soap:Body><ws:listProducts><ws:delay>1</ws:delay></ws:listProducts></soap:Body></soap:Envelope>",
165+
JSONML.toString(jsonobject));
157166

158167
string = "<person created=\"2006-11-11T19:23\" modified=\"2006-12-31T23:59\">\n <firstName>Robert</firstName>\n <lastName>Smith</lastName>\n <address type=\"home\">\n <street>12345 Sixth Ave</street>\n <city>Anytown</city>\n <state>CA</state>\n <postalCode>98765-4321</postalCode>\n </address>\n </person>";
159168
jsonobject = XML.toJSONObject(string);
160169
assertEquals("{\"person\": {\n \"lastName\": \"Smith\",\n \"address\": {\n \"postalCode\": \"98765-4321\",\n \"street\": \"12345 Sixth Ave\",\n \"state\": \"CA\",\n \"type\": \"home\",\n \"city\": \"Anytown\"\n },\n \"created\": \"2006-11-11T19:23\",\n \"firstName\": \"Robert\",\n \"modified\": \"2006-12-31T23:59\"\n}}",
161170
jsonobject.toString(4));
162171

163-
jsonobject = new JSONObject(beanie);
164-
assertEquals("{\"string\":\"A beany object\",\"BENT\":\"All uppercase key\",\"boolean\":true,\"number\":42,\"x\":\"x\"}"
165-
, jsonobject.toString());
166-
167172
string = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
168173
jsonobject = new JSONObject(string);
169174
assertEquals("{\"entity\": {\n \"id\": 12336,\n \"averageRating\": null,\n \"ratingCount\": null,\n \"name\": \"IXXXXXXXXXXXXX\",\n \"imageURL\": \"\"\n}}",

0 commit comments

Comments
 (0)