ObjFW  Check-in [e07fa89b44]

Overview
Comment:Add JSON5 output option
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: e07fa89b442be190bf1d02031d19afcb6ba1ab122a89cfc55977180ff8d49e04
User & Date: js on 2014-02-08 18:04:30
Other Links: manifest | tags
Context
2014-02-10
10:46
-[cancelAsyncRequests] documentation: Add warning check-in: 048c4912d6 user: js tags: trunk
2014-02-08
18:04
Add JSON5 output option check-in: e07fa89b44 user: js tags: trunk
09:26
Add -[JSONRepresentationWithOptions:] check-in: eaa612ad50 user: js tags: trunk
Changes

Modified src/OFDictionary.m from [a7500a2644] to [a9ccdce0b3].

613
614
615
616
617
618
619





620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644


645



646
647
648
649
650
651
652
653
654

		[JSON appendString: @"\n"];

		i = 0;
		while ((key = [keyEnumerator nextObject]) != nil &&
		    (object = [objectEnumerator nextObject]) != nil) {
			void *pool2 = objc_autoreleasePoolPush();






			[JSON appendString: indentation];
			[JSON appendString: @"\t"];
			[JSON appendString: [key
			    OF_JSONRepresentationWithOptions: options
						       depth: depth + 1]];
			[JSON appendString: @": "];
			[JSON appendString: [object
			    OF_JSONRepresentationWithOptions: options
						       depth: depth + 1]];

			if (++i < count)
				[JSON appendString: @",\n"];
			else
				[JSON appendString: @"\n"];

			objc_autoreleasePoolPop(pool2);
		}

		[JSON appendString: indentation];
	} else {
		i = 0;
		while ((key = [keyEnumerator nextObject]) != nil &&
		    (object = [objectEnumerator nextObject]) != nil) {
			void *pool2 = objc_autoreleasePoolPush();






			[JSON appendString: [key
			    OF_JSONRepresentationWithOptions: options
						       depth: depth + 1]];
			[JSON appendString: @":"];
			[JSON appendString: [object
			    OF_JSONRepresentationWithOptions: options
						       depth: depth + 1]];

			if (++i < count)







>
>
>
>
>




|




















>
>

>
>
>

|







613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664

		[JSON appendString: @"\n"];

		i = 0;
		while ((key = [keyEnumerator nextObject]) != nil &&
		    (object = [objectEnumerator nextObject]) != nil) {
			void *pool2 = objc_autoreleasePoolPush();
			int identifierOptions =
			    options | OF_JSON_REPRESENTATION_IDENTIFIER;

			if (![key isKindOfClass: [OFString class]])
				@throw [OFInvalidArgumentException exception];

			[JSON appendString: indentation];
			[JSON appendString: @"\t"];
			[JSON appendString: [key
			    OF_JSONRepresentationWithOptions: identifierOptions
						       depth: depth + 1]];
			[JSON appendString: @": "];
			[JSON appendString: [object
			    OF_JSONRepresentationWithOptions: options
						       depth: depth + 1]];

			if (++i < count)
				[JSON appendString: @",\n"];
			else
				[JSON appendString: @"\n"];

			objc_autoreleasePoolPop(pool2);
		}

		[JSON appendString: indentation];
	} else {
		i = 0;
		while ((key = [keyEnumerator nextObject]) != nil &&
		    (object = [objectEnumerator nextObject]) != nil) {
			void *pool2 = objc_autoreleasePoolPush();
			int identifierOptions =
			    options | OF_JSON_REPRESENTATION_IDENTIFIER;

			if (![key isKindOfClass: [OFString class]])
				@throw [OFInvalidArgumentException exception];

			[JSON appendString: [key
			    OF_JSONRepresentationWithOptions: identifierOptions
						       depth: depth + 1]];
			[JSON appendString: @":"];
			[JSON appendString: [object
			    OF_JSONRepresentationWithOptions: options
						       depth: depth + 1]];

			if (++i < count)

Modified src/OFJSONRepresentation.h from [25865c5918] to [f9fd77253b].

13
14
15
16
17
18
19
20


21
22
23
24
25
26
27
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

@class OFString;

enum {
	OF_JSON_REPRESENTATION_PRETTY = 1


};

/*!
 * @brief A protocol implemented by classes that support encoding to a JSON
 *	  representation.
 *
 * @warning Although this method can be called directly on classes other than







|
>
>







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
 * file.
 */

@class OFString;

enum {
	OF_JSON_REPRESENTATION_PRETTY	  = 0x01,
	OF_JSON_REPRESENTATION_JSON5	  = 0x02,
	OF_JSON_REPRESENTATION_IDENTIFIER = 0x10
};

/*!
 * @brief A protocol implemented by classes that support encoding to a JSON
 *	  representation.
 *
 * @warning Although this method can be called directly on classes other than
40
41
42
43
44
45
46

47
48
49
50
51
 * @brief Returns the JSON representation of the object as a string.
 *
 * @param options The options to use when creating a JSON representation.@n
 *		  Possible values are:
 *		  Value                           | Description
 *		  --------------------------------|-------------------------
 *		  `OF_JSON_REPRESENTATION_PRETTY` | Optimize for readability

 *
 * @return The JSON representation of the object as a string
 */
- (OFString*)JSONRepresentationWithOptions: (int)options;
@end







>





42
43
44
45
46
47
48
49
50
51
52
53
54
 * @brief Returns the JSON representation of the object as a string.
 *
 * @param options The options to use when creating a JSON representation.@n
 *		  Possible values are:
 *		  Value                           | Description
 *		  --------------------------------|-------------------------
 *		  `OF_JSON_REPRESENTATION_PRETTY` | Optimize for readability
 *		  `OF_JSON_REPRESENTATION_JSON5`  | Generate JSON5
 *
 * @return The JSON representation of the object as a string
 */
- (OFString*)JSONRepresentationWithOptions: (int)options;
@end

Modified src/OFNumber.m from [12ac22c22b] to [890ea8bb44].

1420
1421
1422
1423
1424
1425
1426

1427
1428
1429
1430


1431
1432
1433
1434
1435
1436
1437
	double doubleValue;

	if (_type == OF_NUMBER_BOOL)
		return (_value.bool_ ? @"true" : @"false");

	doubleValue = [self doubleValue];
	if (isinf(doubleValue)) {

		if (doubleValue > 0)
			return @"Infinity";
		else
			return @"-Infinity";


	}

	return [self description];
}

- (OFDataArray*)messagePackRepresentation
{







>
|
|
|
|
>
>







1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
	double doubleValue;

	if (_type == OF_NUMBER_BOOL)
		return (_value.bool_ ? @"true" : @"false");

	doubleValue = [self doubleValue];
	if (isinf(doubleValue)) {
		if (options & OF_JSON_REPRESENTATION_JSON5) {
			if (doubleValue > 0)
				return @"Infinity";
			else
				return @"-Infinity";
		} else
			@throw [OFInvalidArgumentException exception];
	}

	return [self description];
}

- (OFDataArray*)messagePackRepresentation
{

Modified src/OFString.m from [006b401a4b] to [ab5f534f76].

15
16
17
18
19
20
21

22
23
24
25
26
27
28
 */

#include "config.h"

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>


#include <sys/stat.h>

#import "OFString.h"
#import "OFString_UTF8.h"
#import "OFString_UTF8+Private.h"
#import "OFArray.h"







>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 */

#include "config.h"

#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include <sys/stat.h>

#import "OFString.h"
#import "OFString_UTF8.h"
#import "OFString_UTF8+Private.h"
#import "OFArray.h"
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469










1470
1471












1472
1473
1474
1475
1476
1477
1478
			      withString: @"\\\\"];
	[JSON replaceOccurrencesOfString: @"\""
			      withString: @"\\\""];
	[JSON replaceOccurrencesOfString: @"\b"
			      withString: @"\\b"];
	[JSON replaceOccurrencesOfString: @"\f"
			      withString: @"\\f"];
	[JSON replaceOccurrencesOfString: @"\n"
			      withString: @"\\n"];
	[JSON replaceOccurrencesOfString: @"\r"
			      withString: @"\\r"];
	[JSON replaceOccurrencesOfString: @"\t"
			      withString: @"\\t"];











	[JSON prependString: @"\""];
	[JSON appendString: @"\""];













	[JSON makeImmutable];

	return JSON;
}

- (OFDataArray*)messagePackRepresentation







<
<





>
>
>
>
>
>
>
>
>
>
|
|
>
>
>
>
>
>
>
>
>
>
>
>







1457
1458
1459
1460
1461
1462
1463


1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
			      withString: @"\\\\"];
	[JSON replaceOccurrencesOfString: @"\""
			      withString: @"\\\""];
	[JSON replaceOccurrencesOfString: @"\b"
			      withString: @"\\b"];
	[JSON replaceOccurrencesOfString: @"\f"
			      withString: @"\\f"];


	[JSON replaceOccurrencesOfString: @"\r"
			      withString: @"\\r"];
	[JSON replaceOccurrencesOfString: @"\t"
			      withString: @"\\t"];

	if (options & OF_JSON_REPRESENTATION_JSON5) {
		[JSON replaceOccurrencesOfString: @"\n"
				      withString: @"\\\n"];

		if (options & OF_JSON_REPRESENTATION_IDENTIFIER) {
			const char *cString = [self UTF8String];

			if ((!isalpha(cString[0]) && cString[0] != '_' &&
			    cString[0] != '$') ||
			    strpbrk(cString, " \n\r\t\b\f\\\"'") != NULL) {
				[JSON prependString: @"\""];
				[JSON appendString: @"\""];
			}
		} else {
			[JSON prependString: @"\""];
			[JSON appendString: @"\""];
		}
	} else {
		[JSON replaceOccurrencesOfString: @"\n"
				      withString: @"\\n"];

		[JSON prependString: @"\""];
		[JSON appendString: @"\""];
	}

	[JSON makeImmutable];

	return JSON;
}

- (OFDataArray*)messagePackRepresentation