ObjFW  Check-in [7e1c8b88b7]

Overview
Comment:Add OFJSONRepresentationOptionSorted
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 7e1c8b88b7ccf551cb237f0687435fb56c77b2a52ef285a6710721aff01e05f4
User & Date: js on 2024-08-16 22:26:51
Other Links: manifest | tags
Context
2024-08-17
09:51
Move private functions into private headers check-in: b829f3e798 user: js tags: trunk
2024-08-16
22:26
Add OFJSONRepresentationOptionSorted check-in: 7e1c8b88b7 user: js tags: trunk
22:13
runtime: Hide symbols for mutex / once / TLS key check-in: 304dad64b4 user: js tags: trunk
Changes

Modified src/OFDictionary.m from [d2c729f348] to [795a1fd15b].

674
675
676
677
678
679
680
681

682
683
684



685
686
687
688
689
690
691
692
693
694
695

696
697

698
699
700
701
702
703
704
674
675
676
677
678
679
680

681

682

683
684
685
686
687
688
689
690
691
692
693
694
695

696

697
698
699
700
701
702
703
704
705







-
+
-

-
+
+
+










-
+
-

+








- (OFString *)
    of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options
			       depth: (size_t)depth
{
	OFMutableString *JSON = [OFMutableString stringWithString: @"{"];
	void *pool = objc_autoreleasePoolPush();
	OFEnumerator *keyEnumerator = [self keyEnumerator];
	OFArray *keys = self.allKeys;
	OFEnumerator *objectEnumerator = [self objectEnumerator];
	size_t i, count = self.count;
	id key, object;

	if (options & OFJSONRepresentationOptionSorted)
		keys = keys.sortedArray;

	if (options & OFJSONRepresentationOptionPretty) {
		OFMutableString *indentation = [OFMutableString string];

		for (i = 0; i < depth; i++)
			[indentation appendString: @"\t"];

		[JSON appendString: @"\n"];

		i = 0;
		while ((key = [keyEnumerator nextObject]) != nil &&
		for (id key in keys) {
		    (object = [objectEnumerator nextObject]) != nil) {
			void *pool2 = objc_autoreleasePoolPush();
			id object = [self objectForKey: key];
			int identifierOptions =
			    options | OFJSONRepresentationOptionIsIdentifier;

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

			[JSON appendString: indentation];
718
719
720
721
722
723
724
725

726
727

728
729
730
731
732
733
734
719
720
721
722
723
724
725

726

727
728
729
730
731
732
733
734
735







-
+
-

+








			objc_autoreleasePoolPop(pool2);
		}

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

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

			[JSON appendString: [key

Modified src/OFJSONRepresentation.h from [e38501d683] to [94be176033].

27
28
29
30
31
32
33


34
35
36
37
38
39
40
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42







+
+







 * @brief Options to change the behavior when creating a JSON representation.
 */
typedef enum {
	/** Optimize for readability */
	OFJSONRepresentationOptionPretty       = 0x01,
	/** Generate JSON5 */
	OFJSONRepresentationOptionJSON5	       = 0x02,
	/** Sort keys alphabetically */
	OFJSONRepresentationOptionSorted       = 0x04,
	OFJSONRepresentationOptionIsIdentifier = 0x10
} OFJSONRepresentationOptions;

/**
 * @protocol OFJSONRepresentation OFJSONRepresentation.h ObjFW/ObjFW.h
 *
 * @brief A protocol implemented by classes that support encoding to a JSON

Modified tests/OFJSONTests.m from [3d70f6cb68] to [2ee8657285].

61
62
63
64
65
66
67









68
69
70
71
72
73
74
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83







+
+
+
+
+
+
+
+
+







}

- (void)testJSONRepresentation
{
	OTAssert(_dictionary.JSONRepresentation,
	    @"{\"foo\":\"b\\na\\r\",\"x\":[0.5,15,null,\"foo\",false]}");
}

- (void)testSortedJSONRepresentation
{
	OTAssertEqualObjects(
	    [([OFDictionary dictionaryWithKeysAndObjects:
	    @"b", @"a", @"a", @"b", nil])
	    JSONRepresentationWithOptions: OFJSONRepresentationOptionSorted],
	    @"{\"a\":\"b\",\"b\":\"a\"}");
}

- (void)testPrettyJSONRepresentation
{
	OTAssertEqualObjects([_dictionary JSONRepresentationWithOptions:
	    OFJSONRepresentationOptionPretty],
	    @"{\n\t\"foo\": \"b\\na\\r\",\n\t\"x\": [\n\t\t0.5,\n\t\t15,"
	    @"\n\t\tnull,\n\t\t\"foo\",\n\t\tfalse\n\t]\n}");