2
Copyright (C) 2009 Stig Brautaset. All rights reserved.
4
Redistribution and use in source and binary forms, with or without
5
modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright notice, this
8
list of conditions and the following disclaimer.
10
* Redistributions in binary form must reproduce the above copyright notice,
11
this list of conditions and the following disclaimer in the documentation
12
and/or other materials provided with the distribution.
14
* Neither the name of the author nor the names of its contributors may be used
15
to endorse or promote products derived from this software without specific
16
prior written permission.
18
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
#import "SBJsonWriter.h"
32
@interface SBJsonWriter ()
34
- (BOOL)appendValue:(id)fragment into:(NSMutableString*)json;
35
- (BOOL)appendArray:(NSArray*)fragment into:(NSMutableString*)json;
36
- (BOOL)appendDictionary:(NSDictionary*)fragment into:(NSMutableString*)json;
37
- (BOOL)appendString:(NSString*)fragment into:(NSMutableString*)json;
43
@implementation SBJsonWriter
45
static NSMutableCharacterSet *kEscapeChars;
48
kEscapeChars = [[NSMutableCharacterSet characterSetWithRange: NSMakeRange(0,32)] retain];
49
[kEscapeChars addCharactersInString: @"\"\\"];
54
@synthesize humanReadable;
57
@deprecated This exists in order to provide fragment support in older APIs in one more version.
58
It should be removed in the next major version.
60
- (NSString*)stringWithFragment:(id)value {
61
[self clearErrorTrace];
63
NSMutableString *json = [NSMutableString stringWithCapacity:128];
65
if ([self appendValue:value into:json])
72
- (NSString*)stringWithObject:(id)value {
74
if ([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]]) {
75
return [self stringWithFragment:value];
78
if ([value respondsToSelector:@selector(proxyForJson)]) {
79
NSString *tmp = [self stringWithObject:[value proxyForJson]];
85
[self clearErrorTrace];
86
[self addErrorWithCode:EFRAGMENT description:@"Not valid type for JSON"];
92
return [@"\n" stringByPaddingToLength:1 + 2 * depth withString:@" " startingAtIndex:0];
95
- (BOOL)appendValue:(id)fragment into:(NSMutableString*)json {
96
if ([fragment isKindOfClass:[NSDictionary class]]) {
97
if (![self appendDictionary:fragment into:json])
100
} else if ([fragment isKindOfClass:[NSArray class]]) {
101
if (![self appendArray:fragment into:json])
104
} else if ([fragment isKindOfClass:[NSString class]]) {
105
if (![self appendString:fragment into:json])
108
} else if ([fragment isKindOfClass:[NSNumber class]]) {
109
if ('c' == *[fragment objCType])
110
[json appendString:[fragment boolValue] ? @"true" : @"false"];
112
[json appendString:[fragment stringValue]];
114
} else if ([fragment isKindOfClass:[NSNull class]]) {
115
[json appendString:@"null"];
116
} else if ([fragment respondsToSelector:@selector(proxyForJson)]) {
117
[self appendValue:[fragment proxyForJson] into:json];
120
[self addErrorWithCode:EUNSUPPORTED description:[NSString stringWithFormat:@"JSON serialisation not supported for %@", [fragment class]]];
126
- (BOOL)appendArray:(NSArray*)fragment into:(NSMutableString*)json {
127
if (maxDepth && ++depth > maxDepth) {
128
[self addErrorWithCode:EDEPTH description: @"Nested too deep"];
131
[json appendString:@"["];
134
for (id value in fragment) {
136
[json appendString:@","];
140
if ([self humanReadable])
141
[json appendString:[self indent]];
143
if (![self appendValue:value into:json]) {
149
if ([self humanReadable] && [fragment count])
150
[json appendString:[self indent]];
151
[json appendString:@"]"];
155
- (BOOL)appendDictionary:(NSDictionary*)fragment into:(NSMutableString*)json {
156
if (maxDepth && ++depth > maxDepth) {
157
[self addErrorWithCode:EDEPTH description: @"Nested too deep"];
160
[json appendString:@"{"];
162
NSString *colon = [self humanReadable] ? @" : " : @":";
164
NSArray *keys = [fragment allKeys];
166
keys = [keys sortedArrayUsingSelector:@selector(compare:)];
168
for (id value in keys) {
170
[json appendString:@","];
174
if ([self humanReadable])
175
[json appendString:[self indent]];
177
if (![value isKindOfClass:[NSString class]]) {
178
[self addErrorWithCode:EUNSUPPORTED description: @"JSON object key must be string"];
182
if (![self appendString:value into:json])
185
[json appendString:colon];
186
if (![self appendValue:[fragment objectForKey:value] into:json]) {
187
[self addErrorWithCode:EUNSUPPORTED description:[NSString stringWithFormat:@"Unsupported value for key %@ in object", value]];
193
if ([self humanReadable] && [fragment count])
194
[json appendString:[self indent]];
195
[json appendString:@"}"];
199
- (BOOL)appendString:(NSString*)fragment into:(NSMutableString*)json {
201
[json appendString:@"\""];
203
NSRange esc = [fragment rangeOfCharacterFromSet:kEscapeChars];
205
// No special chars -- can just add the raw string:
206
[json appendString:fragment];
209
NSUInteger length = [fragment length];
210
for (NSUInteger i = 0; i < length; i++) {
211
unichar uc = [fragment characterAtIndex:i];
213
case '"': [json appendString:@"\\\""]; break;
214
case '\\': [json appendString:@"\\\\"]; break;
215
case '\t': [json appendString:@"\\t"]; break;
216
case '\n': [json appendString:@"\\n"]; break;
217
case '\r': [json appendString:@"\\r"]; break;
218
case '\b': [json appendString:@"\\b"]; break;
219
case '\f': [json appendString:@"\\f"]; break;
222
[json appendFormat:@"\\u%04x", uc];
224
CFStringAppendCharacters((CFMutableStringRef)json, &uc, 1);
232
[json appendString:@"\""];