JSONKeyMapper.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //
  2. // JSONKeyMapper.h
  3. //
  4. // @version 1.2
  5. // @author Marin Todorov (http://www.underplot.com) and contributors
  6. //
  7. // Copyright (c) 2012-2015 Marin Todorov, Underplot ltd.
  8. // This code is distributed under the terms and conditions of the MIT license.
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  11. // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  13. //
  14. #import <Foundation/Foundation.h>
  15. typedef NSString* (^JSONModelKeyMapBlock)(NSString* keyName);
  16. /**
  17. * **You won't need to create or store instances of this class yourself.** If you want your model
  18. * to have different property names than the JSON feed keys, look below on how to
  19. * make your model use a key mapper.
  20. *
  21. * For example if you consume JSON from twitter
  22. * you get back underscore_case style key names. For example:
  23. *
  24. * <pre>"profile_sidebar_border_color": "0094C2",
  25. * "profile_background_tile": false,</pre>
  26. *
  27. * To comply with Obj-C accepted camelCase property naming for your classes,
  28. * you need to provide mapping between JSON keys and ObjC property names.
  29. *
  30. * In your model overwrite the +(JSONKeyMapper*)keyMapper method and provide a JSONKeyMapper
  31. * instance to convert the key names for your model.
  32. *
  33. * If you need custom mapping it's as easy as:
  34. * <pre>
  35. * +(JSONKeyMapper*)keyMapper {
  36. * &nbsp; return [[JSONKeyMapper&nbsp;alloc]&nbsp;initWithDictionary:@{@"crazy_JSON_name":@"myCamelCaseName"}];
  37. * }
  38. * </pre>
  39. * In case you want to handle underscore_case, **use the predefined key mapper**, like so:
  40. * <pre>
  41. * +(JSONKeyMapper*)keyMapper {
  42. * &nbsp; return [JSONKeyMapper&nbsp;mapperFromUnderscoreCaseToCamelCase];
  43. * }
  44. * </pre>
  45. */
  46. @interface JSONKeyMapper : NSObject
  47. /** @name Name converters */
  48. /** Block, which takes in a JSON key and converts it to the corresponding property name */
  49. @property (readonly, nonatomic) JSONModelKeyMapBlock JSONToModelKeyBlock;
  50. /** Block, which takes in a property name and converts it to the corresponding JSON key name */
  51. @property (readonly, nonatomic) JSONModelKeyMapBlock modelToJSONKeyBlock;
  52. /** Combined converter method
  53. * @param value the source name
  54. * @param importing YES invokes JSONToModelKeyBlock, NO - modelToJSONKeyBlock
  55. * @return JSONKeyMapper instance
  56. */
  57. -(NSString*)convertValue:(NSString*)value isImportingToModel:(BOOL)importing;
  58. /** @name Creating a key mapper */
  59. /**
  60. * Creates a JSONKeyMapper instance, based on the two blocks you provide this initializer.
  61. * The two parameters take in a JSONModelKeyMapBlock block:
  62. * <pre>NSString* (^JSONModelKeyMapBlock)(NSString* keyName)</pre>
  63. * The block takes in a string and returns the transformed (if at all) string.
  64. * @param toModel transforms JSON key name to your model property name
  65. * @param toJSON transforms your model property name to a JSON key
  66. */
  67. -(instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel
  68. modelToJSONBlock:(JSONModelKeyMapBlock)toJSON;
  69. /**
  70. * Creates a JSONKeyMapper instance, based on the mapping you provide
  71. * in the map parameter. Use the JSON key names as keys, your JSONModel
  72. * property names as values.
  73. * @param map map dictionary, in the format: <pre>@{@"crazy_JSON_name":@"myCamelCaseName"}</pre>
  74. * @return JSONKeyMapper instance
  75. */
  76. -(instancetype)initWithDictionary:(NSDictionary*)map;
  77. /**
  78. * Creates a JSONKeyMapper, which converts underscore_case to camelCase and vice versa.
  79. */
  80. +(instancetype)mapperFromUnderscoreCaseToCamelCase;
  81. +(instancetype)mapperFromUpperCaseToLowerCase;
  82. /**
  83. * Creates a JSONKeyMapper based on a built-in JSONKeyMapper, with specific exceptions.
  84. * Use the original JSON key names as keys, and your JSONModel property names as values.
  85. */
  86. + (instancetype)mapper:(JSONKeyMapper *)baseKeyMapper withExceptions:(NSDictionary *)exceptions;
  87. @end