diff --git a/Examples/Tests/Models/Implementations/GitHubKeyMapRepoModel.m b/Examples/Tests/Models/Implementations/GitHubKeyMapRepoModel.m index 9f3586cf..0bf114df 100644 --- a/Examples/Tests/Models/Implementations/GitHubKeyMapRepoModel.m +++ b/Examples/Tests/Models/Implementations/GitHubKeyMapRepoModel.m @@ -12,12 +12,12 @@ @implementation GitHubKeyMapRepoModel +(JSONKeyMapper*)keyMapper { - return [[JSONKeyMapper alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName) + return [[JSONKeyMapper alloc] initWithKeyMappingBlock:^NSString *(Class cls, NSString *modelKey) { - if ([keyName isEqual:@"__description"]) + if ([modelKey isEqual:@"__description"]) return @"description"; else - return keyName; + return modelKey; }]; } diff --git a/JSONModel/JSONModel/JSONModel.m b/JSONModel/JSONModel/JSONModel.m index 6ad83688..c9f96859 100644 --- a/JSONModel/JSONModel/JSONModel.m +++ b/JSONModel/JSONModel/JSONModel.m @@ -211,7 +211,7 @@ -(BOOL)__doesDictionary:(NSDictionary*)dict matchModelWithKeyMapper:(JSONKeyMapp //loop over the required properties list for (JSONModelClassProperty* property in [self __properties__]) { - transformedName = (keyMapper||globalKeyMapper) ? [self __mapString:property.name withKeyMapper:keyMapper] : property.name; + transformedName = (keyMapper||globalKeyMapper) ? [self __mapString:property.name forClass:[self class] withKeyMapper:keyMapper] : property.name; //check if exists and if so, add to incoming keys id value; @@ -251,18 +251,18 @@ -(BOOL)__doesDictionary:(NSDictionary*)dict matchModelWithKeyMapper:(JSONKeyMapp return YES; } --(NSString*)__mapString:(NSString*)string withKeyMapper:(JSONKeyMapper*)keyMapper +-(NSString*)__mapString:(NSString*)string forClass:(Class)cls withKeyMapper:(JSONKeyMapper*)keyMapper { if (keyMapper) { //custom mapper - NSString* mappedName = [keyMapper convertValue:string]; + NSString* mappedName = [keyMapper convertValue:string forClass:cls]; if (globalKeyMapper && [mappedName isEqualToString: string]) { - mappedName = [globalKeyMapper convertValue:string]; + mappedName = [globalKeyMapper convertValue:string forClass:cls]; } string = mappedName; } else if (globalKeyMapper) { //global keymapper - string = [globalKeyMapper convertValue:string]; + string = [globalKeyMapper convertValue:string forClass:cls]; } return string; @@ -274,7 +274,7 @@ -(BOOL)__importDictionary:(NSDictionary*)dict withKeyMapper:(JSONKeyMapper*)keyM for (JSONModelClassProperty* property in [self __properties__]) { //convert key name to model keys, if a mapper is provided - NSString* jsonKeyPath = (keyMapper||globalKeyMapper) ? [self __mapString:property.name withKeyMapper:keyMapper] : property.name; + NSString* jsonKeyPath = (keyMapper||globalKeyMapper) ? [self __mapString:property.name forClass:[self class] withKeyMapper:keyMapper] : property.name; //JMLog(@"keyPath: %@", jsonKeyPath); //general check for data type compliance @@ -930,7 +930,7 @@ -(NSDictionary*)toDictionaryWithKeys:(NSArray*)propertyNames continue; //fetch key and value - NSString* keyPath = (self.__keyMapper||globalKeyMapper) ? [self __mapString:p.name withKeyMapper:self.__keyMapper] : p.name; + NSString* keyPath = (self.__keyMapper||globalKeyMapper) ? [self __mapString:p.name forClass:[self class] withKeyMapper:self.__keyMapper] : p.name; value = [self valueForKey: p.name]; //JMLog(@"toDictionary[%@]->[%@] = '%@'", p.name, keyPath, value); diff --git a/JSONModel/JSONModelTransformations/JSONKeyMapper.h b/JSONModel/JSONModelTransformations/JSONKeyMapper.h index 35e44a84..1d8a9b68 100644 --- a/JSONModel/JSONModelTransformations/JSONKeyMapper.h +++ b/JSONModel/JSONModelTransformations/JSONKeyMapper.h @@ -5,7 +5,8 @@ #import -typedef NSString *(^JSONModelKeyMapBlock)(NSString *keyName); +typedef NSString *(^JSONModelKeyMapBlock)(NSString *keyName) DEPRECATED_MSG_ATTRIBUTE("use JSONModelKeyMappingBlock"); +typedef NSString *(^JSONModelKeyMappingBlock)(Class cls, NSString *modelKey); /** * **You won't need to create or store instances of this class yourself.** If you want your model @@ -41,22 +42,25 @@ typedef NSString *(^JSONModelKeyMapBlock)(NSString *keyName); // deprecated @property (readonly, nonatomic) JSONModelKeyMapBlock JSONToModelKeyBlock DEPRECATED_ATTRIBUTE; -- (NSString *)convertValue:(NSString *)value isImportingToModel:(BOOL)importing DEPRECATED_MSG_ATTRIBUTE("use convertValue:"); +@property (readonly, nonatomic) JSONModelKeyMapBlock modelToJSONKeyBlock DEPRECATED_MSG_ATTRIBUTE("use keyMappingBlock"); +- (NSString *)convertValue:(NSString *)value isImportingToModel:(BOOL)importing DEPRECATED_MSG_ATTRIBUTE("use convertValue:forClass:"); +- (NSString *)convertValue:(NSString *)value DEPRECATED_MSG_ATTRIBUTE("use convertValue:forClass:"); - (instancetype)initWithDictionary:(NSDictionary *)map DEPRECATED_MSG_ATTRIBUTE("use initWithModelToJSONDictionary:"); -- (instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel modelToJSONBlock:(JSONModelKeyMapBlock)toJSON DEPRECATED_MSG_ATTRIBUTE("use initWithModelToJSONBlock:"); +- (instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel modelToJSONBlock:(JSONModelKeyMapBlock)toJSON DEPRECATED_MSG_ATTRIBUTE("use initWithKeyMappingBlock:"); +- (instancetype)initWithModelToJSONBlock:(JSONModelKeyMapBlock)toJSON DEPRECATED_MSG_ATTRIBUTE("use initWithKeyMappingBlock:"); + (instancetype)mapper:(JSONKeyMapper *)baseKeyMapper withExceptions:(NSDictionary *)exceptions DEPRECATED_MSG_ATTRIBUTE("use baseMapper:withModelToJSONExceptions:"); + (instancetype)mapperFromUnderscoreCaseToCamelCase DEPRECATED_MSG_ATTRIBUTE("use mapperForSnakeCase:"); + (instancetype)mapperFromUpperCaseToLowerCase DEPRECATED_ATTRIBUTE; /** @name Name converters */ /** Block, which takes in a property name and converts it to the corresponding JSON key name */ -@property (readonly, nonatomic) JSONModelKeyMapBlock modelToJSONKeyBlock; +@property (readonly, nonatomic) JSONModelKeyMappingBlock keyMappingBlock; /** Combined converter method * @param value the source name * @return JSONKeyMapper instance */ -- (NSString *)convertValue:(NSString *)value; +- (NSString *)convertValue:(NSString *)value forClass:(Class)cls; /** @name Creating a key mapper */ @@ -65,9 +69,9 @@ typedef NSString *(^JSONModelKeyMapBlock)(NSString *keyName); * The parameter takes in a JSONModelKeyMapBlock block: *
NSString *(^JSONModelKeyMapBlock)(NSString *keyName)
* The block takes in a string and returns the transformed (if at all) string. - * @param toJSON transforms your model property name to a JSON key + * @param keyMappingBlock transforms your model property name to a JSON key */ -- (instancetype)initWithModelToJSONBlock:(JSONModelKeyMapBlock)toJSON; +- (instancetype)initWithKeyMappingBlock:(JSONModelKeyMappingBlock)keyMappingBlock; /** * Creates a JSONKeyMapper instance, based on the mapping you provide. diff --git a/JSONModel/JSONModelTransformations/JSONKeyMapper.m b/JSONModel/JSONModelTransformations/JSONKeyMapper.m index 9cdb8f27..8fda8702 100644 --- a/JSONModel/JSONModelTransformations/JSONKeyMapper.m +++ b/JSONModel/JSONModelTransformations/JSONKeyMapper.m @@ -13,11 +13,19 @@ - (instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel modelToJS } - (instancetype)initWithModelToJSONBlock:(JSONModelKeyMapBlock)toJSON +{ + return [self initWithKeyMappingBlock:^NSString *(Class cls, NSString *modelKey) + { + return toJSON(modelKey); + }]; +} + +- (instancetype)initWithKeyMappingBlock:(JSONModelKeyMappingBlock)keyMappingBlock { if (!(self = [self init])) return nil; - _modelToJSONKeyBlock = toJSON; + _keyMappingBlock = keyMappingBlock; return self; } @@ -34,9 +42,9 @@ - (instancetype)initWithModelToJSONDictionary:(NSDictionary *)toJSON if (!(self = [super init])) return nil; - _modelToJSONKeyBlock = ^NSString *(NSString *keyName) + _keyMappingBlock = ^NSString *(Class cls, NSString *modelKey) { - return [toJSON valueForKeyPath:keyName] ?: keyName; + return [toJSON valueForKeyPath:modelKey] ?: modelKey; }; return self; @@ -47,6 +55,14 @@ - (JSONModelKeyMapBlock)JSONToModelKeyBlock return nil; } +- (JSONModelKeyMapBlock)modelToJSONKeyBlock +{ + return ^NSString *(NSString *keyName) + { + return _keyMappingBlock(nil, keyName); + }; +} + + (NSDictionary *)swapKeysAndValuesInDictionary:(NSDictionary *)dictionary { NSArray *keys = dictionary.allKeys; @@ -57,12 +73,17 @@ + (NSDictionary *)swapKeysAndValuesInDictionary:(NSDictionary *)dictionary - (NSString *)convertValue:(NSString *)value isImportingToModel:(BOOL)importing { - return [self convertValue:value]; + return [self convertValue:value forClass:nil]; } - (NSString *)convertValue:(NSString *)value { - return _modelToJSONKeyBlock(value); + return [self convertValue:value forClass:nil]; +} + +- (NSString *)convertValue:(NSString *)value forClass:(Class)cls +{ + return _keyMappingBlock(cls, value); } + (instancetype)mapperFromUnderscoreCaseToCamelCase @@ -72,9 +93,9 @@ + (instancetype)mapperFromUnderscoreCaseToCamelCase + (instancetype)mapperForSnakeCase { - return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName) + return [[self alloc] initWithKeyMappingBlock:^NSString *(Class cls, NSString *modelKey) { - NSMutableString *result = [NSMutableString stringWithString:keyName]; + NSMutableString *result = [NSMutableString stringWithString:modelKey]; NSRange range; // handle upper case chars @@ -108,17 +129,17 @@ + (instancetype)mapperForSnakeCase + (instancetype)mapperForTitleCase { - return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName) + return [[self alloc] initWithKeyMappingBlock:^NSString *(Class cls, NSString *modelKey) { - return [keyName stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[keyName substringToIndex:1].uppercaseString]; + return [modelKey stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[modelKey substringToIndex:1].uppercaseString]; }]; } + (instancetype)mapperFromUpperCaseToLowerCase { - return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName) + return [[self alloc] initWithKeyMappingBlock:^NSString *(Class cls, NSString *modelKey) { - return keyName.uppercaseString; + return modelKey.uppercaseString; }]; } @@ -131,15 +152,15 @@ + (instancetype)mapper:(JSONKeyMapper *)baseKeyMapper withExceptions:(NSDictiona + (instancetype)baseMapper:(JSONKeyMapper *)baseKeyMapper withModelToJSONExceptions:(NSDictionary *)toJSON { - return [[self alloc] initWithModelToJSONBlock:^NSString *(NSString *keyName) + return [[self alloc] initWithKeyMappingBlock:^NSString *(Class cls, NSString *modelKey) { - if (!keyName) + if (!modelKey) return nil; - if (toJSON[keyName]) - return toJSON[keyName]; + if (toJSON[modelKey]) + return toJSON[modelKey]; - return baseKeyMapper.modelToJSONKeyBlock(keyName); + return baseKeyMapper.keyMappingBlock(cls, modelKey); }]; }