@@ -385,6 +385,74 @@ public void shouldHandleArraytoString() {
385385 assertTrue ("Should handle expectedFinal: [" +expectedStr +"] final: [" +
386386 finalStr +"]" , expectedFinalStr .equals (finalStr ));
387387 }
388+
389+ /**
390+ * Tests that the XML output for empty arrays is consistent.
391+ */
392+ @ Test
393+ public void shouldHandleEmptyArray (){
394+ final JSONObject jo1 = new JSONObject ();
395+ jo1 .put ("array" ,new Object []{});
396+ final JSONObject jo2 = new JSONObject ();
397+ jo2 .put ("array" ,new JSONArray ());
398+
399+ final String expected = "<jo></jo>" ;
400+ String output1 = XML .toString (jo1 ,"jo" );
401+ assertTrue ("Expected an empty root tag" , expected .equals (output1 ));
402+ String output2 = XML .toString (jo2 ,"jo" );
403+ assertTrue ("Expected an empty root tag" , expected .equals (output2 ));
404+ }
405+
406+ /**
407+ * Tests that the XML output for arrays is consistent when an internal array is empty.
408+ */
409+ @ Test
410+ public void shouldHandleEmptyMultiArray (){
411+ final JSONObject jo1 = new JSONObject ();
412+ jo1 .put ("arr" ,new Object []{"One" , new String []{}, "Four" });
413+ final JSONObject jo2 = new JSONObject ();
414+ jo2 .put ("arr" ,new JSONArray (new Object []{"One" , new JSONArray (new String []{}), "Four" }));
415+
416+ final String expected = "<jo><arr>One</arr><arr></arr><arr>Four</arr></jo>" ;
417+ String output1 = XML .toString (jo1 ,"jo" );
418+ assertTrue ("Expected a matching array" , expected .equals (output1 ));
419+ String output2 = XML .toString (jo2 ,"jo" );
420+ assertTrue ("Expected a matching array" , expected .equals (output2 ));
421+ }
422+
423+ /**
424+ * Tests that the XML output for arrays is consistent when arrays are not empty.
425+ */
426+ @ Test
427+ public void shouldHandleNonEmptyArray (){
428+ final JSONObject jo1 = new JSONObject ();
429+ jo1 .put ("arr" ,new String []{"One" , "Two" , "Three" });
430+ final JSONObject jo2 = new JSONObject ();
431+ jo2 .put ("arr" ,new JSONArray (new String []{"One" , "Two" , "Three" }));
432+
433+ final String expected = "<jo><arr>One</arr><arr>Two</arr><arr>Three</arr></jo>" ;
434+ String output1 = XML .toString (jo1 ,"jo" );
435+ assertTrue ("Expected a non empty root tag" , expected .equals (output1 ));
436+ String output2 = XML .toString (jo2 ,"jo" );
437+ assertTrue ("Expected a non empty root tag" , expected .equals (output2 ));
438+ }
439+
440+ /**
441+ * Tests that the XML output for arrays is consistent when arrays are not empty and contain internal arrays.
442+ */
443+ @ Test
444+ public void shouldHandleMultiArray (){
445+ final JSONObject jo1 = new JSONObject ();
446+ jo1 .put ("arr" ,new Object []{"One" , new String []{"Two" , "Three" }, "Four" });
447+ final JSONObject jo2 = new JSONObject ();
448+ jo2 .put ("arr" ,new JSONArray (new Object []{"One" , new JSONArray (new String []{"Two" , "Three" }), "Four" }));
449+
450+ final String expected = "<jo><arr>One</arr><arr><array>Two</array><array>Three</array></arr><arr>Four</arr></jo>" ;
451+ String output1 = XML .toString (jo1 ,"jo" );
452+ assertTrue ("Expected a matching array" , expected .equals (output1 ));
453+ String output2 = XML .toString (jo2 ,"jo" );
454+ assertTrue ("Expected a matching array" , expected .equals (output2 ));
455+ }
388456
389457 /**
390458 * Converting a JSON doc containing a named array of nested arrays to
@@ -425,7 +493,7 @@ public void shouldHandleIllegalJSONNodeNames()
425493
426494 String result = XML .toString (inputJSON );
427495
428- /**
496+ /*
429497 * This is invalid XML. Names should not begin with digits or contain
430498 * certain values, including '@'. One possible solution is to replace
431499 * illegal chars with '_', in which case the expected output would be:
@@ -460,7 +528,7 @@ public void shouldHandleNullNodeValue()
460528 */
461529 @ Test
462530 public void contentOperations () {
463- /**
531+ /*
464532 * When a standalone <!CDATA[...]] structure is found while parsing XML into a
465533 * JSONObject, the contents are placed in a string value with key="content".
466534 */
@@ -483,7 +551,7 @@ public void contentOperations() {
483551 assertTrue ("2. content array entry 0" , "if (a < b && a > 0) then return" .equals (jsonArray .get (0 )));
484552 assertTrue ("2. content array entry 1" , "here is another cdata" .equals (jsonArray .get (1 )));
485553
486- /**
554+ /*
487555 * text content is accumulated in a "content" inside a local JSONObject.
488556 * If there is only one instance, it is saved in the context (a different JSONObject
489557 * from the calling code. and the content element is discarded.
@@ -493,7 +561,7 @@ public void contentOperations() {
493561 assertTrue ("3. 2 items" , 1 == jsonObject .length ());
494562 assertTrue ("3. value tag1" , "value 1" .equals (jsonObject .get ("tag1" )));
495563
496- /**
564+ /*
497565 * array-style text content (multiple tags with the same name) is
498566 * accumulated in a local JSONObject with key="content" and value=JSONArray,
499567 * saved in the context, and then the local JSONObject is discarded.
@@ -508,7 +576,7 @@ public void contentOperations() {
508576 assertTrue ("4. content array entry 1" , jsonArray .getInt (1 ) == 2 );
509577 assertTrue ("4. content array entry 2" , jsonArray .getBoolean (2 ) == true );
510578
511- /**
579+ /*
512580 * Complex content is accumulated in a "content" field. For example, an element
513581 * may contain a mix of child elements and text. Each text segment is
514582 * accumulated to content.
@@ -526,7 +594,7 @@ public void contentOperations() {
526594 assertTrue ("5. content array entry 0" , "val1" .equals (jsonArray .get (0 )));
527595 assertTrue ("5. content array entry 1" , "val2" .equals (jsonArray .get (1 )));
528596
529- /**
597+ /*
530598 * If there is only 1 complex text content, then it is accumulated in a
531599 * "content" field as a string.
532600 */
@@ -538,7 +606,7 @@ public void contentOperations() {
538606 assertTrue ("6. contained content found" , "val1" .equals (jsonObject .get ("content" )));
539607 assertTrue ("6. contained tag2" , "" .equals (jsonObject .get ("tag2" )));
540608
541- /**
609+ /*
542610 * In this corner case, the content sibling happens to have key=content
543611 * We end up with an array within an array, and no content element.
544612 * This is probably a bug.
@@ -555,7 +623,7 @@ public void contentOperations() {
555623 assertTrue ("7. inner array item 0" , "val1" .equals (jsonArray .get (0 )));
556624 assertTrue ("7. inner array item 1" , "" .equals (jsonArray .get (1 )));
557625
558- /**
626+ /*
559627 * Confirm behavior of original issue
560628 */
561629 String jsonStr =
@@ -581,7 +649,7 @@ public void contentOperations() {
581649 "}" ;
582650 jsonObject = new JSONObject (jsonStr );
583651 xmlStr = XML .toString (jsonObject );
584- /**
652+ /*
585653 * This is the created XML. Looks like content was mistaken for
586654 * complex (child node + text) XML.
587655 * <Profile>
@@ -600,7 +668,7 @@ public void contentOperations() {
600668
601669 /**
602670 * Convenience method, given an input string and expected result,
603- * convert to JSONBObject and compare actual to expected result.
671+ * convert to JSONObject and compare actual to expected result.
604672 * @param xmlStr the string to parse
605673 * @param expectedStr the expected JSON string
606674 */
@@ -612,7 +680,7 @@ private void compareStringToJSONObject(String xmlStr, String expectedStr) {
612680
613681 /**
614682 * Convenience method, given an input string and expected result,
615- * convert to JSONBObject via reader and compare actual to expected result.
683+ * convert to JSONObject via reader and compare actual to expected result.
616684 * @param xmlStr the string to parse
617685 * @param expectedStr the expected JSON string
618686 */
@@ -628,11 +696,14 @@ private void compareReaderToJSONObject(String xmlStr, String expectedStr) {
628696 }
629697
630698 /**
631- * Convenience method, given an input string and expected result,
632- * convert to JSONBObject via file and compare actual to expected result.
633- * @param xmlStr the string to parse
634- * @param expectedStr the expected JSON string
635- * @throws IOException
699+ * Convenience method, given an input string and expected result, convert to
700+ * JSONObject via file and compare actual to expected result.
701+ *
702+ * @param xmlStr
703+ * the string to parse
704+ * @param expectedStr
705+ * the expected JSON string
706+ * @throws IOException
636707 */
637708 private void compareFileToJSONObject (String xmlStr , String expectedStr ) {
638709 /*
0 commit comments