Permalink
Browse files

Ensure the type is as-desired when loading JSON

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 2135fc88a0eb55a5fe42b9bf1fe77ae76abf1510
Showing with 6 additions and 4 deletions.
  1. +6 −4 NSObject+mxcl.m
View
@@ -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

0 comments on commit 2135fc8

Please sign in to comment.