ObjFW  Check-in [54f80aba7a]

Overview
Comment:Initial JSON5 support.

Supported so far:
* comments
* single quoted strings

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 54f80aba7ae658b9ef60d1a5f4bd056ac79d2f1efc31e8ee5c430070544f9683
User & Date: js on 2012-06-04 22:32:38
Other Links: manifest | tags
Context
2012-06-04
23:36
Allow numbers in JSON5 format. check-in: 3e028f7479 user: js tags: trunk
22:32
Initial JSON5 support. check-in: 54f80aba7a user: js tags: trunk
2012-05-30
22:03
atomic.h: Use sete instead of the ugly jne. check-in: 22aea96b79 user: js tags: trunk
Changes

Modified src/OFString+JSONValue.h from [db1f76de2d] to [7f041856a4].

23
24
25
26
27
28
29



30
31
32
33
34
#ifdef __cplusplus
}
#endif

@interface OFString (JSONValue)
/**
 * \brief Creates an object from the JSON value of the string.



 *
 * \return An object
 */
- (id)JSONValue;
@end







>
>
>





23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifdef __cplusplus
}
#endif

@interface OFString (JSONValue)
/**
 * \brief Creates an object from the JSON value of the string.
 *
 * \note This also allows parsing JSON5, an extension of JSON. See
 *	 http://json5.org/ for more details.
 *
 * \return An object
 */
- (id)JSONValue;
@end

Modified src/OFString+JSONValue.m from [9190d7a166] to [8ad19cb41d].

126
127
128
129
130
131
132

133
134
135
136
137
138
139
}

static inline OFString*
parseString(const char *restrict *pointer, const char *stop)
{
	char *buffer;
	size_t i = 0;


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

	if ((buffer = malloc(stop - *pointer)) == NULL)
		return nil;








>







126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
}

static inline OFString*
parseString(const char *restrict *pointer, const char *stop)
{
	char *buffer;
	size_t i = 0;
	char delimiter = **pointer;

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

	if ((buffer = malloc(stop - *pointer)) == NULL)
		return nil;

232
233
234
235
236
237
238
239
240
241
242
243
244
245
246

				break;
			default:
				 free(buffer);
				 return nil;
			}
		/* End of string found */
		} else if (**pointer == '"') {
			OFString *ret;

			@try {
				ret = [OFString stringWithUTF8String: buffer
							      length: i];
			} @finally {
				free(buffer);







|







233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

				break;
			default:
				 free(buffer);
				 return nil;
			}
		/* End of string found */
		} else if (**pointer == delimiter) {
			OFString *ret;

			@try {
				ret = [OFString stringWithUTF8String: buffer
							      length: i];
			} @finally {
				free(buffer);
396
397
398
399
400
401
402

403
404
405
406
407
408
409
	skipWhitespacesAndComments(pointer, stop);

	if (*pointer >= stop)
		return nil;

	switch (**pointer) {
	case '"':

		return parseString(pointer, stop);
	case '[':
		return parseArray(pointer, stop);
	case '{':
		return parseDictionary(pointer, stop);
	case 't':
		if (*pointer + 3 >= stop)







>







397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
	skipWhitespacesAndComments(pointer, stop);

	if (*pointer >= stop)
		return nil;

	switch (**pointer) {
	case '"':
	case '\'':
		return parseString(pointer, stop);
	case '[':
		return parseArray(pointer, stop);
	case '{':
		return parseDictionary(pointer, stop);
	case 't':
		if (*pointer + 3 >= stop)

Modified tests/OFJSONTests.m from [b853b74aa1] to [e8e782d6ff].

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

static OFString *module = @"OFJSON";

@implementation TestsAppDelegate (JSONTests)
- (void)JSONTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *s = @"{\"foo\"\t:\"bar\", \"x\":/*fooo*/ [7.5\r,null//bar\n"
	    @",\"foo\",false]}";
	OFDictionary *d = [OFDictionary dictionaryWithKeysAndObjects:
	    @"foo", @"bar",
	    @"x", [OFArray arrayWithObjects:
		[OFNumber numberWithFloat: 7.5f],
		[OFNull null],
		@"foo",
		[OFNumber numberWithBool: NO],
		nil],
	    nil];

	TEST(@"-[JSONValue #1]", [[s JSONValue] isEqual: d])

	TEST(@"-[JSONRepresentation]", [[d JSONRepresentation] isEqual:
	    @"{\"foo\":\"bar\",\"x\":[7.5,null,\"foo\",false]}"])

	EXPECT_EXCEPTION(@"-[JSONValue #2]", OFInvalidEncodingException,
	    [@"{" JSONValue])
	EXPECT_EXCEPTION(@"-[JSONValue #3]", OFInvalidEncodingException,
	    [@"]" JSONValue])
	EXPECT_EXCEPTION(@"-[JSONValue #4]", OFInvalidEncodingException,
	    [@"bar" JSONValue])







|


|











|







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

static OFString *module = @"OFJSON";

@implementation TestsAppDelegate (JSONTests)
- (void)JSONTests
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFString *s = @"{\"foo\"\t:'ba\\r', \"x\":/*fooo*/ [7.5\r,null//bar\n"
	    @",\"foo\",false]}";
	OFDictionary *d = [OFDictionary dictionaryWithKeysAndObjects:
	    @"foo", @"ba\r",
	    @"x", [OFArray arrayWithObjects:
		[OFNumber numberWithFloat: 7.5f],
		[OFNull null],
		@"foo",
		[OFNumber numberWithBool: NO],
		nil],
	    nil];

	TEST(@"-[JSONValue #1]", [[s JSONValue] isEqual: d])

	TEST(@"-[JSONRepresentation]", [[d JSONRepresentation] isEqual:
	    @"{\"foo\":\"ba\\r\",\"x\":[7.5,null,\"foo\",false]}"])

	EXPECT_EXCEPTION(@"-[JSONValue #2]", OFInvalidEncodingException,
	    [@"{" JSONValue])
	EXPECT_EXCEPTION(@"-[JSONValue #3]", OFInvalidEncodingException,
	    [@"]" JSONValue])
	EXPECT_EXCEPTION(@"-[JSONValue #4]", OFInvalidEncodingException,
	    [@"bar" JSONValue])