File: CSJSONPrinter.m

package info (click to toggle)
unar 1.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,664 kB
  • sloc: ansic: 52,939; objc: 39,563; cpp: 4,074; makefile: 99; perl: 10
file content (272 lines) | stat: -rw-r--r-- 5,322 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#import "CSJSONPrinter.h"
#import "NSStringPrinting.h"

@implementation CSJSONPrinter

-(id)init
{
	if((self=[super init]))
	{
		indentlevel=0;
		indentstring=[@"\n" retain];
		needseparator=NO;
	}
	return self;
}

-(void)dealloc
{
	[indentstring release];
	[super dealloc];
}




-(void)setIndentString:(NSString *)string
{
	[indentstring autorelease];
	indentstring=[string retain];
}

-(void)setASCIIMode:(BOOL)ascii
{
	asciimode=ascii;
}



-(void)printObject:(id)object
{
	if(object==[NSNull null]) [self printNull];
	else if([object isKindOfClass:[NSNumber class]]) [self printNumber:object];
	else if([object isKindOfClass:[NSString class]]) [self printString:object];
	else if([object isKindOfClass:[NSData class]]) [self printData:object];
	else if([object isKindOfClass:[NSValue class]]) [self printValue:object];
	else if([object isKindOfClass:[NSArray class]]) [self printArray:object];
	else if([object isKindOfClass:[NSDictionary class]]) [self printDictionary:object];
	else [self printString:[object description]];

	needseparator=YES;
}

-(void)printNull
{
	[self printSeparatorIfNeeded];
	[@"null" print];
}

-(void)printNumber:(NSNumber *)number
{
	[self printSeparatorIfNeeded];
	if(strcmp([number objCType],"c")==0)
	{
		if([number boolValue]) [@"true" print];
		else [@"false" print];
	}
	else
	{
		[[number description] print];
	}
}

-(void)printString:(NSString *)string
{
	[self printSeparatorIfNeeded];
	[@"\"" print];
	[[self stringByEscapingString:string] print];
	[@"\"" print];
}

-(void)printData:(NSData *)data
{
	[self printSeparatorIfNeeded];
	[@"\"" print];
	[[self stringByEncodingBytes:[data bytes] length:[data length]] print];
	[@"\"" print];
}

-(void)printValue:(NSValue *)value
{
	NSUInteger length;
	NSGetSizeAndAlignment([value objCType],&length,NULL);
	uint8_t bytes[length];
	[value getValue:bytes];

	[self printSeparatorIfNeeded];
	[@"\"" print];
	[[self stringByEncodingBytes:bytes length:length] print];
	[@"\"" print];
}

-(void)printArray:(NSArray *)array
{
	[self startPrintingArray];
	[self printArrayObjects:array];
	[self endPrintingArray];
}

-(void)printDictionary:(NSDictionary *)dictionary
{
	[self startPrintingDictionary];
	[self printDictionaryKeysAndObjects:dictionary];
	[self endPrintingDictionary];
}



-(void)startPrintingArray
{
	[self printSeparatorIfNeeded];
	[@"[" print];
	indentlevel++;
}

-(void)startPrintingArrayObject
{
	[self printSeparatorIfNeeded];
	[self startNewLine];
}

-(void)endPrintingArrayObject
{
	needseparator=YES;
}

-(void)endPrintingArray
{
	needseparator=NO;

	indentlevel--;
	[self startNewLine];
	[@"]" print];
}

-(void)printArrayObject:(id)object
{
	[self startPrintingArrayObject];
	[self printObject:object];
	[self endPrintingArrayObject];
}

-(void)printArrayObjects:(NSArray *)array
{
	NSEnumerator *enumerator=[array objectEnumerator];
	id object;
	while((object=[enumerator nextObject])) [self printArrayObject:object];
}





-(void)startPrintingDictionary
{
	[self printSeparatorIfNeeded];
	[@"{" print];
	indentlevel++;
}

-(void)printDictionaryKey:(id)key
{
	[self printSeparatorIfNeeded];
	[self startNewLine];
	[@"\"" print];
	[[self stringByEscapingString:[key description]] print];
	[@"\": " print];
}

-(void)startPrintingDictionaryObject
{
}

-(void)endPrintingDictionaryObject
{
	needseparator=YES;
}

-(void)endPrintingDictionary
{
	needseparator=NO;

	indentlevel--;
	[self startNewLine];
	[@"}" print];
}

-(void)printDictionaryObject:(id)object
{
	[self startPrintingDictionaryObject];
	[self printObject:object];
	[self endPrintingDictionaryObject];
}

-(void)printDictionaryKeysAndObjects:(NSDictionary *)dictionary
{
	NSEnumerator *enumerator=[dictionary keyEnumerator];
	id key;
	while((key=[enumerator nextObject]))
	{
		[self printDictionaryKey:key];
		[self printDictionaryObject:[dictionary objectForKey:key]];
	}
}




-(void)startNewLine
{
	[@"\n" print];
	for(int i=0;i<indentlevel;i++) [indentstring print];
}

-(void)printSeparatorIfNeeded
{
	// Generally we should call this method from other methods of
	// this class which have direct print calls, before those print
	// calls.  This ensures all needed comma separators are printed.
	// The exceptions are (a) before printing only whitespace; and
	// (b) when printing the end of a JSON Array or Object.  This is
	// to ensure we print no trailing commas.
	if(needseparator)
	{
		[@"," print];
		needseparator=NO;
	}
}



-(NSString *)stringByEscapingString:(NSString *)string
{
	int length=[string length];
	NSMutableString *res=[NSMutableString stringWithCapacity:length];

	for(int i=0;i<length;i++)
	{
		unichar c=[string characterAtIndex:i];
		if(c=='"'||c=='\\') [res appendFormat:@"\\%C",c];
		else if(c=='\b') [res appendString:@"\\b"];
		else if(c=='\f') [res appendString:@"\\f"];
		else if(c=='\n') [res appendString:@"\\n"];
		else if(c=='\r') [res appendString:@"\\r"];
		else if(c=='\t') [res appendString:@"\\t"];
		else if(c<32) [res appendFormat:@"\\u%04x",c];
		else if(asciimode&&c>=128) [res appendFormat:@"\\u%04x",c];
		else [res appendFormat:@"%C",c];
	}

	return res;
}

-(NSString *)stringByEncodingBytes:(const uint8_t *)bytes length:(int)length
{
	NSMutableString *res=[NSMutableString stringWithCapacity:length*6];

	for(int i=0;i<length;i++) [res appendFormat:@"\\u%04x",bytes[i]];

	return res;
}

@end