Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: TouchCode/TouchJSON
...
head fork: TouchCode/TouchJSON
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 3 files changed
  • 0 commit comments
  • 1 contributor
View
2  Source/CJSONDeserializer.h
@@ -45,6 +45,7 @@ typedef enum {
kJSONDeserializerErrorCode_DictionaryKeyNotTerminated = -103,
kJSONDeserializerErrorCode_DictionaryValueScanFailed = -104,
kJSONDeserializerErrorCode_DictionaryNotTerminated = -106,
+ kJSONDeserializerErrorCode_DictionaryDuplicateKeyNotALlowed = -107,
// Array scanning
kJSONDeserializerErrorCode_ArrayStartCharacterMissing = -201,
@@ -70,6 +71,7 @@ enum {
kJSONDeserializationOptions_MutableLeaves = (1UL << 1),
kJSONDeserializationOptions_AllowFragments = (1UL << 2),
kJSONDeserializationOptions_LaxEscapeCodes = (1UL << 3),
+ kJSONDeserializationOptions_DuplicateKeysAreErrors = (1UL << 4),
kJSONDeserializationOptions_Default = kJSONDeserializationOptions_MutableContainers,
};
typedef NSUInteger EJSONDeserializationOptions;
View
10 Source/CJSONDeserializer.m
@@ -369,7 +369,15 @@ - (BOOL)_scanJSONDictionary:(NSDictionary **)outDictionary error:(NSError **)out
*outError = [self _error:kJSONDeserializerErrorCode_DictionaryKeyScanFailed description:@"Could not scan dictionary. Failed to scan a key."];
return(NO);
}
- CFDictionarySetValue((__bridge CFMutableDictionaryRef) theDictionary, (__bridge void *) theKey, (__bridge void *) theValue);
+ if (_options & kJSONDeserializationOptions_DuplicateKeysAreErrors && [theDictionary objectForKey:theKey] != NULL)
+ {
+ if (outError)
+ {
+ *outError = [self _error:kJSONDeserializerErrorCode_DictionaryDuplicateKeyNotALlowed description:@"Duplicate keys found."];
+ }
+ return (NO);
+ }
+ CFDictionarySetValue((__bridge CFMutableDictionaryRef)theDictionary, (__bridge void *)theKey, (__bridge void *)theValue);
}
_current = _SkipWhiteSpace(_current, _end);
View
14 Support/UnitTests/CJSONDeserializer_UnitTests.m
@@ -328,6 +328,20 @@ - (void)testDictionary_DupeKey2
STAssertEqualObjects(theDeseralizedValue, @{ @"key": @(0) }, @"");
}
+- (void)testDictionary_DupeKey3
+ {
+ NSString *theString = @"{ \"key\": 1, \"key\": 0 }";
+ NSData *theData = [theString dataUsingEncoding:NSUTF8StringEncoding];
+ CJSONDeserializer *theDeserializer = [CJSONDeserializer deserializer];
+ theDeserializer.options |= kJSONDeserializationOptions_DuplicateKeysAreErrors;
+ NSError *theError = NULL;
+ id theDeseralizedValue = [theDeserializer deserialize:theData error:&theError];
+ STAssertNil(theDeseralizedValue, @"This test should return nil");
+ STAssertNotNil(theError, @"This test should return an error");
+ }
+
+
+
- (void)testDictionary_InvalidKeys1
{
NSString *theString = @"{ 0: 0 }";

No commit comments for this range

Something went wrong with that request. Please try again.