Skip to content

Commit 4af912e

Browse files
committed
Better control over pretty printing
1 parent 00fa153 commit 4af912e

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

Headers/Foundation/NSJSONSerialization.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ enum
5151
* whitespace, producing space-efficient but not very human-friendly JSON.
5252
*/
5353
NSJSONWritingPrettyPrinted = (1UL << 0),
54+
55+
#if OS_API_VERSION(GS_API_NONE,GS_API_LATEST)
56+
/** Additional modified for indentation control when pretty-printing
57+
*/
58+
GSJSONWritingIndentOneSpace = (0UL << 24),
59+
GSJSONWritingIndentTwoSpaces = (1UL << 24),
60+
GSJSONWritingIndentFourSpaces = (2UL << 24),
61+
GSJSONWritingIndentUsingTab = (3UL << 24),
62+
GSJSONWritingIndentMask = (3UL << 24),
63+
#endif
64+
5465
#if OS_API_VERSION(MAC_OS_X_VERSION_10_13, GS_API_LATEST)
5566
/**
5667
* When writing JSON, sort keys in lexicographic order.

Source/NSJSONSerialization.m

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -840,26 +840,51 @@
840840
static NSMutableCharacterSet *escapeSet;
841841

842842
static inline void
843-
writeTabs(NSMutableString *output, NSInteger tabs, NSUInteger opt)
843+
writeTabs(NSMutableString *output, NSInteger tabs, NSJSONWritingOptions opt)
844844
{
845845
if (opt & NSJSONWritingPrettyPrinted)
846846
{
847847
NSInteger i;
848848

849-
for (i = 0 ; i < tabs ; i++)
849+
switch (opt & GSJSONWritingIndentMask)
850850
{
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;
853875
}
854876
}
855877
}
856878

857879
static inline void
858-
writeNewline(NSMutableString *output, NSInteger tabs)
880+
writeNewline(NSMutableString *output, NSInteger tabs, NSJSONWritingOptions opt)
859881
{
860-
if (tabs >= 0)
882+
if (opt & NSJSONWritingPrettyPrinted)
861883
{
862-
[output appendString: @"\n"];
884+
if (tabs >= 0)
885+
{
886+
[output appendString: @"\n"];
887+
}
863888
}
864889
}
865890

@@ -870,17 +895,19 @@
870895
{
871896
BOOL writeComma = NO;
872897
[output appendString: @"["];
898+
tabs++;
873899
FOR_IN(id, o, obj)
874900
if (writeComma)
875901
{
876902
[output appendString: @","];
877903
}
878904
writeComma = YES;
879-
writeNewline(output, tabs);
905+
writeNewline(output, tabs, opt);
880906
writeTabs(output, tabs, opt);
881-
writeObject(o, output, tabs + 1, opt);
907+
writeObject(o, output, tabs, opt);
882908
END_FOR_IN(obj)
883-
writeNewline(output, tabs);
909+
tabs--;
910+
writeNewline(output, tabs, opt);
884911
writeTabs(output, tabs, opt);
885912
[output appendString: @"]"];
886913
}
@@ -895,6 +922,7 @@
895922
keys = [keys sortedArrayUsingSelector: @selector(compare:)];
896923
}
897924

925+
tabs++;
898926
FOR_IN(id, o, keys)
899927
// Keys in dictionaries must be strings
900928
if (![o isKindOfClass: NSStringClass]) { return NO; }
@@ -903,14 +931,17 @@
903931
[output appendString: @","];
904932
}
905933
writeComma = YES;
906-
writeNewline(output, tabs);
934+
writeNewline(output, tabs, opt);
907935
writeTabs(output, tabs, opt);
908-
writeObject(o, output, tabs + 1, opt);
936+
writeObject(o, output, tabs, opt);
909937
[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);
911941
END_FOR_IN(keys)
912942

913-
writeNewline(output, tabs);
943+
tabs--;
944+
writeNewline(output, tabs, opt);
914945
writeTabs(output, tabs, opt);
915946
[output appendString: @"}"];
916947
}

0 commit comments

Comments
 (0)