Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Catch key path exceptions

  • Loading branch information...
commit 0644728d407cb7efcb9b78d3156f44d9b01c24e9 1 parent d944064
@robb robb authored
Showing with 13 additions and 16 deletions.
  1. +13 −16 Mantle/MTLJSONAdapter.m
View
29 Mantle/MTLJSONAdapter.m
@@ -106,24 +106,21 @@ - (id)initWithJSONDictionary:(NSDictionary *)JSONDictionary modelClass:(Class)mo
NSString *JSONKeyPath = [self JSONKeyPathForPropertyKey:propertyKey];
if (JSONKeyPath == nil) continue;
- id value = JSONDictionary;
- NSArray *JSONKeyPathComponents = [JSONKeyPath componentsSeparatedByString:@"."];
- for (NSString *itemJSONKeyPathComponent in JSONKeyPathComponents) {
- if (![value isKindOfClass:NSDictionary.class]) {
- if (error != NULL) {
- NSDictionary *userInfo = @{
- NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSON dictionary", @""),
- NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%@ could not be parsed because an invalid JSON dictionary was provided for key path \"%@\"", @""), modelClass, JSONKeyPath],
- };
-
- *error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo];
- }
-
- return nil;
+ id value;
+ @try {
+ value = [JSONDictionary valueForKeyPath:JSONKeyPath];
+ } @catch (NSException *ex) {
+ if (error != NULL) {
+ NSDictionary *userInfo = @{
+ NSLocalizedDescriptionKey: NSLocalizedString(@"Invalid JSON dictionary", nil),
+ NSLocalizedFailureReasonErrorKey: [NSString stringWithFormat:NSLocalizedString(@"%1$@ could not be parsed because an invalid JSON dictionary was provided for key path \"%2$@\"", nil), modelClass, JSONKeyPath],
+ MTLJSONAdapterThrownExceptionErrorKey: ex
+ };
+
+ *error = [NSError errorWithDomain:MTLJSONAdapterErrorDomain code:MTLJSONAdapterErrorInvalidJSONDictionary userInfo:userInfo];
}
- value = [value valueForKey:itemJSONKeyPathComponent];
- if (value == nil || value == NSNull.null) break;
+ return nil;
}
if (value == nil) continue;
Please sign in to comment.
Something went wrong with that request. Please try again.