1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Merge pull request #6 from stleary/JSONMLTest-for-toString-issue

Jsonml test for to string issue
This commit is contained in:
Sean Leary 2015-05-02 14:19:16 -05:00
commit 43396cf603

View file

@ -6,36 +6,63 @@ import org.json.*;
import org.junit.Test; import org.junit.Test;
/** /**
* Tests for JSON-Java JSONML.java * Tests for org.json.JSONML.java
*
* Certain inputs are expected to result in exceptions. These tests are
* executed first. JSONML provides an API to:
* Convert an XML string into a JSONArray or a JSONObject.
* Convert a JSONArray or JSONObject into an XML string.
* Both fromstring and tostring operations operations should be symmetrical
* within the limits of JSONML.
* It should be possible to perform the following operations, which should
* result in the original string being recovered, within the limits of the
* underlying classes:
* Convert a string -> JSONArray -> string -> JSONObject -> string
* Convert a string -> JSONObject -> string -> JSONArray -> string
*
*/ */
public class JSONMLTest { public class JSONMLTest {
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void nullXMLException() { public void nullXMLException() {
/**
* Attempts to transform a null XML string to JSON
*/
String xmlStr = null; String xmlStr = null;
JSONML.toJSONArray(xmlStr); JSONML.toJSONArray(xmlStr);
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void emptyXMLException() { public void emptyXMLException() {
/**
* Attempts to transform an empty XML string to JSON
*/
String xmlStr = ""; String xmlStr = "";
JSONML.toJSONArray(xmlStr); JSONML.toJSONArray(xmlStr);
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void nonXMLException() { public void nonXMLException() {
/**
* Attempts to transform a nonXML string to JSON
*/
String xmlStr = "{ \"this is\": \"not xml\"}"; String xmlStr = "{ \"this is\": \"not xml\"}";
JSONML.toJSONArray(xmlStr); JSONML.toJSONArray(xmlStr);
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void emptyTagException() { public void emptyTagException() {
/**
* jsonArrayStr is used to build a JSONArray which is then
* turned into XML. For this transformation, all arrays represent
* elements and the first array entry is the name of the element.
* In this case, one of the arrays does not have a name
*/
String jsonArrayStr = String jsonArrayStr =
"[\"addresses\","+ "[\"addresses\","+
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
// this array has no name
"["+ "["+
"[\"name\"],"+ "[\"name\"],"+
"[\"nocontent\"],"+ "[\"nocontent\"],"+
@ -48,10 +75,18 @@ public class JSONMLTest {
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void spaceInTagException() { public void spaceInTagException() {
/**
* jsonArrayStr is used to build a JSONArray which is then
* turned into XML. For this transformation, all arrays represent
* elements and the first array entry is the name of the element.
* In this case, one of the element names has an embedded space,
* which is not allowed.
*/
String jsonArrayStr = String jsonArrayStr =
"[\"addresses\","+ "[\"addresses\","+
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
// this array has an invalid name
"[\"addr esses\","+ "[\"addr esses\","+
"[\"name\"],"+ "[\"name\"],"+
"[\"nocontent\"],"+ "[\"nocontent\"],"+
@ -63,7 +98,12 @@ public class JSONMLTest {
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void unvalidSlashInTagException() { public void invalidSlashInTagException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
* In this case, the XML is invalid because the 'name' element
* contains an invalid frontslash.
*/
String xmlStr = String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
@ -71,13 +111,18 @@ public class JSONMLTest {
" <address>\n"+ " <address>\n"+
" <name/x>\n"+ " <name/x>\n"+
" <street>abc street</street>\n"+ " <street>abc street</street>\n"+
" </address>\n"+ " </address>\n"+
"</addresses>"; "</addresses>";
JSONML.toJSONArray(xmlStr); JSONML.toJSONArray(xmlStr);
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void invalidBangInTagException() { public void invalidBangInTagException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
* In this case, the XML is invalid because an element
* has the invalid name '!'.
*/
String xmlStr = String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
@ -85,13 +130,18 @@ public class JSONMLTest {
" <address>\n"+ " <address>\n"+
" <name/>\n"+ " <name/>\n"+
" <!>\n"+ " <!>\n"+
" </address>\n"+ " </address>\n"+
"</addresses>"; "</addresses>";
JSONML.toJSONArray(xmlStr); JSONML.toJSONArray(xmlStr);
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void invalidBangNoCloseInTagException() { public void invalidBangNoCloseInTagException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
* In this case, the XML is invalid because an element
* starts with '!' and has no closing tag
*/
String xmlStr = String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
@ -99,13 +149,18 @@ public class JSONMLTest {
" <address>\n"+ " <address>\n"+
" <name/>\n"+ " <name/>\n"+
" <!\n"+ " <!\n"+
" </address>\n"+ " </address>\n"+
"</addresses>"; "</addresses>";
JSONML.toJSONArray(xmlStr); JSONML.toJSONArray(xmlStr);
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void noCloseStartTagException() { public void noCloseStartTagException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
* In this case, the XML is invalid because an element
* has no closing '>'.
*/
String xmlStr = String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
@ -113,13 +168,56 @@ public class JSONMLTest {
" <address>\n"+ " <address>\n"+
" <name/>\n"+ " <name/>\n"+
" <abc\n"+ " <abc\n"+
" </address>\n"+ " </address>\n"+
"</addresses>";
JSONML.toJSONArray(xmlStr);
}
@Test(expected=JSONException.class)
public void noCloseEndTagException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
* In this case, the XML is invalid because an element
* has no name after the closing tag '</'.
*/
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
" <address>\n"+
" <name/>\n"+
" <abc/>\n"+
" </>\n"+
"</addresses>";
JSONML.toJSONArray(xmlStr);
}
@Test(expected=JSONException.class)
public void noCloseEndBraceException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
* In this case, the XML is invalid because an element
* has '>' after the closing tag '</' and name.
*/
String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
" xsi:noNamespaceSchemaLocation=\"test.xsd\">\n"+
" <address>\n"+
" <name/>\n"+
" <abc/>\n"+
" </address\n"+
"</addresses>"; "</addresses>";
JSONML.toJSONArray(xmlStr); JSONML.toJSONArray(xmlStr);
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void invalidCDATABangInTagException() { public void invalidCDATABangInTagException() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
* In this case, the XML is invalid because an element
* does not have a complete CDATA string.
*/
String xmlStr = String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
@ -134,24 +232,42 @@ public class JSONMLTest {
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void nullJSONXMLException() { public void nullJSONXMLException() {
/**
* Tries to convert a null JSONArray to XML.
*/
JSONArray jsonArray= null; JSONArray jsonArray= null;
JSONML.toString(jsonArray); JSONML.toString(jsonArray);
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void emptyJSONXMLException() { public void emptyJSONXMLException() {
/**
* Tries to convert an empty JSONArray to XML.
*/
JSONArray jsonArray = new JSONArray(); JSONArray jsonArray = new JSONArray();
JSONML.toString(jsonArray); JSONML.toString(jsonArray);
} }
@Test @Test
public void complexTypeXML() { public void toJSONArray() {
/**
* xmlStr contains XML text which is transformed into a JSONArray.
* Each element becomes a JSONArray:
* 1st entry = elementname
* 2nd entry = attributes object (if present)
* 3rd entry = content (if present)
* 4th entry = child element JSONArrays (if present)
* The result is compared against an expected JSONArray.
* The transformed JSONArray is then transformed back into a string
* which is used to create a final JSONArray, which is also compared
* against the expected JSONArray.
*/
String xmlStr = String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
"xsi:noNamespaceSchemaLocation='test.xsd'>\n"+ "xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
"<address>\n"+ "<address attr1=\"attrValue1\" attr2=\"attrValue2\" attr3=\"attrValue3\">\n"+
"<name/>\n"+ "<name nameType=\"mine\">myName</name>\n"+
"<nocontent/>>\n"+ "<nocontent/>>\n"+
"</address>\n"+ "</address>\n"+
"</addresses>"; "</addresses>";
@ -160,7 +276,8 @@ public class JSONMLTest {
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
"[\"address\","+ "[\"address\","+
"[\"name\"],"+ "{\"attr1\":\"attrValue1\",\"attr2\":\"attrValue2\",\"attr3\":\"attrValue3\"},"+
"[\"name\", {\"nameType\":\"mine\"},\"myName\"],"+
"[\"nocontent\"],"+ "[\"nocontent\"],"+
"\">\""+ "\">\""+
"]"+ "]"+
@ -174,15 +291,34 @@ public class JSONMLTest {
} }
@Test @Test
public void basicXMLAsObject() { public void toJSONObjectToJSONArray() {
/**
* xmlStr contains XML text which is transformed into a JSONObject,
* restored to XML, transformed into a JSONArray, and then restored
* to XML again. Both JSONObject and JSONArray should contain the same
* information and should produce the same XML, allowing for non-ordered
* attributes.
*
* Transformation to JSONObject:
* The elementName is stored as a string where key="tagName"
* Attributes are simply stored as key/value pairs
* If the element has either content or child elements, they are stored
* in a jsonArray with key="childNodes".
*
* Transformation to JSONArray:
* 1st entry = elementname
* 2nd entry = attributes object (if present)
* 3rd entry = content (if present)
* 4th entry = child element JSONArrays (if present)
*/
String xmlStr = String xmlStr =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ "<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+
"xsi:noNamespaceSchemaLocation='test.xsd'>\n"+ "xsi:noNamespaceSchemaLocation='test.xsd'>\n"+
"<address>\n"+ "<address addrType=\"my address\">\n"+
"<name>Joe Tester</name>\n"+ "<name nameType=\"my name\">Joe Tester</name>\n"+
"<street>[CDATA[Baker street 5]</street>\n"+ "<street><![CDATA[Baker street 5]]></street>\n"+
"<NothingHere/>\n"+ "<NothingHere except=\"an attribute\"/>\n"+
"<TrueValue>true</TrueValue>\n"+ "<TrueValue>true</TrueValue>\n"+
"<FalseValue>false</FalseValue>\n"+ "<FalseValue>false</FalseValue>\n"+
"<NullValue>null</NullValue>\n"+ "<NullValue>null</NullValue>\n"+
@ -193,6 +329,7 @@ public class JSONMLTest {
"<ArrayOfNum>\n"+ "<ArrayOfNum>\n"+
"<value>1</value>\n"+ "<value>1</value>\n"+
"<value>2</value>\n"+ "<value>2</value>\n"+
"<value><subValue svAttr=\"svValue\">abc</subValue></value>\n"+
"<value>3</value>\n"+ "<value>3</value>\n"+
"<value>4.1</value>\n"+ "<value>4.1</value>\n"+
"<value>5.2</value>\n"+ "<value>5.2</value>\n"+
@ -200,90 +337,121 @@ public class JSONMLTest {
"</address>\n"+ "</address>\n"+
"</addresses>"; "</addresses>";
String expectedStr = String expectedJSONObjectStr =
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+ "{"+
"\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
"\"childNodes\":["+ "\"childNodes\":["+
"{\"childNodes\":["+ "{"+
"{\"childNodes\":[\"Joe Tester\"],"+ "\"childNodes\":["+
"\"tagName\":\"name\"},"+ "{"+
"{\"childNodes\":[\"[CDATA[Baker street 5]\"],"+ "\"childNodes\":[\"Joe Tester\"],"+
"\"tagName\":\"street\"},"+ "\"nameType\":\"my name\","+
"{\"tagName\":\"NothingHere\"},"+ "\"tagName\":\"name\""+
"{\"childNodes\":[true],"+ "},"+
"\"tagName\":\"TrueValue\"},"+ "{"+
"{\"childNodes\":[false],"+ "\"childNodes\":[\"Baker street 5\"],"+
"\"tagName\":\"FalseValue\"},"+ "\"tagName\":\"street\""+
"{\"childNodes\":[null],"+ "},"+
"\"tagName\":\"NullValue\"},"+ "{"+
"{\"childNodes\":[42],"+ "\"tagName\":\"NothingHere\","+
"\"tagName\":\"PositiveValue\"},"+ "\"except\":\"an attribute\""+
"{\"childNodes\":[-23],"+ "},"+
"\"tagName\":\"NegativeValue\"},"+ "{"+
"{\"childNodes\":[-23.45],"+ "\"childNodes\":[true],"+
"\"tagName\":\"DoubleValue\"},"+ "\"tagName\":\"TrueValue\""+
"{\"childNodes\":[\"-23x.45\"],"+ "},"+
"\"tagName\":\"Nan\"},"+ "{"+
"{\"childNodes\":["+ "\"childNodes\":[false],"+
"{\"childNodes\":[1],"+ "\"tagName\":\"FalseValue\""+
"\"tagName\":\"value\"},"+ "},"+
"{\"childNodes\":[2],"+ "{"+
"\"tagName\":\"value\"},"+ "\"childNodes\":[null],"+
"{\"childNodes\":[3],"+ "\"tagName\":\"NullValue\""+
"\"tagName\":\"value\"},"+ "},"+
"{\"childNodes\":[4.1],"+ "{"+
"\"tagName\":\"value\"},"+ "\"childNodes\":[42],"+
"{\"childNodes\":[5.2],"+ "\"tagName\":\"PositiveValue\""+
"\"tagName\":\"value\"}"+ "},"+
"],"+ "{"+
"\"tagName\":\"ArrayOfNum\"}"+ "\"childNodes\":[-23],"+
"],"+ "\"tagName\":\"NegativeValue\""+
"\"tagName\":\"address\"}"+ "},"+
"{"+
"\"childNodes\":[-23.45],"+
"\"tagName\":\"DoubleValue\""+
"},"+
"{"+
"\"childNodes\":[\"-23x.45\"],"+
"\"tagName\":\"Nan\""+
"},"+
"{"+
"\"childNodes\":["+
"{"+
"\"childNodes\":[1],"+
"\"tagName\":\"value\""+
"},"+
"{"+
"\"childNodes\":[2],"+
"\"tagName\":\"value\""+
"},"+
"{"+
"\"childNodes\":["+
"{"+
"\"childNodes\":[\"abc\"],"+
"\"svAttr\":\"svValue\","+
"\"tagName\":\"subValue\""+
"}"+
"],"+
"\"tagName\":\"value\""+
"},"+
"{"+
"\"childNodes\":[3],"+
"\"tagName\":\"value\""+
"},"+
"{"+
"\"childNodes\":[4.1],"+
"\"tagName\":\"value\""+
"},"+
"{"+
"\"childNodes\":[5.2],"+
"\"tagName\":\"value\""+
"}"+
"],"+
"\"tagName\":\"ArrayOfNum\""+
"}"+
"],"+
"\"addrType\":\"my address\","+
"\"tagName\":\"address\""+
"}"+
"],"+ "],"+
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\","+
"\"tagName\":\"addresses\"}"; "\"tagName\":\"addresses\""+
JSONObject jsonObject = JSONML.toJSONObject(xmlStr); "}";
JSONObject expectedJsonObject = new JSONObject(expectedStr);
String xmlToStr = JSONML.toString(jsonObject);
JSONObject finalJsonObject = JSONML.toJSONObject(xmlToStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
}
@Test String expectedJSONArrayStr =
public void basicXMLAsArray() { "["+
String xmlStr = "\"addresses\","+
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"+ "{"+
"<addresses xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\""+ "\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
"xsi:noNamespaceSchemaLocation='test.xsd'>\n"+ "\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\""+
"<address>\n"+ "},"+
"<name>Joe Tester</name>\n"+ "["+
"<street>[CDATA[Baker street 5]</street>\n"+ "\"address\","+
"<NothingHere/>\n"+ "{"+
"<TrueValue>true</TrueValue>\n"+ "\"addrType\":\"my address\""+
"<FalseValue>false</FalseValue>\n"+ "},"+
"<NullValue>null</NullValue>\n"+ "["+
"<PositiveValue>42</PositiveValue>\n"+ "\"name\","+
"<NegativeValue>-23</NegativeValue>\n"+ "{"+
"<DoubleValue>-23.45</DoubleValue>\n"+ "\"nameType\":\"my name\""+
"<Nan>-23x.45</Nan>\n"+ "},"+
"<ArrayOfNum>\n"+ "\"Joe Tester\""+
"<value>1</value>\n"+ "],"+
"<value>2</value>\n"+ "[\"street\",\"Baker street 5\"],"+
"<value>3</value>\n"+ "["+
"<value>4.1</value>\n"+ "\"NothingHere\","+
"<value>5.2</value>\n"+ "{\"except\":\"an attribute\"}"+
"</ArrayOfNum>\n"+ "],"+
"</address>\n"+
"</addresses>";
String expectedStr =
"[\"addresses\","+
"{\"xsi:noNamespaceSchemaLocation\":\"test.xsd\","+
"\"xmlns:xsi\":\"http://www.w3.org/2001/XMLSchema-instance\"},"+
"[\"address\","+
"[\"name\",\"Joe Tester\"],"+
"[\"street\",\"[CDATA[Baker street 5]\"],"+
"[\"NothingHere\"],"+
"[\"TrueValue\",true],"+ "[\"TrueValue\",true],"+
"[\"FalseValue\",false],"+ "[\"FalseValue\",false],"+
"[\"NullValue\",null],"+ "[\"NullValue\",null],"+
@ -291,25 +459,53 @@ public class JSONMLTest {
"[\"NegativeValue\",-23],"+ "[\"NegativeValue\",-23],"+
"[\"DoubleValue\",-23.45],"+ "[\"DoubleValue\",-23.45],"+
"[\"Nan\",\"-23x.45\"],"+ "[\"Nan\",\"-23x.45\"],"+
"[\"ArrayOfNum\","+ "["+
"\"ArrayOfNum\","+
"[\"value\",1],"+ "[\"value\",1],"+
"[\"value\",2],"+ "[\"value\",2],"+
"[\"value\","+
"["+
"\"subValue\","+
"{\"svAttr\":\"svValue\"},"+
"\"abc\""+
"],"+
"],"+
"[\"value\",3],"+ "[\"value\",3],"+
"[\"value\",4.1],"+ "[\"value\",4.1],"+
"[\"value\",5.2]"+ "[\"value\",5.2]"+
"]"+ "]"+
"]"+ "]"+
"]"; "]";
// make a JSONObject and make sure it looks as expected
JSONObject jsonObject = JSONML.toJSONObject(xmlStr);
JSONObject expectedJsonObject = new JSONObject(expectedJSONObjectStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
// restore the XML, then make another JSONObject and make sure it
// looks as expected
String jsonObjectXmlToStr = JSONML.toString(jsonObject);
JSONObject finalJsonObject = JSONML.toJSONObject(jsonObjectXmlToStr);
Util.compareActualVsExpectedJsonObjects(finalJsonObject, expectedJsonObject);
// create a JSON array from the original string and make sure it
// looks as expected
JSONArray jsonArray = JSONML.toJSONArray(xmlStr); JSONArray jsonArray = JSONML.toJSONArray(xmlStr);
JSONArray expectedJsonArray = new JSONArray(expectedStr); JSONArray expectedJsonArray = new JSONArray(expectedJSONArrayStr);
String xmlToStr = JSONML.toString(jsonArray); Util.compareActualVsExpectedJsonArrays(jsonArray,expectedJsonArray);
JSONArray finalJsonArray = JSONML.toJSONArray(xmlToStr);
Util.compareActualVsExpectedJsonArrays(jsonArray, expectedJsonArray); // restore the XML, then make another JSONArray and make sure it
// TODO: this test fails because JSONML.toString() does not emit values // looks as expected
// for true, false, null, and numbers String jsonArrayXmlToStr = JSONML.toString(jsonArray);
// Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray); JSONArray finalJsonArray = JSONML.toJSONArray(jsonArrayXmlToStr);
Util.compareActualVsExpectedJsonArrays(finalJsonArray, expectedJsonArray);
// lastly, confirm the restored JSONObject XML and JSONArray XML look
// reasonably similar
Util.compareXML(jsonObjectXmlToStr, jsonArrayXmlToStr);
} }
@Test @Test
public void commentsInXML() { public void commentsInXML() {