|
840 | 840 | static NSMutableCharacterSet *escapeSet; |
841 | 841 |
|
842 | 842 | static inline void |
843 | | -writeTabs(NSMutableString *output, NSInteger tabs, NSUInteger opt) |
| 843 | +writeTabs(NSMutableString *output, NSInteger tabs, NSJSONWritingOptions opt) |
844 | 844 | { |
845 | 845 | if (opt & NSJSONWritingPrettyPrinted) |
846 | 846 | { |
847 | 847 | NSInteger i; |
848 | 848 |
|
849 | | - for (i = 0 ; i < tabs ; i++) |
| 849 | + switch (opt & GSJSONWritingIndentMask) |
850 | 850 | { |
851 | | -// [output appendString: @"\t"]; |
852 | | - [output appendString: @" "]; // One tabstop is two spaces |
| 851 | + case GSJSONWritingIndentOneSpace: |
| 852 | + for (i = 0 ; i < tabs ; i++) |
| 853 | + { |
| 854 | + [output appendString: @" "]; |
| 855 | + } |
| 856 | + break; |
| 857 | + case GSJSONWritingIndentTwoSpaces: |
| 858 | + for (i = 0 ; i < tabs ; i++) |
| 859 | + { |
| 860 | + [output appendString: @" "]; |
| 861 | + } |
| 862 | + break; |
| 863 | + case GSJSONWritingIndentFourSpaces: |
| 864 | + for (i = 0 ; i < tabs ; i++) |
| 865 | + { |
| 866 | + [output appendString: @" "]; |
| 867 | + } |
| 868 | + break; |
| 869 | + case GSJSONWritingIndentUsingTab: |
| 870 | + for (i = 0 ; i < tabs ; i++) |
| 871 | + { |
| 872 | + [output appendString: @"\t"]; |
| 873 | + } |
| 874 | + break; |
853 | 875 | } |
854 | 876 | } |
855 | 877 | } |
856 | 878 |
|
857 | 879 | static inline void |
858 | | -writeNewline(NSMutableString *output, NSInteger tabs) |
| 880 | +writeNewline(NSMutableString *output, NSInteger tabs, NSJSONWritingOptions opt) |
859 | 881 | { |
860 | | - if (tabs >= 0) |
| 882 | + if (opt & NSJSONWritingPrettyPrinted) |
861 | 883 | { |
862 | | - [output appendString: @"\n"]; |
| 884 | + if (tabs >= 0) |
| 885 | + { |
| 886 | + [output appendString: @"\n"]; |
| 887 | + } |
863 | 888 | } |
864 | 889 | } |
865 | 890 |
|
|
870 | 895 | { |
871 | 896 | BOOL writeComma = NO; |
872 | 897 | [output appendString: @"["]; |
| 898 | + tabs++; |
873 | 899 | FOR_IN(id, o, obj) |
874 | 900 | if (writeComma) |
875 | 901 | { |
876 | 902 | [output appendString: @","]; |
877 | 903 | } |
878 | 904 | writeComma = YES; |
879 | | - writeNewline(output, tabs); |
| 905 | + writeNewline(output, tabs, opt); |
880 | 906 | writeTabs(output, tabs, opt); |
881 | | - writeObject(o, output, tabs + 1, opt); |
| 907 | + writeObject(o, output, tabs, opt); |
882 | 908 | END_FOR_IN(obj) |
883 | | - writeNewline(output, tabs); |
| 909 | + tabs--; |
| 910 | + writeNewline(output, tabs, opt); |
884 | 911 | writeTabs(output, tabs, opt); |
885 | 912 | [output appendString: @"]"]; |
886 | 913 | } |
|
895 | 922 | keys = [keys sortedArrayUsingSelector: @selector(compare:)]; |
896 | 923 | } |
897 | 924 |
|
| 925 | + tabs++; |
898 | 926 | FOR_IN(id, o, keys) |
899 | 927 | // Keys in dictionaries must be strings |
900 | 928 | if (![o isKindOfClass: NSStringClass]) { return NO; } |
|
903 | 931 | [output appendString: @","]; |
904 | 932 | } |
905 | 933 | writeComma = YES; |
906 | | - writeNewline(output, tabs); |
| 934 | + writeNewline(output, tabs, opt); |
907 | 935 | writeTabs(output, tabs, opt); |
908 | | - writeObject(o, output, tabs + 1, opt); |
| 936 | + writeObject(o, output, tabs, opt); |
909 | 937 | [output appendString: @":"]; |
910 | | - writeObject([obj objectForKey: o], output, tabs + 1, opt); |
| 938 | + if (opt & NSJSONWritingPrettyPrinted) |
| 939 | + [output appendString: @" "]; |
| 940 | + writeObject([obj objectForKey: o], output, tabs, opt); |
911 | 941 | END_FOR_IN(keys) |
912 | 942 |
|
913 | | - writeNewline(output, tabs); |
| 943 | + tabs--; |
| 944 | + writeNewline(output, tabs, opt); |
914 | 945 | writeTabs(output, tabs, opt); |
915 | 946 | [output appendString: @"}"]; |
916 | 947 | } |
|
0 commit comments