Skip to content

Commit

Permalink
Fixed crash if the key for an object mapping is not in the JSON dicti…
Browse files Browse the repository at this point in the history
…onary.

Also removed a warning from the view controller.
  • Loading branch information
zc committed Aug 1, 2012
1 parent 1f09c91 commit 631754c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 9 deletions.
6 changes: 0 additions & 6 deletions JTObjectMapping/JTObjectMappingViewController.m
Expand Up @@ -28,12 +28,6 @@ - (void)viewDidLoad
}
*/

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
Expand Down
11 changes: 10 additions & 1 deletion JTObjectMapping/Source/NSObject+JTObjectMapping.m
Expand Up @@ -106,7 +106,10 @@ - (void)setValueFromDictionary:(NSDictionary *)dict mapping:(NSDictionary *)mapp
#if ! JTOBJECTMAPPING_DISABLE_KEYPATH_SUPPORT
[notMapped enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
id value = [dict valueForKeyPath:key];
[self setValue:value forKey:obj];
// Only set the property value if we have one to set -- otherwise this will crash
if (value != nil) {
[self setValue:value forKey:obj];
}
}];
#endif

Expand All @@ -117,12 +120,18 @@ - (void)setValueFromDictionary:(NSDictionary *)dict mapping:(NSDictionary *)mapp
return [JTMappings mappingWithKey:key targetClass:[self class] mapping:mapping];
}

/*
Instantiate and populate the properties of this class with the JTValidJSONResponse (NSDictionary).
If this is a dictionary or array, recurse into the json dict and create the corresponding child objects.
*/
+ (id)objectFromJSONObject:(id<JTValidJSONResponse>)object mapping:(NSDictionary *)mapping {
id returnObject = nil;
if ([object isKindOfClass:[NSDictionary class]]) {
// the json object is a dict -- create a new dict with the objects we can map from its contents
returnObject = [[[[self class] alloc] init] autorelease];
[returnObject setValueFromDictionary:(NSDictionary *)object mapping:mapping];
} else if ([object isKindOfClass:[NSArray class]]) {
// the json object is an array -- create a new array with the objects we can map from its contents
NSMutableArray *array = [NSMutableArray array];
for (NSObject *dict in (NSArray *)object) {
NSParameterAssert([dict isKindOfClass:[NSDictionary class]]);
Expand Down
12 changes: 11 additions & 1 deletion JTObjectMappingTests/JTObjectMappingTests.m
Expand Up @@ -152,7 +152,10 @@ - (void)setUp
@"nullArray", @"null_array",
@"nullSet", @"null_set",
@"nullNumber", @"null_number",

// missing auto-mapping -- this key doesn't exist in the json, which is fine
@"missingString", @"missingString",
// missing class-mapping -- this key doesn't exist in the json, which is fine
[NSDate mappingWithKey:@"missingDate" divisorForSeconds:1], @"missingDate",
nil];

self.object = [JTUserTest objectFromJSONObject:json mapping:mapping];
Expand All @@ -172,6 +175,7 @@ - (void)tearDown
// NSLog(@"%@", self.json);
//}


- (void)testTitle {
STAssertTrue([self.object.title isEqual:@"Manager"], @"title = %@ fails to equal %@", self.object.title, @"Manager");
}
Expand Down Expand Up @@ -249,6 +253,12 @@ - (void)testKeyPath {
STAssertEqualObjects(self.object.hashedString, @"string", nil, nil);
}

- (void)testMissingJSON {
STAssertNil(self.object.missingString, @"missingString should be nil");
STAssertNil(self.object.missingDate, @"missingDate should be nil");
}


//- (void)testAutoMapping {
// STAssertEqualObjects(self.object.autoString, @"yes", nil, nil);
// NSArray *array = [NSArray arrayWithObjects:
Expand Down
2 changes: 2 additions & 0 deletions JTObjectMappingTests/JTUserTest.h
Expand Up @@ -28,6 +28,8 @@
@property (nonatomic, copy) NSString *autoString;
@property (nonatomic, retain) NSArray *autoArray;
@property (nonatomic, retain) NSArray *nestedArray;
@property (nonatomic, retain) NSArray *missingString;
@property (nonatomic, retain) NSArray *missingDate;
//@property (nonatomic, retain) JTSocialNetworkTest *autoSocialNetwork;

@property (nonatomic, retain) JTSocialNetworkTest *socialNetwork;
Expand Down
2 changes: 1 addition & 1 deletion JTObjectMappingTests/JTUserTest.m
Expand Up @@ -20,7 +20,7 @@ @implementation JTUserTest
@synthesize data, dataLossy;
@synthesize favoriteColors;
@synthesize hashedString;

@synthesize missingDate, missingString;
@synthesize null, nullDate, nullArray, nullSet, nullChild, nullNumber;

@end

0 comments on commit 631754c

Please sign in to comment.