Navigation Menu

Skip to content

Commit

Permalink
Ensure the type is as-desired when loading JSON
Browse files Browse the repository at this point in the history
So if you call the NSArray version, you want an array back or nil.
  • Loading branch information
mxcl committed Nov 30, 2012
1 parent d5678a1 commit 2135fc8
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions NSObject+mxcl.m
Expand Up @@ -12,12 +12,14 @@ - (void)dumpSelectors {
free(methods);
}

+ (id)objectWithJSONData:(NSData *)json {
return [NSJSONSerialization JSONObjectWithData:json options:kNilOptions error:nil];
+ (id)objectWithJSONData:(NSData *)data {
if (!data) return nil;
id o = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
return [o isKindOfClass:self] ? o : nil;
}

+ (id)objectWithJSONString:(NSString *)json {
return [NSObject objectWithJSONData:[json dataUsingEncoding:NSUTF8StringEncoding]];
+ (id)objectWithJSONString:(NSString *)jsonString {
return [self objectWithJSONData:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
}

@end
Expand Down

0 comments on commit 2135fc8

Please sign in to comment.