Adds JSONHelper methods to read dictionaries.
This commit is contained in:
@@ -20,6 +20,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
+(int)readIntFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(int)defaultValue;
|
||||
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key;
|
||||
+(BOOL)readBoolFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(BOOL)defaultValue;
|
||||
+(NSDictionary*)readDictionaryFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key;
|
||||
+(NSDictionary*)readDictionaryFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSDictionary* _Nullable)defaultValue;
|
||||
|
||||
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
||||
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSString* _Nullable)defaultValue;
|
||||
@@ -29,6 +31,8 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
+(int)readIntFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(int)defaultValue;
|
||||
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
||||
+(BOOL)readBoolFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(BOOL)defaultValue;
|
||||
+(NSDictionary*)readDictionaryFromArray:(NSArray*)array forIndex:(NSUInteger)index;
|
||||
+(NSDictionary*)readDictionaryFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSDictionary* _Nullable)defaultValue;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -87,6 +87,19 @@ BOOL coerceObjectToBool(NSObject *object, BOOL defaultValue) {
|
||||
return coerceObjectToBool(object, defaultValue);
|
||||
}
|
||||
|
||||
+(NSDictionary*)readDictionaryFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key {
|
||||
return [JSONHelper readDictionaryFromDictionary:dictionary forKey:key withDefaultValue:nil];
|
||||
}
|
||||
|
||||
+(NSDictionary*)readDictionaryFromDictionary:(NSDictionary*)dictionary forKey:(NSString*)key withDefaultValue:(NSDictionary* _Nullable)defaultValue {
|
||||
NSObject *object = [dictionary objectForKey:key];
|
||||
if ([object isKindOfClass:[NSDictionary class]]) {
|
||||
return (NSDictionary*)object;
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
+(NSString*)readStringFromArray:(NSArray*)array forIndex:(NSUInteger)index{
|
||||
return [JSONHelper readStringFromArray:array forIndex:index withDefaultValue:nil];
|
||||
}
|
||||
@@ -123,4 +136,17 @@ BOOL coerceObjectToBool(NSObject *object, BOOL defaultValue) {
|
||||
return coerceObjectToBool(object, defaultValue);
|
||||
}
|
||||
|
||||
+(NSDictionary*)readDictionaryFromArray:(NSArray*)array forIndex:(NSUInteger)index {
|
||||
return [JSONHelper readDictionaryFromArray:array forIndex:index withDefaultValue:nil];
|
||||
}
|
||||
|
||||
+(NSDictionary*)readDictionaryFromArray:(NSArray*)array forIndex:(NSUInteger)index withDefaultValue:(NSDictionary* _Nullable)defaultValue {
|
||||
NSObject *object = [array objectAtIndex:index];
|
||||
if ([object isKindOfClass:[NSDictionary class]]) {
|
||||
return (NSDictionary*)object;
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
@@ -23,6 +23,10 @@
|
||||
NSString *_jsonBooleanKey;
|
||||
BOOL _jsonBooleanValue;
|
||||
NSString *_jsonBooleanFragment;
|
||||
NSString *_jsonDictionaryKey;
|
||||
NSDictionary *_jsonDictionaryValue;
|
||||
NSString *_jsonDictionaryFragment;
|
||||
NSString *_jsonDictionaryStringValue;
|
||||
}
|
||||
|
||||
NSString* escapeStringForJSON(NSString *unescaped) {
|
||||
@@ -59,6 +63,10 @@ NSArray* readJSONArrayFromString(NSString *jsonString) {
|
||||
_jsonBooleanKey = @"my_bool";
|
||||
_jsonBooleanValue = YES;
|
||||
_jsonBooleanFragment = [NSString stringWithFormat:@"\"%@\":true", escapeStringForJSON(_jsonBooleanKey)];
|
||||
_jsonDictionaryKey = @"my_dictionary";
|
||||
_jsonDictionaryValue = [NSDictionary dictionaryWithObjectsAndKeys:_jsonStringValue, _jsonStringKey, nil];
|
||||
_jsonDictionaryStringValue = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
|
||||
_jsonDictionaryFragment = [NSString stringWithFormat:@"\"%@\":%@", escapeStringForJSON(_jsonDictionaryKey), _jsonDictionaryStringValue];
|
||||
}
|
||||
|
||||
- (void)tearDown {
|
||||
@@ -242,6 +250,54 @@ NSArray* readJSONArrayFromString(NSString *jsonString) {
|
||||
XCTAssertEqual(0, readNumber);
|
||||
}
|
||||
|
||||
#pragma mark - Dictionaries in Dictionaries
|
||||
|
||||
- (void)testReadDictionaryFromDictionaryReturnsNilIfKeyNotPresent {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
|
||||
NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey];
|
||||
XCTAssertNil(readValue);
|
||||
}
|
||||
|
||||
- (void)testReadDictionaryFromDictionaryWithDefaultReturnsDefaultIfKeyNotPresent{
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonStringFragment];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey withDefaultValue:@{}];
|
||||
XCTAssertEqualObjects(@{}, readValue);
|
||||
}
|
||||
|
||||
- (void)testReadDictionaryFromDictionaryReturnsCorrectValue {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonDictionaryFragment];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey];
|
||||
XCTAssertEqualObjects(_jsonDictionaryValue, readValue);
|
||||
}
|
||||
|
||||
- (void)testReadDictionaryFromDictionaryWithDefaultReturnsCorrectValue {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{%@}", _jsonIntegerFragment];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey withDefaultValue:@{}];
|
||||
XCTAssertEqualObjects(@{}, readValue);
|
||||
}
|
||||
|
||||
- (void)testReadDictionaryFromDictionaryReturnsNilIfWrongType {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"{\"%@\":\"%@\"}", _jsonDictionaryKey, _jsonStringValue];
|
||||
NSDictionary *jsonRoot = readJSONDictionaryFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
NSDictionary *readValue = [JSONHelper readDictionaryFromDictionary:jsonRoot forKey:_jsonDictionaryKey];
|
||||
XCTAssertNil(readValue);
|
||||
}
|
||||
|
||||
#pragma mark - Integers in Arrays
|
||||
|
||||
- (void)testReadIntegerFromArrayReturnsNilIfNotAnInteger {
|
||||
@@ -404,4 +460,43 @@ NSArray* readJSONArrayFromString(NSString *jsonString) {
|
||||
XCTAssertEqual(_jsonBooleanValue, readValue);
|
||||
}
|
||||
|
||||
#pragma mark - Dictionaries in Arrays
|
||||
|
||||
- (void)testReadDictionaryFromArrayReturnsNilIfNotCoercable {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
|
||||
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
NSDictionary *readValue = [JSONHelper readDictionaryFromArray:jsonRoot forIndex:0];
|
||||
XCTAssertNil(readValue);
|
||||
}
|
||||
|
||||
- (void)testReadDictionaryFromArrayWithDefaultReturnsDefaultValueIfNotCoercable {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"[\"%@\"]", _jsonStringValue];
|
||||
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
NSDictionary *readValue = [JSONHelper readDictionaryFromArray:jsonRoot forIndex:0 withDefaultValue:@{}];
|
||||
XCTAssertEqualObjects(@{}, readValue);
|
||||
}
|
||||
|
||||
- (void)testReadDictionaryFromArrayThrowsIfIndexOutOfRange {
|
||||
// TODO: Decide if this should throw or return nil
|
||||
NSString *jsonString = @"[]";
|
||||
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
XCTAssertThrows([JSONHelper readDictionaryFromArray:jsonRoot forIndex:0]);
|
||||
XCTAssertThrows([JSONHelper readDictionaryFromArray:jsonRoot forIndex:-1]);
|
||||
}
|
||||
|
||||
- (void)testReadDictionaryFromArrayReturnsCorrectValue {
|
||||
NSString *jsonString = [NSString stringWithFormat:@"[%@]", _jsonDictionaryStringValue];
|
||||
NSArray *jsonRoot = readJSONArrayFromString(jsonString);
|
||||
XCTAssertNotNil(jsonRoot);
|
||||
|
||||
NSDictionary *readValue = [JSONHelper readDictionaryFromArray:jsonRoot forIndex:0];
|
||||
XCTAssertEqualObjects(_jsonDictionaryValue, readValue);
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user