Skip to content

Instantly share code, notes, and snippets.

@dbachrach
Created August 12, 2014 16:04
Show Gist options
  • Save dbachrach/4b73e88625edadcbde74 to your computer and use it in GitHub Desktop.
Save dbachrach/4b73e88625edadcbde74 to your computer and use it in GitHub Desktop.
ConditionalJSONModel
#import <Foundation/Foundation.h>
#import <JSONModel/JSONModel.h>
@interface ConditionalJsonModel : JSONModel
- (NSDictionary*)classMapping;
- (NSString*)classNameProperty;
- (BOOL)requiresExactMatch;
@end
#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