assertEquals("<list of lists><array>1</array><array>2</array><array>3</array></list of lists><list of lists><array>4</array><array>5</array><array>6</array></list of lists>",
XML.toString(jsonobject));
string = "<recipe name=\"bread\" prep_time=\"5 mins\" cook_time=\"3 hours\"> <title>Basic bread</title> <ingredient amount=\"8\" unit=\"dL\">Flour</ingredient> <ingredient amount=\"10\" unit=\"grams\">Yeast</ingredient> <ingredient amount=\"4\" unit=\"dL\" state=\"warm\">Water</ingredient> <ingredient amount=\"1\" unit=\"teaspoon\">Salt</ingredient> <instructions> <step>Mix all ingredients together.</step> <step>Knead thoroughly.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Knead again.</step> <step>Place in a bread baking tin.</step> <step>Cover with a cloth, and leave for one hour in warm room.</step> <step>Bake in the oven at 180(degrees)C for 30 minutes.</step> </instructions> </recipe> ";
jsonobject = XML.toJSONObject(string);
assertEquals("{\"recipe\": {\n\"title\": \"Basic bread\",\n\"cook_time\": \"3 hours\",\n\"instructions\": {\"step\": [\n\"Mix all ingredients together.\",\n\"Knead thoroughly.\",\n\"Cover with a cloth, and leave for one hour in warm room.\",\n\"Knead again.\",\n\"Place in a bread baking tin.\",\n\"Cover with a cloth, and leave for one hour in warm room.\",\n\"Bake in the oven at 180(degrees)C for 30 minutes.\"\n ]},\n\"name\": \"bread\",\n\"ingredient\": [\n {\n\"content\": \"Flour\",\n\"amount\": 8,\n\"unit\": \"dL\"\n },\n {\n\"content\": \"Yeast\",\n\"amount\": 10,\n\"unit\": \"grams\"\n },\n {\n\"content\": \"Water\",\n\"amount\": 4,\n\"unit\": \"dL\",\n\"state\": \"warm\"\n },\n {\n\"content\": \"Salt\",\n\"amount\": 1,\n\"unit\": \"teaspoon\"\n }\n ],\n\"prep_time\": \"5 mins\"\n}}",
jsonobject.toString(4));
jsonobject = JSONML.toJSONObject(string);
assertEquals("{\"cook_time\":\"3 hours\",\"name\":\"bread\",\"tagName\":\"recipe\",\"childNodes\":[{\"tagName\":\"title\",\"childNodes\":[\"Basic bread\"]},{\"amount\":8,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"childNodes\":[\"Flour\"]},{\"amount\":10,\"unit\":\"grams\",\"tagName\":\"ingredient\",\"childNodes\":[\"Yeast\"]},{\"amount\":4,\"unit\":\"dL\",\"tagName\":\"ingredient\",\"state\":\"warm\",\"childNodes\":[\"Water\"]},{\"amount\":1,\"unit\":\"teaspoon\",\"tagName\":\"ingredient\",\"childNodes\":[\"Salt\"]},{\"tagName\":\"instructions\",\"childNodes\":[{\"tagName\":\"step\",\"childNodes\":[\"Mix all ingredients together.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead thoroughly.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Knead again.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Place in a bread baking tin.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Cover with a cloth, and leave for one hour in warm room.\"]},{\"tagName\":\"step\",\"childNodes\":[\"Bake in the oven at 180(degrees)C for 30 minutes.\"]}]}],\"prep_time\":\"5 mins\"}",
jsonobject.toString());
assertEquals("<recipe cook_time=\"3 hours\" name=\"bread\" prep_time=\"5 mins\"><title>Basic bread</title><ingredient amount=\"8\" unit=\"dL\">Flour</ingredient><ingredient amount=\"10\" unit=\"grams\">Yeast</ingredient><ingredient amount=\"4\" unit=\"dL\" state=\"warm\">Water</ingredient><ingredient amount=\"1\" unit=\"teaspoon\">Salt</ingredient><instructions><step>Mix all ingredients together.</step><step>Knead thoroughly.</step><step>Cover with a cloth, and leave for one hour in warm room.</step><step>Knead again.</step><step>Place in a bread baking tin.</step><step>Cover with a cloth, and leave for one hour in warm room.</step><step>Bake in the oven at 180(degrees)C for 30 minutes.</step></instructions></recipe>",
JSONML.toString(jsonobject));
jsonarray = JSONML.toJSONArray(string);
assertEquals("[\n\"recipe\",\n {\n\"cook_time\": \"3 hours\",\n\"name\": \"bread\",\n\"prep_time\": \"5 mins\"\n },\n [\n\"title\",\n\"Basic bread\"\n ],\n [\n\"ingredient\",\n {\n\"amount\": 8,\n\"unit\": \"dL\"\n },\n\"Flour\"\n ],\n [\n\"ingredient\",\n {\n\"amount\": 10,\n\"unit\": \"grams\"\n },\n\"Yeast\"\n ],\n [\n\"ingredient\",\n {\n\"amount\": 4,\n\"unit\": \"dL\",\n\"state\": \"warm\"\n },\n\"Water\"\n ],\n [\n\"ingredient\",\n {\n\"amount\": 1,\n\"unit\": \"teaspoon\"\n },\n\"Salt\"\n ],\n [\n\"instructions\",\n [\n\"step\",\n\"Mix all ingredients together.\"\n ],\n [\n\"step\",\n\"Knead thoroughly.\"\n ],\n [\n\"step\",\n\"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n\"step\",\n\"Knead again.\"\n ],\n [\n\"step\",\n\"Place in a bread baking tin.\"\n ],\n [\n\"step\",\n\"Cover with a cloth, and leave for one hour in warm room.\"\n ],\n [\n\"step\",\n\"Bake in the oven at 180(degrees)C for 30 minutes.\"\n ]\n ]\n]",
jsonarray.toString(4));
assertEquals("<recipe cook_time=\"3 hours\" name=\"bread\" prep_time=\"5 mins\"><title>Basic bread</title><ingredient amount=\"8\" unit=\"dL\">Flour</ingredient><ingredient amount=\"10\" unit=\"grams\">Yeast</ingredient><ingredient amount=\"4\" unit=\"dL\" state=\"warm\">Water</ingredient><ingredient amount=\"1\" unit=\"teaspoon\">Salt</ingredient><instructions><step>Mix all ingredients together.</step><step>Knead thoroughly.</step><step>Cover with a cloth, and leave for one hour in warm room.</step><step>Knead again.</step><step>Place in a bread baking tin.</step><step>Cover with a cloth, and leave for one hour in warm room.</step><step>Bake in the oven at 180(degrees)C for 30 minutes.</step></instructions></recipe>",
JSONML.toString(jsonarray));
string = "<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>";
jsonobject = JSONML.toJSONObject(string);
assertEquals("{\n\"id\": \"demo\",\n\"tagName\": \"div\",\n\"class\": \"JSONML\",\n\"childNodes\": [\n {\n\"tagName\": \"p\",\n\"childNodes\": [\n\"JSONML is a transformation between\",\n {\n\"tagName\": \"b\",\n\"childNodes\": [\"JSON\"]\n },\n\"and\",\n {\n\"tagName\": \"b\",\n\"childNodes\": [\"XML\"]\n },\n\"that preserves ordering of document features.\"\n ]\n },\n {\n\"tagName\": \"p\",\n\"childNodes\": [\"JSONML can work with JSON arrays or JSON objects.\"]\n },\n {\n\"tagName\": \"p\",\n\"childNodes\": [\n\"Three\",\n {\"tagName\": \"br\"},\n\"little\",\n {\"tagName\": \"br\"},\n\"words\"\n ]\n }\n ]\n}",
jsonobject.toString(4));
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>",
JSONML.toString(jsonobject));
jsonarray = JSONML.toJSONArray(string);
assertEquals("[\n\"div\",\n {\n\"id\": \"demo\",\n\"class\": \"JSONML\"\n },\n [\n\"p\",\n\"JSONML is a transformation between\",\n [\n\"b\",\n\"JSON\"\n ],\n\"and\",\n [\n\"b\",\n\"XML\"\n ],\n\"that preserves ordering of document features.\"\n ],\n [\n\"p\",\n\"JSONML can work with JSON arrays or JSON objects.\"\n ],\n [\n\"p\",\n\"Three\",\n [\"br\"],\n\"little\",\n [\"br\"],\n\"words\"\n ]\n]",
jsonarray.toString(4));
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>",
string = "<xml one = 1 two=' \"2\" '><five></five>First \u0009<content><five></five> This is \"content\". <three> 3 </three>JSON does not preserve the sequencing of elements and contents.<three> III </three> <three> T H R E E</three><four/>Content text is an implied structure in XML. <six content=\"6\"/>JSON does not have implied structure:<seven>7</seven>everything is explicit.<![CDATA[CDATA blocks<are><supported>!]]></xml>";
jsonobject = XML.toJSONObject(string);
assertEquals("{\"xml\": {\n\"content\": [\n\"First \\t<content>\",\n\"This is \\\"content\\\".\",\n\"JSON does not preserve the sequencing of elements and contents.\",\n\"Content text is an implied structure in XML.\",\n\"JSON does not have implied structure:\",\n\"everything is explicit.\",\n\"CDATA blocks<are><supported>!\"\n ],\n\"two\": \"\\\"2\\\"\",\n\"seven\": 7,\n\"five\": [\n\"\",\n\"\"\n ],\n\"one\": 1,\n\"three\": [\n 3,\n\"III\",\n\"T H R E E\"\n ],\n\"four\": \"\",\n\"six\": {\"content\": 6}\n}}",
jsonobject.toString(2));
assertEquals("<xml>First \t<content>\n" +
"This is "content".\n" +
"JSON does not preserve the sequencing of elements and contents.\n" +
"Content text is an implied structure in XML.\n" +
"JSON does not have implied structure:\n" +
"everything is explicit.\n" +
"CDATA blocks<are><supported>!<two> "2" </two><seven>7</seven><five/><five/><one>1</one><three>3</three><three>III</three><three>T H R E E</three><four/><six>6</six></xml>",
XML.toString(jsonobject));
ja = JSONML.toJSONArray(string);
assertEquals("[\n\"xml\",\n {\n\"two\": \"\\\"2\\\"\",\n\"one\": 1\n },\n [\"five\"],\n\"First \\t<content>\",\n [\"five\"],\n\"This is \\\"content\\\".\",\n [\n\"three\",\n 3\n ],\n\"JSON does not preserve the sequencing of elements and contents.\",\n [\n\"three\",\n\"III\"\n ],\n [\n\"three\",\n\"T H R E E\"\n ],\n [\"four\"],\n\"Content text is an implied structure in XML.\",\n [\n\"six\",\n {\"content\": 6}\n ],\n\"JSON does not have implied structure:\",\n [\n\"seven\",\n 7\n ],\n\"everything is explicit.\",\n\"CDATA blocks<are><supported>!\"\n]",
ja.toString(4));
assertEquals("<xml two=\" "2" \" one=\"1\"><five/>First \t<content><five/>This is "content".<three></three>JSON does not preserve the sequencing of elements and contents.<three>III</three><three>T H R E E</three><four/>Content text is an implied structure in XML.<six content=\"6\"/>JSON does not have implied structure:<seven></seven>everything is explicit.CDATA blocks<are><supported>!</xml>",
jsonobject = XML.toJSONObject("<?xml version=\"1.0\" ?><Book Author=\"Anonymous\"><Title>Sample Book</Title><Chapter id=\"1\">This is chapter 1. It is not very long or interesting.</Chapter><Chapter id=\"2\">This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.</Chapter></Book>");
assertEquals("{\"Book\": {\n\"Chapter\": [\n {\n\"content\": \"This is chapter 1. It is not very long or interesting.\",\n\"id\": 1\n },\n {\n\"content\": \"This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.\",\n\"id\": 2\n }\n ],\n\"Author\": \"Anonymous\",\n\"Title\": \"Sample Book\"\n}}",
jsonobject.toString(2));
assertEquals("<Book><Chapter>This is chapter 1. It is not very long or interesting.<id>1</id></Chapter><Chapter>This is chapter 2. Although it is longer than chapter 1, it is not any more interesting.<id>2</id></Chapter><Author>Anonymous</Author><Title>Sample Book</Title></Book>",
jsonobject = newJSONObject("{script: 'It is not allowed in HTML to send a close script tag in a string<script>because it confuses browsers</script>so we insert a backslash before the /'}");
assertEquals("{\"script\":\"It is not allowed in HTML to send a close script tag in a string<script>because it confuses browsers<\\/script>so we insert a backslash before the /\"}",
jsonarray = CDL.toJSONArray("Comma delimited list test, '\"Strip\"Quotes', 'quote, comma', No quotes, 'Single Quotes', \"Double Quotes\"\n1,'2',\"3\"\n,'It is \"good,\"', \"It works.\"\n\n");
string = CDL.toString(jsonarray);
assertEquals("\"quote, comma\",\"StripQuotes\",Comma delimited list test\n" +
"3,2,1\n" +
"It works.,\"It is good,\",\n",
string);
assertEquals("[\n {\n\"quote, comma\": \"3\",\n\"\\\"Strip\\\"Quotes\": \"2\",\n\"Comma delimited list test\": \"1\"\n },\n {\n\"quote, comma\": \"It works.\",\n\"\\\"Strip\\\"Quotes\": \"It is \\\"good,\\\"\",\n\"Comma delimited list test\": \"\"\n }\n]",
jsonarray.toString(1));
jsonarray = CDL.toJSONArray(string);
assertEquals("[\n {\n\"quote, comma\": \"3\",\n\"StripQuotes\": \"2\",\n\"Comma delimited list test\": \"1\"\n },\n {\n\"quote, comma\": \"It works.\",\n\"StripQuotes\": \"It is good,\",\n\"Comma delimited list test\": \"\"\n }\n]",
jsonarray.toString(1));
jsonarray = newJSONArray(" [\"<escape>\", next is an implied null , , ok,] ");
assertEquals("[\"<escape>\",\"next is an implied null\",null,\"ok\"]",
jsonarray.toString());
assertEquals("<array><escape></array><array>next is an implied null</array><array>null</array><array>ok</array>",
XML.toString(jsonarray));
jsonobject = newJSONObject("{ fun => with non-standard forms ; forgiving => This package can be used to parse formats that are similar to but not stricting conforming to JSON; why=To make it easier to migrate existing data to JSON,one = [[1.00]]; uno=[[{1=>1}]];'+':+6e66 ;pluses=+++;empty = '' , 'double':0.666,true: TRUE, false: FALSE, null=NULL;[true] = [[!,@;*]]; string=> o. k. ; \r oct=0666; hex=0x666; dec=666; o=0999; noh=0x0x}");
assertEquals("{\n\"noh\": \"0x0x\",\n\"one\": [[1]],\n\"o\": 999,\n\"+\": 6.0E66,\n\"true\": true,\n\"forgiving\": \"This package can be used to parse formats that are similar to but not stricting conforming to JSON\",\n\"fun\": \"with non-standard forms\",\n\"double\": 0.666,\n\"uno\": [[{\"1\": 1}]],\n\"dec\": 666,\n\"oct\": 666,\n\"hex\": 1638,\n\"string\": \"o. k.\",\n\"empty\": \"\",\n\"false\": false,\n\"[true]\": [[\n\"!\",\n\"@\",\n\"*\"\n ]],\n\"pluses\": \"+++\",\n\"why\": \"To make it easier to migrate existing data to JSON\",\n\"null\": null\n}", jsonobject.toString(1));
string = "<book><chapter>Content of the first chapter</chapter><chapter>Content of the second chapter <chapter>Content of the first subchapter</chapter> <chapter>Content of the second subchapter</chapter></chapter><chapter>Third Chapter</chapter></book>";
jsonobject = XML.toJSONObject(string);
assertEquals("{\"book\": {\"chapter\": [\n\"Content of the first chapter\",\n {\n\"content\": \"Content of the second chapter\",\n\"chapter\": [\n\"Content of the first subchapter\",\n\"Content of the second subchapter\"\n ]\n },\n\"Third Chapter\"\n]}}", jsonobject.toString(1));
assertEquals("<book><chapter>Content of the first chapter</chapter><chapter>Content of the second chapter<chapter>Content of the first subchapter</chapter><chapter>Content of the second subchapter</chapter></chapter><chapter>Third Chapter</chapter></book>",
XML.toString(jsonobject));
jsonarray = JSONML.toJSONArray(string);
assertEquals("[\n" +
" \"book\",\n" +
" [\n" +
" \"chapter\",\n" +
" \"Content of the first chapter\"\n" +
" ],\n" +
" [\n" +
" \"chapter\",\n" +
" \"Content of the second chapter\",\n" +
" [\n" +
" \"chapter\",\n" +
" \"Content of the first subchapter\"\n" +
" ],\n" +
" [\n" +
" \"chapter\",\n" +
" \"Content of the second subchapter\"\n" +
" ]\n" +
" ],\n" +
" [\n" +
" \"chapter\",\n" +
" \"Third Chapter\"\n" +
" ]\n" +
"]", jsonarray.toString(4));
assertEquals("<book><chapter>Content of the first chapter</chapter><chapter>Content of the second chapter<chapter>Content of the first subchapter</chapter><chapter>Content of the second subchapter</chapter></chapter><chapter>Third Chapter</chapter></book>",
assertEquals("{\"AnimalColors\":{\"worm\":\"pink\",\"lamb\":\"black\",\"pig\":\"pink\"},\"plist\":\"Apple\",\"AnimalSounds\":{\"worm\":\"baa\",\"Lisa\":\"Why is the worm talking like a lamb?\",\"lamb\":\"baa\",\"pig\":\"oink\"},\"AnimalSmells\":{\"worm\":\"wormy\",\"lamb\":\"lambish\",\"pig\":\"piggish\"}}",
string = "<a ichi='1' ni='2'><b>The content of b</b> and <c san='3'>The content of c</c><d>do</d><e></e><d>re</d><f/><d>mi</d></a>";
jsonobject = XML.toJSONObject(string);
assertEquals("{\"a\":{\"f\":\"\",\"content\":\"and\",\"d\":[\"do\",\"re\",\"mi\"],\"ichi\":1,\"e\":\"\",\"b\":\"The content of b\",\"c\":{\"content\":\"The content of c\",\"san\":3},\"ni\":2}}",
jsonobject.toString());
assertEquals("<a><f/>and<d>do</d><d>re</d><d>mi</d><ichi>1</ichi><e/><b>The content of b</b><c>The content of c<san>3</san></c><ni>2</ni></a>",
XML.toString(jsonobject));
ja = JSONML.toJSONArray(string);
assertEquals("[\n" +
" \"a\",\n" +
" {\n" +
" \"ichi\": 1,\n" +
" \"ni\": 2\n" +
" },\n" +
" [\n" +
" \"b\",\n" +
" \"The content of b\"\n" +
" ],\n" +
" \"and\",\n" +
" [\n" +
" \"c\",\n" +
" {\"san\": 3},\n" +
" \"The content of c\"\n" +
" ],\n" +
" [\n" +
" \"d\",\n" +
" \"do\"\n" +
" ],\n" +
" [\"e\"],\n" +
" [\n" +
" \"d\",\n" +
" \"re\"\n" +
" ],\n" +
" [\"f\"],\n" +
" [\n" +
" \"d\",\n" +
" \"mi\"\n" +
" ]\n" +
"]", ja.toString(4));
assertEquals("<a ichi=\"1\" ni=\"2\"><b>The content of b</b>and<c san=\"3\">The content of c</c><d>do</d><e/><d>re</d><f/><d>mi</d></a>",