@@ -752,6 +752,11 @@ public static String toString(final Object object, final String tagName) {
752752 */
753753 public static String toString (final Object object , final String tagName , final XMLParserConfiguration config )
754754 throws JSONException {
755+ return toString (object , tagName , config , 0 , 0 );
756+ }
757+
758+ private static String toString (final Object object , final String tagName , final XMLParserConfiguration config , int indentFactor , int indent )
759+ throws JSONException {
755760 StringBuilder sb = new StringBuilder ();
756761 JSONArray ja ;
757762 JSONObject jo ;
@@ -761,9 +766,14 @@ public static String toString(final Object object, final String tagName, final X
761766
762767 // Emit <tagName>
763768 if (tagName != null ) {
769+ sb .append (indent (indent ));
764770 sb .append ('<' );
765771 sb .append (tagName );
766772 sb .append ('>' );
773+ if (indentFactor > 0 ){
774+ sb .append ("\n " );
775+ indent += indentFactor ;
776+ }
767777 }
768778
769779 // Loop thru the keys.
@@ -806,31 +816,39 @@ public static String toString(final Object object, final String tagName, final X
806816 sb .append ('<' );
807817 sb .append (key );
808818 sb .append ('>' );
809- sb .append (toString (val , null , config ));
819+ sb .append (toString (val , null , config , indentFactor , indent ));
810820 sb .append ("</" );
811821 sb .append (key );
812822 sb .append ('>' );
813823 } else {
814- sb .append (toString (val , key , config ));
824+ sb .append (toString (val , key , config , indentFactor , indent ));
815825 }
816826 }
817827 } else if ("" .equals (value )) {
828+ sb .append (indent (indent ));
818829 sb .append ('<' );
819830 sb .append (key );
820831 sb .append ("/>" );
832+ if (indentFactor > 0 ){
833+ sb .append ("\n " );
834+ }
821835
822836 // Emit a new tag <k>
823837
824838 } else {
825- sb .append (toString (value , key , config ));
839+ sb .append (toString (value , key , config , indentFactor , indent ));
826840 }
827841 }
828842 if (tagName != null ) {
829843
830844 // Emit the </tagName> close tag
845+ sb .append (indent (indent - indentFactor ));
831846 sb .append ("</" );
832847 sb .append (tagName );
833848 sb .append ('>' );
849+ if (indentFactor > 0 ){
850+ sb .append ("\n " );
851+ }
834852 }
835853 return sb .toString ();
836854
@@ -849,15 +867,78 @@ public static String toString(final Object object, final String tagName, final X
849867 // XML does not have good support for arrays. If an array
850868 // appears in a place where XML is lacking, synthesize an
851869 // <array> element.
852- sb .append (toString (val , tagName == null ? "array" : tagName , config ));
870+ sb .append (toString (val , tagName == null ? "array" : tagName , config , indentFactor , indent ));
853871 }
854872 return sb .toString ();
855873 }
856874
875+
857876 string = (object == null ) ? "null" : escape (object .toString ());
858- return (tagName == null ) ? "\" " + string + "\" "
859- : (string .length () == 0 ) ? "<" + tagName + "/>" : "<" + tagName
860- + ">" + string + "</" + tagName + ">" ;
861877
878+ if (tagName == null ){
879+ return indent (indent ) + "\" " + string + "\" " + ((indentFactor > 0 ) ? "\n " : "" );
880+ } else if (string .length () == 0 ){
881+ return indent (indent ) + "<" + tagName + "/>" + ((indentFactor > 0 ) ? "\n " : "" );
882+ } else {
883+ return indent (indent ) + "<" + tagName
884+ + ">" + string + "</" + tagName + ">" + ((indentFactor > 0 ) ? "\n " : "" );
885+ }
886+ }
887+
888+ /**
889+ * Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
890+ *
891+ * @param object
892+ * A JSONObject.
893+ * @param indentFactor
894+ * The number of spaces to add to each level of indentation.
895+ * @return A string.
896+ * @throws JSONException Thrown if there is an error parsing the string
897+ */
898+ public static String toString (Object object , int indentFactor ){
899+ return toString (object , null , XMLParserConfiguration .ORIGINAL , indentFactor );
900+ }
901+
902+ /**
903+ * Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
904+ *
905+ * @param object
906+ * A JSONObject.
907+ * @param tagName
908+ * The optional name of the enclosing tag.
909+ * @param indentFactor
910+ * The number of spaces to add to each level of indentation.
911+ * @return A string.
912+ * @throws JSONException Thrown if there is an error parsing the string
913+ */
914+ public static String toString (final Object object , final String tagName , int indentFactor ) {
915+ return toString (object , tagName , XMLParserConfiguration .ORIGINAL , indentFactor );
916+ }
917+
918+ /**
919+ * Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
920+ *
921+ * @param object
922+ * A JSONObject.
923+ * @param tagName
924+ * The optional name of the enclosing tag.
925+ * @param config
926+ * Configuration that can control output to XML.
927+ * @param indentFactor
928+ * The number of spaces to add to each level of indentation.
929+ * @return A string.
930+ * @throws JSONException Thrown if there is an error parsing the string
931+ */
932+ public static String toString (final Object object , final String tagName , final XMLParserConfiguration config , int indentFactor )
933+ throws JSONException {
934+ return toString (object , tagName , config , indentFactor , 0 );
935+ }
936+
937+ private static final String indent (int indent ) {
938+ StringBuilder sb = new StringBuilder ();
939+ for (int i = 0 ; i < indent ; i ++) {
940+ sb .append (' ' );
941+ }
942+ return sb .toString ();
862943 }
863944}
0 commit comments