-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNSJSONSerialization+Comments.m
More file actions
320 lines (271 loc) · 12.1 KB
/
NSJSONSerialization+Comments.m
File metadata and controls
320 lines (271 loc) · 12.1 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//
// NSJSONSerialization+Comments.m
// ABCodeEditor
//
// Created by Alexander Blach on 22.07.14.
// Copyright (c) 2014 Alexander Blach. All rights reserved.
//
#import "NSJSONSerialization+Comments.h"
static const int EncLen_UTF8[256] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1};
static inline void copyUTF8CharacterAndAdvancePointers(UTF8Char **source, UTF8Char **target) {
UTF8Char character = **source;
if (__builtin_expect(character < 128, 1)) {
// one byte UTF-8 character
**target = **source;
*source += 1;
*target += 1;
} else {
int len = EncLen_UTF8[character];
memcpy(*target, *source, len);
*source += len;
*target += len;
}
}
static inline void skipUTF8Character(UTF8Char **source) { *source += EncLen_UTF8[**source]; }
@implementation NSJSONSerialization (Comments)
+ (NSData *)dataByStrippingJSONCommentsAndWhiteSpaceOfUTF8Data:(NSData *)data
skipBytes:(NSUInteger)bytesToSkip {
UTF8Char *originalString = (UTF8Char *)[data bytes];
NSUInteger length = [data length];
UTF8Char *modifiedString = malloc(sizeof(UTF8Char) * length);
UTF8Char *originalStringCurrent = originalString;
UTF8Char *originalStringEnd = originalString + length;
UTF8Char *modifiedStringCurrent = modifiedString;
// skip bytes
originalStringCurrent += bytesToSkip;
while (originalStringCurrent < originalStringEnd) {
UTF8Char currentChar = *originalStringCurrent;
if (currentChar == '\t' || currentChar == ' ' || currentChar == '\r'
|| currentChar == '\n') {
// skip whitespace
// Ignore whitespace tokens. According to ES 5.1 section 15.12.1.1,
// whitespace tokens include tabs, carriage returns, line feeds, and
// space characters.
originalStringCurrent++;
} else if (currentChar == '"') {
// we found a string! -> handle it
*modifiedStringCurrent++ = currentChar;
originalStringCurrent++;
UTF8Char lastChar = 0;
while (originalStringCurrent < originalStringEnd) {
currentChar = *originalStringCurrent;
if (currentChar == '"') {
*modifiedStringCurrent++ = currentChar;
originalStringCurrent++;
if (lastChar == '\\') {
// was escaped character -> not at string end
} else {
// arrived at end of string
break;
}
} else if (currentChar == '\n' || currentChar == '\r') {
// line breaks should not happen in JSON strings!
*modifiedStringCurrent++ = currentChar;
originalStringCurrent++;
break;
} else {
// still in string -> copy character
copyUTF8CharacterAndAdvancePointers(&originalStringCurrent,
&modifiedStringCurrent);
}
lastChar = currentChar;
}
} else if (currentChar == '/' && originalStringCurrent + 1 < originalStringEnd) {
// maybe we have a single-line or multi-line comment
UTF8Char nextChar = *(originalStringCurrent + 1);
if (nextChar == '/') {
// single line comment
originalStringCurrent += 2;
while (originalStringCurrent < originalStringEnd) {
char currentChar = *originalStringCurrent;
if (currentChar == '\r' || currentChar == '\n') {
// at end of line -> comment end
break;
} else {
// skip
skipUTF8Character(&originalStringCurrent);
}
}
} else if (nextChar == '*') {
// multi line comment
originalStringCurrent += 2;
while (originalStringCurrent < originalStringEnd) {
char currentChar = *originalStringCurrent;
if (currentChar == '*') {
originalStringCurrent++;
if (originalStringCurrent < originalStringEnd) {
currentChar = *originalStringCurrent;
if (currentChar == '/') {
// comment end!
originalStringCurrent++;
break;
}
}
} else {
// skip
skipUTF8Character(&originalStringCurrent);
}
}
} else {
// nope, no comment, just copy the character
*modifiedStringCurrent++ = currentChar;
originalStringCurrent++;
}
} else {
// copy character as is
copyUTF8CharacterAndAdvancePointers(&originalStringCurrent, &modifiedStringCurrent);
}
}
NSUInteger modifiedStringLength = modifiedStringCurrent - modifiedString;
if (modifiedStringLength != length) {
modifiedString = realloc(modifiedString, sizeof(UTF8Char) * modifiedStringLength);
return [NSData dataWithBytesNoCopy:modifiedString
length:modifiedStringLength
freeWhenDone:YES];
} else {
free(modifiedString);
return data;
}
}
+ (id)JSONObjectWithCommentedUTF8Data:(NSData *)data
options:(NSJSONReadingOptions)opt
error:(NSError **)error {
NSData *strippedData =
[self dataByStrippingJSONCommentsAndWhiteSpaceOfUTF8Data:data skipBytes:0];
// NSLog(@"before:\n%@", [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]
// autorelease]);
// NSLog(@"after:\n%@", [[[NSString alloc] initWithData:strippedData
// encoding:NSUTF8StringEncoding] autorelease]);
return [self JSONObjectWithData:strippedData options:opt error:error];
}
+ (NSStringEncoding)stringEncodingFromData:(NSData *)data detectedBOMSize:(NSUInteger *)bomSize {
NSStringEncoding encoding = 0;
if (bomSize) {
*bomSize = 0;
}
NSUInteger fileSize = [data length];
// try to get from BOM
if (fileSize >= 2) {
UInt8 *bomBuffer = (UInt8 *)[data bytes];
// go back to start of file
if (fileSize >= 2 && fileSize % 2 == 0) {
// even amount of bytes? could be UTF-16 or UTF-32
if (bomBuffer[0] == 0xFE && bomBuffer[1] == 0xFF) {
// Big Endian
encoding = NSUTF16StringEncoding;
if (bomSize) {
*bomSize = 2;
}
} else if (bomBuffer[0] == 0xFF && bomBuffer[1] == 0xFE) {
// Little Endian
encoding = NSUTF16StringEncoding;
if (bomSize) {
*bomSize = 2;
}
} else if (fileSize >= 4) {
if (bomBuffer[0] == 0x00 && bomBuffer[1] == 0x00 && bomBuffer[2] == 0xFE
&& bomBuffer[3] == 0xFF) {
// Big Endian
encoding = NSUTF32StringEncoding;
if (bomSize) {
*bomSize = 4;
}
} else if (bomBuffer[0] == 0xFF && bomBuffer[1] == 0xFE && bomBuffer[2] == 0x00
&& bomBuffer[3] == 0x00) {
// Little Endian
encoding = NSUTF32StringEncoding;
if (bomSize) {
*bomSize = 4;
}
}
}
}
if (!encoding) {
if (fileSize >= 3) {
if (bomBuffer[0] == 0xEF && bomBuffer[1] == 0xBB && bomBuffer[2] == 0xBF) {
encoding = NSUTF8StringEncoding;
if (bomSize) {
*bomSize = 3;
}
}
}
}
}
return encoding;
}
+ (id)JSONObjectWithCommentedData:(NSData *)data
options:(NSJSONReadingOptions)opt
error:(NSError **)error {
if (data) {
NSUInteger bomSize = 0;
NSStringEncoding encoding = [self stringEncodingFromData:data detectedBOMSize:&bomSize];
if (encoding == 0 || // assume UTF-8 if no BOM is detected
encoding == NSUTF8StringEncoding) {
// we can use the data as is, because it is already UTF-8
} else {
// convert to UTF-8 first
NSString *string = [[NSString alloc] initWithData:data encoding:encoding];
if (!string) {
if (error) {
// use the same error description, domain, and code as NSJSONSerialization
*error =
[NSError errorWithDomain:NSCocoaErrorDomain
code:0xf00
userInfo:@{
(NSString *)kCFErrorDescriptionKey :
@"Unable to convert data to a string using the "
@"detected encoding. The data may be corrupt."
}];
}
return nil;
} else {
data = [string dataUsingEncoding:NSUTF8StringEncoding];
[string release];
bomSize = 0;
}
}
return [self JSONObjectWithCommentedUTF8Data:data options:opt error:error];
} else {
return nil;
}
}
+ (id)JSONObjectWithCommentedContentsOfURL:(NSURL *)url
options:(NSJSONReadingOptions)opt
error:(NSError **)error {
// load data from URL
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:error];
return [self JSONObjectWithCommentedData:data options:opt error:error];
}
+ (id)JSONObjectWithCommentedContentsOfFile:(NSString *)path
options:(NSJSONReadingOptions)opt
error:(NSError **)error {
// load data from file
NSData *data = [NSData dataWithContentsOfFile:path options:NSDataReadingUncached error:error];
return [self JSONObjectWithCommentedData:data options:opt error:error];
}
+ (id)JSONObjectWithCommentedString:(NSString *)string
options:(NSJSONReadingOptions)opt
error:(NSError **)error {
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
return [self JSONObjectWithCommentedUTF8Data:data options:opt error:error];
}
+ (NSString *)stringWithJSONObject:(id)obj
options:(NSJSONWritingOptions)opt
error:(NSError **)error {
NSData *data = [NSJSONSerialization dataWithJSONObject:obj options:opt error:error];
if (data) {
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return [string autorelease];
} else {
return nil;
}
}
@end