1
1
package org .json .junit ;
2
2
3
3
import static org .junit .Assert .assertEquals ;
4
+ import static org .junit .Assert .assertNotEquals ;
4
5
import static org .junit .Assert .assertTrue ;
5
6
6
7
import java .io .IOException ;
7
8
8
9
import org .json .JSONArray ;
9
10
import org .json .JSONException ;
11
+ import org .json .JSONML ;
10
12
import org .json .JSONObject ;
11
13
import org .json .XML ;
12
14
import org .junit .Rule ;
@@ -723,4 +725,46 @@ private void compareFileToJSONObject(String xmlStr, String expectedStr) {
723
725
}
724
726
*/
725
727
}
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
+
726
770
}
0 commit comments