JSONKeyMapper.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // JSONKeyMapper.h
  3. //
  4. // @version 1.0.0
  5. // @author Marin Todorov, http://www.touch-code-magazine.com
  6. //
  7. // Copyright (c) 2012-2014 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. // The MIT License in plain English: http://www.touch-code-magazine.com/JSONModel/MITLicense
  15. #import <Foundation/Foundation.h>
  16. typedef NSString* (^JSONModelKeyMapBlock)(NSString* keyName);
  17. /**
  18. * **You won't need to create or store instances of this class yourself.** If you want your model
  19. * to have different property names than the JSON feed keys, look below on how to
  20. * make your model use a key mapper.
  21. *
  22. * For example if you consume JSON from twitter
  23. * you get back underscore_case style key names. For example:
  24. *
  25. * <pre>"profile_sidebar_border_color": "0094C2",
  26. * "profile_background_tile": false,</pre>
  27. *
  28. * To comply with Obj-C accepted camelCase property naming for your classes,
  29. * you need to provide mapping between JSON keys and ObjC property names.
  30. *
  31. * In your model overwrite the +(JSONKeyMapper*)keyMapper method and provide a JSONKeyMapper
  32. * instance to convert the key names for your model.
  33. *
  34. * If you need custom mapping it's as easy as:
  35. * <pre>
  36. * +(JSONKeyMapper*)keyMapper {
  37. * &nbsp; return [[JSONKeyMapper&nbsp;alloc]&nbsp;initWithDictionary:@{@"crazy_JSON_name":@"myCamelCaseName"}];
  38. * }
  39. * </pre>
  40. * In case you want to handle underscore_case, **use the predefined key mapper**, like so:
  41. * <pre>
  42. * +(JSONKeyMapper*)keyMapper {
  43. * &nbsp; return [JSONKeyMapper&nbsp;mapperFromUnderscoreCaseToCamelCase];
  44. * }
  45. * </pre>
  46. */
  47. @interface JSONKeyMapper : NSObject
  48. /** @name Name convertors */
  49. /** Block, which takes in a JSON key and converts it to the corresponding property name */
  50. @property (readonly, nonatomic) JSONModelKeyMapBlock JSONToModelKeyBlock;
  51. /** Block, which takes in a property name and converts it to the corresponding JSON key name */
  52. @property (readonly, nonatomic) JSONModelKeyMapBlock modelToJSONKeyBlock;
  53. /** Combined convertor method
  54. * @param value the source name
  55. * @param importing YES invokes JSONToModelKeyBlock, NO - modelToJSONKeyBlock
  56. * @return JSONKeyMapper instance
  57. */
  58. -(NSString*)convertValue:(NSString*)value isImportingToModel:(BOOL)importing;
  59. /** @name Creating a key mapper */
  60. /**
  61. * Creates a JSONKeyMapper instance, based on the two blocks you provide this initializer.
  62. * The two parameters take in a JSONModelKeyMapBlock block:
  63. * <pre>NSString* (^JSONModelKeyMapBlock)(NSString* keyName)</pre>
  64. * The block takes in a string and returns the transformed (if at all) string.
  65. * @param toModel transforms JSON key name to your model property name
  66. * @param toJSON transforms your model property name to a JSON key
  67. */
  68. -(instancetype)initWithJSONToModelBlock:(JSONModelKeyMapBlock)toModel
  69. modelToJSONBlock:(JSONModelKeyMapBlock)toJSON;
  70. /**
  71. * Creates a JSONKeyMapper instance, based on the mapping you provide
  72. * in the map parameter. Use the JSON key names as keys, your JSONModel
  73. * property names as values.
  74. * @param map map dictionary, in the format: <pre>@{@"crazy_JSON_name":@"myCamelCaseName"}</pre>
  75. * @return JSONKeyMapper instance
  76. */
  77. -(instancetype)initWithDictionary:(NSDictionary*)map;
  78. /**
  79. * Creates a JSONKeyMapper, which converts underscore_case to camelCase and vice versa.
  80. */
  81. +(instancetype)mapperFromUnderscoreCaseToCamelCase;
  82. +(instancetype)mapperFromUpperCaseToLowerCase;
  83. @end