11package org .json .junit ;
22
33import static org .junit .Assert .assertEquals ;
4+ import static org .junit .Assert .assertNotEquals ;
45import static org .junit .Assert .assertTrue ;
56
67import java .io .IOException ;
78
89import org .json .JSONArray ;
910import org .json .JSONException ;
11+ import org .json .JSONML ;
1012import org .json .JSONObject ;
1113import org .json .XML ;
1214import org .junit .Rule ;
@@ -723,4 +725,46 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) {
723725 }
724726 */
725727 }
728+
729+ /**
730+ * JSON string lost leading zero and converted "True" to true.
731+ */
732+ @ Test
733+ public void testToJSONArray_jsonOutput () {
734+ final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\" 01\" /><title>True</title></root>" ;
735+ final String expectedJsonString = "{\" root\" :{\" item\" :{\" id\" :\" 01\" },\" id\" :[\" 01\" ,1,\" 00\" ,0],\" title\" :true}}" ;
736+ final JSONObject actualJsonOutput = XML .toJSONObject (originalXml , false );
737+
738+ assertEquals (expectedJsonString , actualJsonOutput .toString ());
739+ }
740+
741+ /**
742+ * JSON string cannot be reverted to original xml.
743+ */
744+ @ Test
745+ public void testToJSONArray_reversibility () {
746+ final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\" 01\" /><title>True</title></root>" ;
747+ final String revertedXml = XML .toString (XML .toJSONObject (originalXml , false ));
748+
749+ assertNotEquals (revertedXml , originalXml );
750+ }
751+
752+ /**
753+ * test passes when using the new method toJsonArray.
754+ */
755+ @ Test
756+ public void testToJsonXML () {
757+ final String originalXml = "<root><id>01</id><id>1</id><id>00</id><id>0</id><item id=\" 01\" /><title>True</title></root>" ;
758+ final String expectedJsonString = "{\" root\" :{\" item\" :{\" id\" :\" 01\" },\" id\" :[\" 01\" ,\" 1\" ,\" 00\" ,\" 0\" ],\" title\" :\" True\" }}" ;
759+
760+ final JSONObject json = XML .toJSONObject (originalXml ,true );
761+ assertEquals (expectedJsonString , json .toString ());
762+
763+ final String reverseXml = XML .toString (json );
764+ // this reversal isn't exactly the same. use JSONML for an exact reversal
765+ final String expectedReverseXml = "<root><item><id>01</id></item><id>01</id><id>1</id><id>00</id><id>0</id><title>True</title></root>" ;
766+
767+ assertEquals (expectedReverseXml , reverseXml );
768+ }
769+
726770}
0 commit comments