Overview
Comment: | Rename JSON representation options |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | new-naming-convention |
Files: | files | file ages | folders |
SHA3-256: |
5ce8c7cca741504894b36168c6b116ca |
User & Date: | js on 2021-04-17 17:49:04 |
Other Links: | branch diff | manifest | tags |
Context
2021-04-17
| ||
18:36 | Rename all remaining enums check-in: aa74e85220 user: js tags: new-naming-convention | |
17:49 | Rename JSON representation options check-in: 5ce8c7cca7 user: js tags: new-naming-convention | |
16:57 | Rename all enums in OFGZIPStream.h check-in: 827819d93c user: js tags: new-naming-convention | |
Changes
Modified src/OFArray.m from [a47e5750f8] to [3de2dc55f8].
︙ | ︙ | |||
34 35 36 37 38 39 40 | #import "OFOutOfRangeException.h" static struct { Class isa; } placeholder; @interface OFArray () | | > | | 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #import "OFOutOfRangeException.h" static struct { Class isa; } placeholder; @interface OFArray () - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth; @end @interface OFPlaceholderArray: OFArray @end @implementation OFPlaceholderArray - (instancetype)init |
︙ | ︙ | |||
574 575 576 577 578 579 580 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } | | > | > | | | 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } - (OFString *)JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options { return [self of_JSONRepresentationWithOptions: options depth: 0]; } - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth { OFMutableString *JSON = [OFMutableString stringWithString: @"["]; void *pool = objc_autoreleasePoolPush(); size_t i, count = self.count; if (options & OFJSONRepresentationOptionPretty) { OFMutableString *indentation = [OFMutableString string]; for (i = 0; i < depth; i++) [indentation appendString: @"\t"]; [JSON appendString: @"\n"]; |
︙ | ︙ |
Modified src/OFDictionary.m from [18419a43d0] to [6b73f7f97f].
︙ | ︙ | |||
35 36 37 38 39 40 41 | static struct { Class isa; } placeholder; static OFCharacterSet *URLQueryPartAllowedCharacterSet = nil; @interface OFDictionary () | | > | | 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | static struct { Class isa; } placeholder; static OFCharacterSet *URLQueryPartAllowedCharacterSet = nil; @interface OFDictionary () - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth; @end @interface OFDictionaryPlaceholder: OFDictionary @end OF_DIRECT_MEMBERS @interface OFDictionaryObjectEnumerator: OFEnumerator |
︙ | ︙ | |||
715 716 717 718 719 720 721 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } | | > | > | | | | 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } - (OFString *)JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options { return [self of_JSONRepresentationWithOptions: options depth: 0]; } - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth { OFMutableString *JSON = [OFMutableString stringWithString: @"{"]; void *pool = objc_autoreleasePoolPush(); OFEnumerator *keyEnumerator = [self keyEnumerator]; OFEnumerator *objectEnumerator = [self objectEnumerator]; size_t i, count = self.count; id key, object; 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 && (object = [objectEnumerator nextObject]) != nil) { void *pool2 = objc_autoreleasePoolPush(); int identifierOptions = options | OFJSONRepresentationOptionIsIdentifier; if (![key isKindOfClass: [OFString class]]) @throw [OFInvalidArgumentException exception]; [JSON appendString: indentation]; [JSON appendString: @"\t"]; [JSON appendString: [key |
︙ | ︙ | |||
773 774 775 776 777 778 779 | [JSON appendString: indentation]; } else { i = 0; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) { void *pool2 = objc_autoreleasePoolPush(); int identifierOptions = | | | 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | [JSON appendString: indentation]; } else { i = 0; while ((key = [keyEnumerator nextObject]) != nil && (object = [objectEnumerator nextObject]) != nil) { void *pool2 = objc_autoreleasePoolPush(); int identifierOptions = options | OFJSONRepresentationOptionIsIdentifier; if (![key isKindOfClass: [OFString class]]) @throw [OFInvalidArgumentException exception]; [JSON appendString: [key of_JSONRepresentationWithOptions: identifierOptions depth: depth + 1]]; |
︙ | ︙ |
Modified src/OFJSONRepresentation.h from [121e6d00f3] to [a068535d8e].
︙ | ︙ | |||
15 16 17 18 19 20 21 | #import "OFObject.h" @class OFString; OF_ASSUME_NONNULL_BEGIN | > > > | > | > | | < > | 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #import "OFObject.h" @class OFString; OF_ASSUME_NONNULL_BEGIN /** * @brief Options to change the behavior when creating a JSON representation. */ typedef enum OFJSONRepresentationOptions { /** Optimize for readability */ OFJSONRepresentationOptionPretty = 0x01, /** Generate JSON5 */ OFJSONRepresentationOptionJSON5 = 0x02, OFJSONRepresentationOptionIsIdentifier = 0x10 } OFJSONRepresentationOptions; /** * @protocol OFJSONRepresentation * OFJSONRepresentation.h ObjFW/OFJSONRepresentation.h * * @brief A protocol implemented by classes that support encoding to a JSON * representation. |
︙ | ︙ | |||
41 42 43 44 45 46 47 | * @brief The JSON representation of the object as a string. */ @property (readonly, nonatomic) OFString *JSONRepresentation; /** * @brief Returns the JSON representation of the object as a string. * | | < < < < < < | > | 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | * @brief The JSON representation of the object as a string. */ @property (readonly, nonatomic) OFString *JSONRepresentation; /** * @brief Returns the JSON representation of the object as a string. * * @param options The options to use when creating a JSON representation * @return The JSON representation of the object as a string */ - (OFString *)JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options; @end OF_ASSUME_NONNULL_END |
Modified src/OFNull.m from [252ff4815f] to [f75550a742].
︙ | ︙ | |||
19 20 21 22 23 24 25 | #import "OFString.h" #import "OFXMLElement.h" #import "OFData.h" #import "OFInvalidArgumentException.h" @interface OFNull () | | > | | 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | #import "OFString.h" #import "OFXMLElement.h" #import "OFData.h" #import "OFInvalidArgumentException.h" @interface OFNull () - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth; @end static OFNull *null = nil; @implementation OFNull + (void)initialize { |
︙ | ︙ | |||
83 84 85 86 87 88 89 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } | | > | > | 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } - (OFString *)JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options { return [self of_JSONRepresentationWithOptions: options depth: 0]; } - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth { return @"null"; } - (OFData *)messagePackRepresentation { |
︙ | ︙ |
Modified src/OFNumber.m from [fbce14e70c] to [74384103b7].
︙ | ︙ | |||
27 28 29 30 31 32 33 | #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFOutOfRangeException.h" @interface OFNumber () + (instancetype)of_alloc; | | > | | 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #import "OFInvalidArgumentException.h" #import "OFInvalidFormatException.h" #import "OFOutOfRangeException.h" @interface OFNumber () + (instancetype)of_alloc; - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth; @end @interface OFNumberPlaceholder: OFNumber @end @interface OFNumberSingleton: OFNumber @end |
︙ | ︙ | |||
1069 1070 1071 1072 1073 1074 1075 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } | | > | > | | | 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } - (OFString *)JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options { return [self of_JSONRepresentationWithOptions: options depth: 0]; } - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth { double doubleValue; if (*self.objCType == 'B') return (self.boolValue ? @"true" : @"false"); doubleValue = self.doubleValue; if (isinf(doubleValue)) { if (options & OFJSONRepresentationOptionJSON5) { if (doubleValue > 0) return @"Infinity"; else return @"-Infinity"; } else @throw [OFInvalidArgumentException exception]; } |
︙ | ︙ |
Modified src/OFString.m from [d072a99f4c] to [1ee38b4496].
︙ | ︙ | |||
89 90 91 92 93 94 95 | @interface OFString () - (size_t)of_getCString: (char *)cString maxLength: (size_t)maxLength encoding: (OFStringEncoding)encoding lossy: (bool)lossy OF_DIRECT; - (const char *)of_cStringWithEncoding: (OFStringEncoding)encoding lossy: (bool)lossy OF_DIRECT; | | > | | 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | @interface OFString () - (size_t)of_getCString: (char *)cString maxLength: (size_t)maxLength encoding: (OFStringEncoding)encoding lossy: (bool)lossy OF_DIRECT; - (const char *)of_cStringWithEncoding: (OFStringEncoding)encoding lossy: (bool)lossy OF_DIRECT; - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth; @end @interface OFStringPlaceholder: OFString @end extern bool of_unicode_to_iso_8859_2(const OFUnichar *, unsigned char *, size_t, bool); |
︙ | ︙ | |||
1707 1708 1709 1710 1711 1712 1713 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } | | > | > | | | | 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 | } - (OFString *)JSONRepresentation { return [self of_JSONRepresentationWithOptions: 0 depth: 0]; } - (OFString *)JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options { return [self of_JSONRepresentationWithOptions: options depth: 0]; } - (OFString *) of_JSONRepresentationWithOptions: (OFJSONRepresentationOptions)options depth: (size_t)depth { OFMutableString *JSON = [[self mutableCopy] autorelease]; /* FIXME: This is slow! Write it in pure C! */ [JSON replaceOccurrencesOfString: @"\\" 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 & OFJSONRepresentationOptionJSON5) { [JSON replaceOccurrencesOfString: @"\n" withString: @"\\\n"]; if (options & OFJSONRepresentationOptionIsIdentifier) { const char *cString = self.UTF8String; if ((!of_ascii_isalpha(cString[0]) && cString[0] != '_' && cString[0] != '$') || strpbrk(cString, " \n\r\t\b\f\\\"'") != NULL) { [JSON prependString: @"\""]; [JSON appendString: @"\""]; |
︙ | ︙ |
Modified tests/OFJSONTests.m from [1509791c9a] to [15d8e0cc74].
︙ | ︙ | |||
37 38 39 40 41 42 43 | nil]; TEST(@"-[objectByParsingJSON] #1", [s.objectByParsingJSON isEqual: d]) TEST(@"-[JSONRepresentation]", [[d JSONRepresentation] isEqual: @"{\"x\":[0.5,15,null,\"foo\",false],\"foo\":\"b\\na\\r\"}"]) | | | | | | 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | nil]; TEST(@"-[objectByParsingJSON] #1", [s.objectByParsingJSON isEqual: d]) TEST(@"-[JSONRepresentation]", [[d JSONRepresentation] isEqual: @"{\"x\":[0.5,15,null,\"foo\",false],\"foo\":\"b\\na\\r\"}"]) TEST(@"OFJSONRepresentationOptionPretty", [[d JSONRepresentationWithOptions: OFJSONRepresentationOptionPretty] isEqual: @"{\n\t\"x\": [\n\t\t0.5,\n\t\t15,\n\t\tnull,\n\t\t" @"\"foo\",\n\t\tfalse\n\t],\n\t\"foo\": \"b\\na\\r\"\n}"]) TEST(@"OFJSONRepresentationOptionJSON5", [[d JSONRepresentationWithOptions: OFJSONRepresentationOptionJSON5] isEqual: @"{x:[0.5,15,null,\"foo\",false],foo:\"b\\\na\\r\"}"]) EXPECT_EXCEPTION(@"-[objectByParsingJSON] #2", OFInvalidJSONException, [@"{" objectByParsingJSON]) EXPECT_EXCEPTION(@"-[objectByParsingJSON] #3", OFInvalidJSONException, [@"]" objectByParsingJSON]) EXPECT_EXCEPTION(@"-[objectByParsingJSON] #4", OFInvalidJSONException, |
︙ | ︙ |