JSONKeyMapper.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // JSONKeyMapper.h
  3. // JSONModel
  4. //
  5. #import <Foundation/Foundation.h>
  6. typedef NSString *(^JSONModelKeyMapBlock)(NSString *keyName);
  7. /**
  8. * **You won't need to create or store instances of this class yourself.** If you want your model
  9. * to have different property names than the JSON feed keys, look below on how to
  10. * make your model use a key mapper.
  11. *
  12. * For example if you consume JSON from twitter
  13. * you get back underscore_case style key names. For example:
  14. *
  15. * <pre>"profile_sidebar_border_color": "0094C2",
  16. * "profile_background_tile": false,</pre>
  17. *
  18. * To comply with Obj-C accepted camelCase property naming for your classes,
  19. * you need to provide mapping between JSON keys and ObjC property names.
  20. *
  21. * In your model overwrite the + (JSONKeyMapper *)keyMapper method and provide a JSONKeyMapper
  22. * instance to convert the key names for your model.
  23. *
  24. * If you need custom mapping it's as easy as:
  25. * <pre>
  26. * + (JSONKeyMapper *)keyMapper {
  27. * &nbsp; return [[JSONKeyMapper&nbsp;alloc]&nbsp;initWithDictionary:@{@"crazy_JSON_name":@"myCamelCaseName"}];
  28. * }
  29. * </pre>
  30. * In case you want to handle underscore_case, **use the predefined key mapper**, like so:
  31. * <pre>
  32. * + (JSONKeyMapper *)keyMapper {
  33. * &nbsp; return [JSONKeyMapper&nbsp;mapperFromUnderscoreCaseToCamelCase];
  34. * }
  35. * </pre>
  36. */
  37. @interface JSONKeyMapper : NSObject
  38. // deprecated
  39. @property (readonly, nonatomic) JSONModelKeyMapBlock JSONToModelKeyBlock DEPRECATED_ATTRIBUTE;
  40. - (NSString *)convertValue:(NSString *)value isImportingToModel:(BOOL)importing DEPRECATED_MSG_ATTRIBUTE("use convertValue:");
  41. - (instancetype)initWithDictionary:(NSDictionary *)map DEPRECATED_MSG_ATTRIBUTE("use initWithModelToJSONDictionary:");
  42. - (instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel modelToJSONBlock:(JSONModelKeyMapBlock)toJSON DEPRECATED_MSG_ATTRIBUTE("use initWithModelToJSONBlock:");
  43. + (instancetype)mapper:(JSONKeyMapper *)baseKeyMapper withExceptions:(NSDictionary *)exceptions DEPRECATED_MSG_ATTRIBUTE("use baseMapper:withModelToJSONExceptions:");
  44. + (instancetype)mapperFromUnderscoreCaseToCamelCase DEPRECATED_MSG_ATTRIBUTE("use mapperForSnakeCase:");
  45. + (instancetype)mapperFromUpperCaseToLowerCase DEPRECATED_ATTRIBUTE;
  46. /** @name Name converters */
  47. /** Block, which takes in a property name and converts it to the corresponding JSON key name */
  48. @property (readonly, nonatomic) JSONModelKeyMapBlock modelToJSONKeyBlock;
  49. /** Combined converter method
  50. * @param value the source name
  51. * @return JSONKeyMapper instance
  52. */
  53. - (NSString *)convertValue:(NSString *)value;
  54. /** @name Creating a key mapper */
  55. /**
  56. * Creates a JSONKeyMapper instance, based on the block you provide this initializer.
  57. * The parameter takes in a JSONModelKeyMapBlock block:
  58. * <pre>NSString *(^JSONModelKeyMapBlock)(NSString *keyName)</pre>
  59. * The block takes in a string and returns the transformed (if at all) string.
  60. * @param toJSON transforms your model property name to a JSON key
  61. */
  62. - (instancetype)initWithModelToJSONBlock:(JSONModelKeyMapBlock)toJSON;
  63. /**
  64. * Creates a JSONKeyMapper instance, based on the mapping you provide.
  65. * Use your JSONModel property names as keys, and the JSON key names as values.
  66. * @param toJSON map dictionary, in the format: <pre>@{@"myCamelCaseName":@"crazy_JSON_name"}</pre>
  67. * @return JSONKeyMapper instance
  68. */
  69. - (instancetype)initWithModelToJSONDictionary:(NSDictionary <NSString *, NSString *> *)toJSON;
  70. /**
  71. * Given a camelCase model property, this mapper finds JSON keys using the snake_case equivalent.
  72. */
  73. + (instancetype)mapperForSnakeCase;
  74. /**
  75. * Given a camelCase model property, this mapper finds JSON keys using the TitleCase equivalent.
  76. */
  77. + (instancetype)mapperForTitleCase;
  78. /**
  79. * Creates a JSONKeyMapper based on a built-in JSONKeyMapper, with specific exceptions.
  80. * Use your JSONModel property names as keys, and the JSON key names as values.
  81. */
  82. + (instancetype)baseMapper:(JSONKeyMapper *)baseKeyMapper withModelToJSONExceptions:(NSDictionary *)toJSON;
  83. @end