@@ -752,6 +752,28 @@ public static String toString(final Object object, final String tagName) {
752
752
*/
753
753
public static String toString (final Object object , final String tagName , final XMLParserConfiguration config )
754
754
throws JSONException {
755
+ return toString (object , tagName , config , 0 , 0 );
756
+ }
757
+
758
+ /**
759
+ * Convert a JSONObject into a well-formed, element-normal XML string,
760
+ * either pretty print or single-lined depending on indent factor.
761
+ *
762
+ * @param object
763
+ * A JSONObject.
764
+ * @param tagName
765
+ * The optional name of the enclosing tag.
766
+ * @param config
767
+ * Configuration that can control output to XML.
768
+ * @param indentFactor
769
+ * The number of spaces to add to each level of indentation.
770
+ * @param indent
771
+ * The current ident level in spaces.
772
+ * @return
773
+ * @throws JSONException
774
+ */
775
+ private static String toString (final Object object , final String tagName , final XMLParserConfiguration config , int indentFactor , int indent )
776
+ throws JSONException {
755
777
StringBuilder sb = new StringBuilder ();
756
778
JSONArray ja ;
757
779
JSONObject jo ;
@@ -761,9 +783,14 @@ public static String toString(final Object object, final String tagName, final X
761
783
762
784
// Emit <tagName>
763
785
if (tagName != null ) {
786
+ sb .append (indent (indent ));
764
787
sb .append ('<' );
765
788
sb .append (tagName );
766
789
sb .append ('>' );
790
+ if (indentFactor > 0 ){
791
+ sb .append ("\n " );
792
+ indent += indentFactor ;
793
+ }
767
794
}
768
795
769
796
// Loop thru the keys.
@@ -806,31 +833,39 @@ public static String toString(final Object object, final String tagName, final X
806
833
sb .append ('<' );
807
834
sb .append (key );
808
835
sb .append ('>' );
809
- sb .append (toString (val , null , config ));
836
+ sb .append (toString (val , null , config , indentFactor , indent ));
810
837
sb .append ("</" );
811
838
sb .append (key );
812
839
sb .append ('>' );
813
840
} else {
814
- sb .append (toString (val , key , config ));
841
+ sb .append (toString (val , key , config , indentFactor , indent ));
815
842
}
816
843
}
817
844
} else if ("" .equals (value )) {
845
+ sb .append (indent (indent ));
818
846
sb .append ('<' );
819
847
sb .append (key );
820
848
sb .append ("/>" );
849
+ if (indentFactor > 0 ){
850
+ sb .append ("\n " );
851
+ }
821
852
822
853
// Emit a new tag <k>
823
854
824
855
} else {
825
- sb .append (toString (value , key , config ));
856
+ sb .append (toString (value , key , config , indentFactor , indent ));
826
857
}
827
858
}
828
859
if (tagName != null ) {
829
860
830
861
// Emit the </tagName> close tag
862
+ sb .append (indent (indent - indentFactor ));
831
863
sb .append ("</" );
832
864
sb .append (tagName );
833
865
sb .append ('>' );
866
+ if (indentFactor > 0 ){
867
+ sb .append ("\n " );
868
+ }
834
869
}
835
870
return sb .toString ();
836
871
@@ -849,15 +884,85 @@ public static String toString(final Object object, final String tagName, final X
849
884
// XML does not have good support for arrays. If an array
850
885
// appears in a place where XML is lacking, synthesize an
851
886
// <array> element.
852
- sb .append (toString (val , tagName == null ? "array" : tagName , config ));
887
+ sb .append (toString (val , tagName == null ? "array" : tagName , config , indentFactor , indent ));
853
888
}
854
889
return sb .toString ();
855
890
}
856
891
892
+
857
893
string = (object == null ) ? "null" : escape (object .toString ());
858
- return (tagName == null ) ? "\" " + string + "\" "
859
- : (string .length () == 0 ) ? "<" + tagName + "/>" : "<" + tagName
860
- + ">" + string + "</" + tagName + ">" ;
861
894
895
+ if (tagName == null ){
896
+ return indent (indent ) + "\" " + string + "\" " + ((indentFactor > 0 ) ? "\n " : "" );
897
+ } else if (string .length () == 0 ){
898
+ return indent (indent ) + "<" + tagName + "/>" + ((indentFactor > 0 ) ? "\n " : "" );
899
+ } else {
900
+ return indent (indent ) + "<" + tagName
901
+ + ">" + string + "</" + tagName + ">" + ((indentFactor > 0 ) ? "\n " : "" );
902
+ }
903
+ }
904
+
905
+ /**
906
+ * Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
907
+ *
908
+ * @param object
909
+ * A JSONObject.
910
+ * @param indentFactor
911
+ * The number of spaces to add to each level of indentation.
912
+ * @return A string.
913
+ * @throws JSONException Thrown if there is an error parsing the string
914
+ */
915
+ public static String toString (Object object , int indentFactor ){
916
+ return toString (object , null , XMLParserConfiguration .ORIGINAL , indentFactor );
917
+ }
918
+
919
+ /**
920
+ * Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
921
+ *
922
+ * @param object
923
+ * A JSONObject.
924
+ * @param tagName
925
+ * The optional name of the enclosing tag.
926
+ * @param indentFactor
927
+ * The number of spaces to add to each level of indentation.
928
+ * @return A string.
929
+ * @throws JSONException Thrown if there is an error parsing the string
930
+ */
931
+ public static String toString (final Object object , final String tagName , int indentFactor ) {
932
+ return toString (object , tagName , XMLParserConfiguration .ORIGINAL , indentFactor );
933
+ }
934
+
935
+ /**
936
+ * Convert a JSONObject into a well-formed, pretty printed element-normal XML string.
937
+ *
938
+ * @param object
939
+ * A JSONObject.
940
+ * @param tagName
941
+ * The optional name of the enclosing tag.
942
+ * @param config
943
+ * Configuration that can control output to XML.
944
+ * @param indentFactor
945
+ * The number of spaces to add to each level of indentation.
946
+ * @return A string.
947
+ * @throws JSONException Thrown if there is an error parsing the string
948
+ */
949
+ public static String toString (final Object object , final String tagName , final XMLParserConfiguration config , int indentFactor )
950
+ throws JSONException {
951
+ return toString (object , tagName , config , indentFactor , 0 );
952
+ }
953
+
954
+ /**
955
+ * Return a String consisting of a number of space characters specified by indent
956
+ *
957
+ * @param indent
958
+ * The number of spaces to be appended to the String.
959
+ * @return
960
+ */
961
+ private static final String indent (int indent ) {
962
+ StringBuilder sb = new StringBuilder ();
963
+ for (int i = 0 ; i < indent ; i ++) {
964
+ sb .append (' ' );
965
+ }
966
+ return sb .toString ();
862
967
}
863
968
}
0 commit comments