Skip to content

Commit a1fcffe

Browse files
'
1 parent 8a50d65 commit a1fcffe

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

Test.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ of this software and associated documentation files (the "Software"), to deal
4141
*
4242
* @author JSON.org
4343
* @author yusuke at mac.com
44-
* @version 2010-12-29
44+
* @version 2011-01-13
4545
*/
4646
public class Test extends TestCase {
4747
public Test(String name) {
@@ -350,8 +350,8 @@ public void testJSON() throws Exception {
350350
"}}", jsonobject.toString(4));
351351

352352
jsonobject = new JSONObject(beanie);
353-
//assertEquals("{\"string\":\"A beany object\",\"BENT\":\"All uppercase key\",\"boolean\":true,\"number\":42,\"x\":\"x\"}"
354-
// , jsonobject.toString());
353+
assertEquals("{\"string\":\"A beany object\",\"BENT\":\"All uppercase key\",\"boolean\":true,\"number\":42,\"x\":\"x\"}"
354+
, jsonobject.toString());
355355

356356
string = "{ \"entity\": { \"imageURL\": \"\", \"name\": \"IXXXXXXXXXXXXX\", \"id\": 12336, \"ratingCount\": null, \"averageRating\": null } }";
357357
jsonobject = new JSONObject(string);
@@ -491,7 +491,7 @@ public void testJSON() throws Exception {
491491
" ],\n" +
492492
" \"slashes\": \"///\"\n" +
493493
"}", jsonobject.toString(2));
494-
assertEquals("<quotes>'</quotes><quotes>&quot;</quotes><slashes>///</slashes><ei><quotes>&quot;'</quotes></ei><eo><b>don't</b><a>&quot;quoted&quot;</a></eo><closetag>&lt;/script&gt;</closetag><backslash>\\</backslash>",
494+
assertEquals("<quotes>&apos;</quotes><quotes>&quot;</quotes><slashes>///</slashes><ei><quotes>&quot;&apos;</quotes></ei><eo><b>don&apos;t</b><a>&quot;quoted&quot;</a></eo><closetag>&lt;/script&gt;</closetag><backslash>\\</backslash>",
495495
XML.toString(jsonobject));
496496

497497
jsonobject = new JSONObject(
@@ -520,7 +520,7 @@ public void testJSON() throws Exception {
520520
jsonarray.put(false);
521521
jsonarray.put(new JSONArray());
522522
jsonarray.put(new JSONObject());
523-
jsonobject.put("keys", JSONObject.getNames(jsonobject));
523+
jsonobject.put("keys", JSONObject.getNames(jsonobject));
524524
assertEquals("{\n" +
525525
" \"JSONArray\": [],\n" +
526526
" \"JSONObject\": {},\n" +
@@ -581,8 +581,8 @@ public void testJSON() throws Exception {
581581
" \"true\": true,\n" +
582582
" \"zero\": -0\n" +
583583
"}", jsonobject.toString(4));
584-
// assertEquals("<to>null</to><ten>10</ten><JSONObject></JSONObject><op>Good</op><keys>[Ljava.lang.String;@4d125127</keys><int>57</int><true>true</true><foo>true</foo><foo>false</foo><foo>9876543210</foo><foo>0.0</foo><foo>1.00000001</foo><foo>1.000000000001</foo><foo>1.0</foo><foo>1.0E-17</foo><foo>2.0</foo><foo>0.1</foo><foo>2.0E100</foo><foo>-32</foo><foo></foo><foo></foo><foo>string</foo><foo>666</foo><foo>2001.99</foo><foo>so &quot;fine&quot;.</foo><foo>so &lt;fine&gt;.</foo><foo>true</foo><foo>false</foo><foo></foo><foo></foo><zero>-0.0</zero><double>1.2345678901234568E29</double><String>98.6</String><false>false</false><bool>true</bool><\\u2028>?</\\u2028><\\u2029>?</\\u2029><null>null</null>",
585-
// XML.toString(j));
584+
assertEquals("<to>null</to><ten>10</ten><JSONObject></JSONObject><op>Good</op><keys>to</keys><keys>ten</keys><keys>JSONObject</keys><keys>JSONArray</keys><keys>op</keys><keys>int</keys><keys>true</keys><keys>foo</keys><keys>zero</keys><keys>double</keys><keys>String</keys><keys>false</keys><keys>bool</keys><keys>\\u2028</keys><keys>\\u2029</keys><keys>null</keys><int>57</int><true>true</true><foo>true</foo><foo>false</foo><foo>9876543210</foo><foo>0.0</foo><foo>1.00000001</foo><foo>1.000000000001</foo><foo>1.0</foo><foo>1.0E-17</foo><foo>2.0</foo><foo>0.1</foo><foo>2.0E100</foo><foo>-32</foo><foo></foo><foo></foo><foo>string</foo><foo>666</foo><foo>2001.99</foo><foo>so &quot;fine&quot;.</foo><foo>so &lt;fine&gt;.</foo><foo>true</foo><foo>false</foo><foo></foo><foo></foo><zero>-0.0</zero><double>1.2345678901234568E29</double><String>98.6</String><false>false</false><bool>true</bool><\\u2028>\u2028</\\u2028><\\u2029>\u2029</\\u2029><null>null</null>",
585+
XML.toString(jsonobject));
586586
assertEquals(98.6d, jsonobject.getDouble("String"), eps);
587587
assertTrue(jsonobject.getBoolean("bool"));
588588
assertEquals(null, jsonobject.getString("to"));
@@ -1057,7 +1057,7 @@ public void testJSON() throws Exception {
10571057
" \"xmlns:xsi\": \"http://www.w3.org/1999/XMLSchema-instance\"\n" +
10581058
"}}", jsonobject.toString(2));
10591059

1060-
assertEquals("<SOAP-ENV:Envelope><SOAP-ENV:Body><ns1:doGoogleSearch><oe>latin1<xsi:type>xsd:string</xsi:type></oe><SOAP-ENV:encodingStyle>http://schemas.xmlsoap.org/soap/encoding/</SOAP-ENV:encodingStyle><lr><xsi:type>xsd:string</xsi:type></lr><start>0<xsi:type>xsd:int</xsi:type></start><q>'+search+'<xsi:type>xsd:string</xsi:type></q><ie>latin1<xsi:type>xsd:string</xsi:type></ie><safeSearch>false<xsi:type>xsd:boolean</xsi:type></safeSearch><xmlns:ns1>urn:GoogleSearch</xmlns:ns1><restrict><xsi:type>xsd:string</xsi:type></restrict><filter>true<xsi:type>xsd:boolean</xsi:type></filter><maxResults>10<xsi:type>xsd:int</xsi:type></maxResults><key>GOOGLEKEY<xsi:type>xsd:string</xsi:type></key></ns1:doGoogleSearch></SOAP-ENV:Body><xmlns:xsd>http://www.w3.org/1999/XMLSchema</xmlns:xsd><xmlns:xsi>http://www.w3.org/1999/XMLSchema-instance</xmlns:xsi><xmlns:SOAP-ENV>http://schemas.xmlsoap.org/soap/envelope/</xmlns:SOAP-ENV></SOAP-ENV:Envelope>",
1060+
assertEquals("<SOAP-ENV:Envelope><SOAP-ENV:Body><ns1:doGoogleSearch><oe>latin1<xsi:type>xsd:string</xsi:type></oe><SOAP-ENV:encodingStyle>http://schemas.xmlsoap.org/soap/encoding/</SOAP-ENV:encodingStyle><lr><xsi:type>xsd:string</xsi:type></lr><start>0<xsi:type>xsd:int</xsi:type></start><q>&apos;+search+&apos;<xsi:type>xsd:string</xsi:type></q><ie>latin1<xsi:type>xsd:string</xsi:type></ie><safeSearch>false<xsi:type>xsd:boolean</xsi:type></safeSearch><xmlns:ns1>urn:GoogleSearch</xmlns:ns1><restrict><xsi:type>xsd:string</xsi:type></restrict><filter>true<xsi:type>xsd:boolean</xsi:type></filter><maxResults>10<xsi:type>xsd:int</xsi:type></maxResults><key>GOOGLEKEY<xsi:type>xsd:string</xsi:type></key></ns1:doGoogleSearch></SOAP-ENV:Body><xmlns:xsd>http://www.w3.org/1999/XMLSchema</xmlns:xsd><xmlns:xsi>http://www.w3.org/1999/XMLSchema-instance</xmlns:xsi><xmlns:SOAP-ENV>http://schemas.xmlsoap.org/soap/envelope/</xmlns:SOAP-ENV></SOAP-ENV:Envelope>",
10611061
XML.toString(jsonobject));
10621062

10631063
jsonobject = new JSONObject("{Envelope: {Body: {\"ns1:doGoogleSearch\": {oe: \"latin1\", filter: true, q: \"'+search+'\", key: \"GOOGLEKEY\", maxResults: 10, \"SOAP-ENV:encodingStyle\": \"http://schemas.xmlsoap.org/soap/encoding/\", start: 0, ie: \"latin1\", safeSearch:false, \"xmlns:ns1\": \"urn:GoogleSearch\"}}}}");
@@ -1073,7 +1073,7 @@ public void testJSON() throws Exception {
10731073
" \"start\": 0,\n" +
10741074
" \"xmlns:ns1\": \"urn:GoogleSearch\"\n" +
10751075
"}}}}", jsonobject.toString(2));
1076-
assertEquals("<Envelope><Body><ns1:doGoogleSearch><oe>latin1</oe><SOAP-ENV:encodingStyle>http://schemas.xmlsoap.org/soap/encoding/</SOAP-ENV:encodingStyle><start>0</start><q>'+search+'</q><ie>latin1</ie><safeSearch>false</safeSearch><xmlns:ns1>urn:GoogleSearch</xmlns:ns1><maxResults>10</maxResults><key>GOOGLEKEY</key><filter>true</filter></ns1:doGoogleSearch></Body></Envelope>",
1076+
assertEquals("<Envelope><Body><ns1:doGoogleSearch><oe>latin1</oe><SOAP-ENV:encodingStyle>http://schemas.xmlsoap.org/soap/encoding/</SOAP-ENV:encodingStyle><start>0</start><q>&apos;+search+&apos;</q><ie>latin1</ie><safeSearch>false</safeSearch><xmlns:ns1>urn:GoogleSearch</xmlns:ns1><maxResults>10</maxResults><key>GOOGLEKEY</key><filter>true</filter></ns1:doGoogleSearch></Body></Envelope>",
10771077
XML.toString(jsonobject));
10781078

10791079
jsonobject = CookieList.toJSONObject(" f%oo = b+l=ah ; o;n%40e = t.wo ");

XML.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ of this software and associated documentation files (the "Software"), to deal
2424
SOFTWARE.
2525
*/
2626

27+
import java.lang.reflect.Array;
2728
import java.util.Iterator;
2829

2930

3031
/**
3132
* This provides static methods to convert an XML text into a JSONObject,
3233
* and to covert a JSONObject into an XML text.
3334
* @author JSON.org
34-
* @version 2010-12-24
35+
* @version 2011-01-13
3536
*/
3637
public class XML {
3738

@@ -90,6 +91,9 @@ public static String escape(String string) {
9091
case '"':
9192
sb.append("&quot;");
9293
break;
94+
case '\'':
95+
sb.append("&apos;");
96+
break;
9397
default:
9498
sb.append(c);
9599
}
@@ -443,18 +447,29 @@ public static String toString(Object object, String tagName)
443447
ja = (JSONArray)value;
444448
length = ja.length();
445449
for (i = 0; i < length; i += 1) {
446-
value = ja.get(i);
447-
if (value instanceof JSONArray) {
450+
value = ja.get(i);
451+
if (value instanceof JSONArray) {
448452
sb.append('<');
449453
sb.append(key);
450454
sb.append('>');
451-
sb.append(toString(value));
455+
sb.append(toString(value));
452456
sb.append("</");
453457
sb.append(key);
454458
sb.append('>');
455-
} else {
456-
sb.append(toString(value, key));
457-
}
459+
} else {
460+
sb.append(toString(value, key));
461+
}
462+
}
463+
} else if (value.getClass().isArray()) {
464+
length = Array.getLength(value);
465+
for (i = 0; i < length; i += 1) {
466+
sb.append('<');
467+
sb.append(key);
468+
sb.append('>');
469+
sb.append(Array.get(value, i).toString());
470+
sb.append("</");
471+
sb.append(key);
472+
sb.append('>');
458473
}
459474
} else if (value.equals("")) {
460475
sb.append('<');

0 commit comments

Comments
 (0)