ObjFW  Check-in [6203c9e8f2]

Overview
Comment:Allow comments in JSON.

While comments are not part of the JSON standard itself, allowing them
makes JSON a useful format for configurations and accepting more than is
required never hurts.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 6203c9e8f276c814a8ce9cd659e395c1bc2a19b26c7bed157f1389b7e3b2c81c
User & Date: js on 2011-12-11 01:59:12
Other Links: manifest | tags
Context
2011-12-12
13:32
Improve serialization of OFXMLElement. check-in: 0a083111c7 user: js tags: trunk
2011-12-11
01:59
Allow comments in JSON. check-in: 6203c9e8f2 user: js tags: trunk
2011-12-10
21:54
Add new files to Xcode project. check-in: e3006f8376 user: js tags: trunk
Changes

Modified src/OFString+JSONValue.m from [5f77e1f7f1] to [12e5560901].

38
39
40
41
42
43
44





















































45
46
47
48
49
50
51
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







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







static void
skipWhitespaces(const char *restrict *pointer, const char *stop)
{
	while (*pointer < stop && (**pointer == ' ' || **pointer == '\t' ||
	    **pointer == '\r' || **pointer == '\n'))
		(*pointer)++;
}

static void
skipComment(const char *restrict *pointer, const char *stop)
{
	if (**pointer != '/')
		return;

	if (*pointer + 1 >= stop)
		return;

	(*pointer)++;

	if (**pointer == '*') {
		BOOL lastIsAsterisk = NO;

		(*pointer)++;

		while (*pointer < stop) {
			if (lastIsAsterisk && **pointer == '/') {
				(*pointer)++;
				return;
			}

			lastIsAsterisk = (**pointer == '*');

			(*pointer)++;
		}
	} else {
		(*pointer)++;

		while (*pointer < stop) {
			if (**pointer == '\r' || **pointer == '\n') {
				(*pointer)++;
				return;
			}

			(*pointer)++;
		}
	}
}

static void
skipWhitespacesAndComments(const char *restrict *pointer, const char *stop)
{
	const char *old = NULL;

	while (old != *pointer) {
		old = *pointer;

		skipWhitespaces(pointer, stop);
		skipComment(pointer, stop);
	}
}

static OF_INLINE uint16_t
parseUnicodeEscape(const char *pointer, const char *stop)
{
	uint16_t ret = 0;
	char i;

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
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







-
+











-
+





-
+








	if (++(*pointer) >= stop)
		return nil;

	while (**pointer != ']') {
		id object;

		skipWhitespaces(pointer, stop);
		skipWhitespacesAndComments(pointer, stop);
		if (*pointer >= stop)
			return nil;

		if (**pointer == ']')
			break;

		if ((object = nextObject(pointer, stop)) == nil)
			return nil;

		[array addObject: object];

		skipWhitespaces(pointer, stop);
		skipWhitespacesAndComments(pointer, stop);
		if (*pointer >= stop)
			return nil;

		if (**pointer == ',') {
			(*pointer)++;
			skipWhitespaces(pointer, stop);
			skipWhitespacesAndComments(pointer, stop);

			if (*pointer >= stop)
				return nil;
		} else if (**pointer != ']')
			return nil;
	}

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
310
311
312
313
314
315
316

317
318
319
320
321
322
323
324
325
326

327
328
329
330
331
332
333
334
335
336
337
338

339
340
341
342
343
344

345
346
347
348
349
350
351
352







-
+









-
+











-
+





-
+








	if (++(*pointer) >= stop)
		return nil;

	while (**pointer != '}') {
		id key, object;

		skipWhitespaces(pointer, stop);
		skipWhitespacesAndComments(pointer, stop);
		if (*pointer >= stop)
			return nil;

		if (**pointer == '}')
			break;

		if ((key = nextObject(pointer, stop)) == nil)
			return nil;

		skipWhitespaces(pointer, stop);
		skipWhitespacesAndComments(pointer, stop);
		if (*pointer + 1 >= stop || **pointer != ':')
			return nil;

		(*pointer)++;

		if ((object = nextObject(pointer, stop)) == nil)
			return nil;

		[dictionary setObject: object
			       forKey: key];

		skipWhitespaces(pointer, stop);
		skipWhitespacesAndComments(pointer, stop);
		if (*pointer >= stop)
			return nil;

		if (**pointer == ',') {
			(*pointer)++;
			skipWhitespaces(pointer, stop);
			skipWhitespacesAndComments(pointer, stop);

			if (*pointer >= stop)
				return nil;
		} else if (**pointer != '}')
			return nil;
	}

340
341
342
343
344
345
346
347

348
349
350
351
352
353
354
393
394
395
396
397
398
399

400
401
402
403
404
405
406
407







-
+








	return number;
}

static id
nextObject(const char *restrict *pointer, const char *stop)
{
	skipWhitespaces(pointer, stop);
	skipWhitespacesAndComments(pointer, stop);

	if (*pointer >= stop)
		return nil;

	switch (**pointer) {
	case '"':
		return parseString(pointer, stop);
407
408
409
410
411
412
413
414

415
416
417
418
419
420
421
460
461
462
463
464
465
466

467
468
469
470
471
472
473
474







-
+







- (id)JSONValue
{
	const char *pointer = [self UTF8String];
	const char *stop = pointer + [self UTF8StringLength];
	id object;

	object = nextObject(&pointer, stop);
	skipWhitespaces(&pointer, stop);
	skipWhitespacesAndComments(&pointer, stop);

	if (pointer < stop || object == nil)
		@throw [OFInvalidEncodingException exceptionWithClass: isa];

	return object;
}
@end