// // JSONKeyMapper.h // JSONModel // #import typedef NSString *(^JSONModelKeyMapBlock)(NSString *keyName); /** * **You won't need to create or store instances of this class yourself.** If you want your model * to have different property names than the JSON feed keys, look below on how to * make your model use a key mapper. * * For example if you consume JSON from twitter * you get back underscore_case style key names. For example: * *
"profile_sidebar_border_color": "0094C2",
 * "profile_background_tile": false,
* * To comply with Obj-C accepted camelCase property naming for your classes, * you need to provide mapping between JSON keys and ObjC property names. * * In your model overwrite the + (JSONKeyMapper *)keyMapper method and provide a JSONKeyMapper * instance to convert the key names for your model. * * If you need custom mapping it's as easy as: *
 * + (JSONKeyMapper *)keyMapper {
 *   return [[JSONKeyMapper alloc] initWithDictionary:@{@"crazy_JSON_name":@"myCamelCaseName"}];
 * }
 * 
* In case you want to handle underscore_case, **use the predefined key mapper**, like so: *
 * + (JSONKeyMapper *)keyMapper {
 *   return [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase];
 * }
 * 
*/ @interface JSONKeyMapper : NSObject // deprecated @property (readonly, nonatomic) JSONModelKeyMapBlock JSONToModelKeyBlock DEPRECATED_ATTRIBUTE; - (NSString *)convertValue:(NSString *)value isImportingToModel:(BOOL)importing DEPRECATED_MSG_ATTRIBUTE("use convertValue:"); - (instancetype)initWithDictionary:(NSDictionary *)map DEPRECATED_MSG_ATTRIBUTE("use initWithModelToJSONDictionary:"); - (instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel modelToJSONBlock:(JSONModelKeyMapBlock)toJSON DEPRECATED_MSG_ATTRIBUTE("use initWithModelToJSONBlock:"); + (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; /** Combined converter method * @param value the source name * @return JSONKeyMapper instance */ - (NSString *)convertValue:(NSString *)value; /** @name Creating a key mapper */ /** * Creates a JSONKeyMapper instance, based on the block you provide this initializer. * 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 */ - (instancetype)initWithModelToJSONBlock:(JSONModelKeyMapBlock)toJSON; /** * Creates a JSONKeyMapper instance, based on the mapping you provide. * Use your JSONModel property names as keys, and the JSON key names as values. * @param toJSON map dictionary, in the format:
@{@"myCamelCaseName":@"crazy_JSON_name"}
* @return JSONKeyMapper instance */ - (instancetype)initWithModelToJSONDictionary:(NSDictionary *)toJSON; /** * Given a camelCase model property, this mapper finds JSON keys using the snake_case equivalent. */ + (instancetype)mapperForSnakeCase; /** * Given a camelCase model property, this mapper finds JSON keys using the TitleCase equivalent. */ + (instancetype)mapperForTitleCase; /** * Creates a JSONKeyMapper based on a built-in JSONKeyMapper, with specific exceptions. * Use your JSONModel property names as keys, and the JSON key names as values. */ + (instancetype)baseMapper:(JSONKeyMapper *)baseKeyMapper withModelToJSONExceptions:(NSDictionary *)toJSON; @end