Created
August 12, 2014 16:04
-
-
Save dbachrach/4b73e88625edadcbde74 to your computer and use it in GitHub Desktop.
ConditionalJSONModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <JSONModel/JSONModel.h> | |
@interface ConditionalJsonModel : JSONModel | |
- (NSDictionary*)classMapping; | |
- (NSString*)classNameProperty; | |
- (BOOL)requiresExactMatch; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "ConditionalJsonModel.h" | |
@implementation ConditionalJsonModel | |
- (id)initWithDictionary:(NSDictionary *)dict error:(NSError *__autoreleasing *)err | |
{ | |
NSString* value = dict[[self classNameProperty]]; | |
NSDictionary* mapping = [self classMapping]; | |
Class class = nil; | |
if ([self requiresExactMatch]) { | |
class = mapping[value]; | |
} | |
else { | |
for (id key in mapping.allKeys) { | |
if ([value hasPrefix:key]) { | |
class = mapping[key]; | |
break; | |
} | |
} | |
} | |
if (!class) { | |
NSLog(@"!!Warning!! ConditionalJsonModel did not find a class for value: %@", value); | |
} | |
return [[class alloc] initWithDictionary:dict error:err]; | |
} | |
- (BOOL)requiresExactMatch | |
{ | |
return YES; | |
} | |
- (NSDictionary*)classMapping | |
{ | |
return nil; | |
} | |
- (NSString*)classNameProperty | |
{ | |
return nil; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment