JSONModel.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. //
  2. // JSONModel.h
  3. //
  4. // @version 1.0.2
  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. #import "JSONModelError.h"
  17. #import "JSONValueTransformer.h"
  18. #import "JSONKeyMapper.h"
  19. /////////////////////////////////////////////////////////////////////////////////////////////
  20. #if TARGET_IPHONE_SIMULATOR
  21. #define JMLog( s, ... ) NSLog( @"[%@:%d] %@", [[NSString stringWithUTF8String:__FILE__] \
  22. lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
  23. #else
  24. #define JMLog( s, ... )
  25. #endif
  26. /////////////////////////////////////////////////////////////////////////////////////////////
  27. #pragma mark - Property Protocols
  28. /**
  29. * Protocol for defining properties in a JSON Model class that should not be considered at all
  30. * neither while importing nor when exporting JSON.
  31. *
  32. * @property (strong, nonatomic) NSString&lt;Ignore&gt;* propertyName;
  33. *
  34. */
  35. @protocol Ignore
  36. @end
  37. /**
  38. * Protocol for defining optional properties in a JSON Model class. Use like below to define
  39. * model properties that are not required to have values in the JSON input:
  40. *
  41. * @property (strong, nonatomic) NSString&lt;Optional&gt;* propertyName;
  42. *
  43. */
  44. @protocol Optional
  45. @end
  46. /**
  47. * Protocol for defining index properties in a JSON Model class. Use like below to define
  48. * model properties that are considered the Model's identifier (id).
  49. *
  50. * @property (strong, nonatomic) NSString&lt;Index&gt;* propertyName;
  51. *
  52. */
  53. @protocol Index
  54. @end
  55. /**
  56. * Make all objects Optional compatible to avoid compiler warnings
  57. */
  58. @interface NSObject(JSONModelPropertyCompatibility)<Optional, Index, Ignore>
  59. @end
  60. /**
  61. * ConvertOnDemand enables lazy model initialization for NSArrays of models
  62. *
  63. * @property (strong, nonatomic) NSArray&lt;JSONModel, ConvertOnDemand&gt;* propertyName;
  64. */
  65. @protocol ConvertOnDemand
  66. @end
  67. /**
  68. * Make all arrays ConvertOnDemand compatible to avoid compiler warnings
  69. */
  70. @interface NSArray(JSONModelPropertyCompatibility)<ConvertOnDemand>
  71. @end
  72. /////////////////////////////////////////////////////////////////////////////////////////////
  73. #pragma mark - JSONModel protocol
  74. /**
  75. * A protocol describing an abstract JSONModel class
  76. * JSONModel conforms to this protocol, so it can use itself abstractly
  77. */
  78. @protocol AbstractJSONModelProtocol <NSCopying, NSCoding>
  79. @required
  80. /**
  81. * All JSONModel classes should implement initWithDictionary:
  82. *
  83. * For most classes the default initWithDictionary: inherited from JSONModel itself
  84. * should suffice, but developers have the option ot also overwrite it if needed.
  85. *
  86. * @param dict a dictionary holding JSON objects, to be imported in the model.
  87. * @param err an error or NULL
  88. */
  89. -(instancetype)initWithDictionary:(NSDictionary*)dict error:(NSError**)err;
  90. /**
  91. * All JSONModel classes should implement initWithData:error:
  92. *
  93. * For most classes the default initWithData: inherited from JSONModel itself
  94. * should suffice, but developers have the option ot also overwrite it if needed.
  95. *
  96. * @param data representing a JSON response (usually fetched from web), to be imported in the model.
  97. * @param err an error or NULL
  98. */
  99. -(instancetype)initWithData:(NSData*)data error:(NSError**)error;
  100. /**
  101. * All JSONModel classes should be able to export themselves as a dictionary of
  102. * JSON compliant objects.
  103. *
  104. * For most classes the inherited from JSONModel default toDictionary implementation
  105. * should suffice.
  106. *
  107. * @return NSDictionary dictionary of JSON compliant objects
  108. * @exception JSONModelTypeNotAllowedException thrown when one of your model's custom class properties
  109. * does not have matching transformer method in an JSONValueTransformer.
  110. */
  111. -(NSDictionary*)toDictionary;
  112. /**
  113. * Export a model class to a dictionary, including only given properties
  114. *
  115. * @param propertyNames the properties to export; if nil, all properties exported
  116. * @return NSDictionary dictionary of JSON compliant objects
  117. * @exception JSONModelTypeNotAllowedException thrown when one of your model's custom class properties
  118. * does not have matching transformer method in an JSONValueTransformer.
  119. */
  120. -(NSDictionary*)toDictionaryWithKeys:(NSArray*)propertyNames;
  121. @end
  122. /////////////////////////////////////////////////////////////////////////////////////////////
  123. #pragma mark - JSONModel interface
  124. /**
  125. * The JSONModel is an abstract model class, you should not instantiate it directly,
  126. * as it does not have any properties, and therefore cannot serve as a data model.
  127. * Instead you should subclass it, and define the properties you want your data model
  128. * to have as properties of your own class.
  129. */
  130. @interface JSONModel : NSObject <AbstractJSONModelProtocol, NSSecureCoding>
  131. /** @name Creating and initializing models */
  132. /**
  133. * Create a new model instance and initialize it with the JSON from a text parameter. The method assumes UTF8 encoded input text.
  134. * @param string JSON text data
  135. * @param err an initialization error or nil
  136. * @exception JSONModelTypeNotAllowedException thrown when unsported type is found in the incoming JSON,
  137. * or a property type in your model is not supported by JSONValueTransformer and its categories
  138. * @see initWithString:usingEncoding:error: for use of custom text encodings
  139. */
  140. -(instancetype)initWithString:(NSString*)string error:(JSONModelError**)err;
  141. /**
  142. * Create a new model instance and initialize it with the JSON from a text parameter using the given encoding.
  143. * @param string JSON text data
  144. * @param encoding the text encoding to use when parsing the string (see NSStringEncoding)
  145. * @param err an initialization error or nil
  146. * @exception JSONModelTypeNotAllowedException thrown when unsported type is found in the incoming JSON,
  147. * or a property type in your model is not supported by JSONValueTransformer and its categories
  148. */
  149. -(instancetype)initWithString:(NSString *)string usingEncoding:(NSStringEncoding)encoding error:(JSONModelError**)err;
  150. -(instancetype)initWithDictionary:(NSDictionary*)dict error:(NSError **)err;
  151. -(instancetype)initWithData:(NSData *)data error:(NSError **)error;
  152. /** @name Exporting model contents */
  153. /**
  154. * Export the whole object to a dictionary
  155. * @return dictionary containing the data model
  156. */
  157. -(NSDictionary*)toDictionary;
  158. /**
  159. * Export the whole object to a JSON data text string
  160. * @return JSON text describing the data model
  161. */
  162. -(NSString*)toJSONString;
  163. /**
  164. * Export the whole object to a JSON data text string
  165. * @return JSON text data describing the data model
  166. */
  167. -(NSData*)toJSONData;
  168. /**
  169. * Export the specified properties of the object to a dictionary
  170. * @param propertyNames the properties to export; if nil, all properties exported
  171. * @return dictionary containing the data model
  172. */
  173. -(NSDictionary*)toDictionaryWithKeys:(NSArray*)propertyNames;
  174. /**
  175. * Export the specified properties of the object to a JSON data text string
  176. * @param propertyNames the properties to export; if nil, all properties exported
  177. * @return JSON text describing the data model
  178. */
  179. -(NSString*)toJSONStringWithKeys:(NSArray*)propertyNames;
  180. /**
  181. * Export the specified properties of the object to a JSON data text string
  182. * @param propertyNames the properties to export; if nil, all properties exported
  183. * @return JSON text data describing the data model
  184. */
  185. -(NSData*)toJSONDataWithKeys:(NSArray*)propertyNames;
  186. /** @name Batch methods */
  187. /**
  188. * If you have a list of dictionaries in a JSON feed, you can use this method to create an NSArray
  189. * of model objects. Handy when importing JSON data lists.
  190. * This method will loop over the input list and initialize a data model for every dictionary in the list.
  191. *
  192. * @param array list of dictionaries to be imported as models
  193. * @return list of initialized data model objects
  194. * @exception JSONModelTypeNotAllowedException thrown when unsported type is found in the incoming JSON,
  195. * or a property type in your model is not supported by JSONValueTransformer and its categories
  196. * @exception JSONModelInvalidDataException thrown when the input data does not include all required keys
  197. * @see arrayOfDictionariesFromModels:
  198. */
  199. +(NSMutableArray*)arrayOfModelsFromDictionaries:(NSArray*)array;
  200. +(NSMutableArray*)arrayOfModelsFromDictionaries:(NSArray*)array error:(NSError**)err;
  201. +(NSMutableArray*)arrayOfModelsFromData:(NSData*)data error:(NSError**)err;
  202. /**
  203. * If you have an NSArray of data model objects, this method takes it in and outputs a list of the
  204. * matching dictionaries. This method does the opposite of arrayOfObjectsFromDictionaries:
  205. * @param array list of JSONModel objects
  206. * @return a list of NSDictionary objects
  207. * @exception JSONModelTypeNotAllowedException thrown when unsported type is found in the incoming JSON,
  208. * or a property type in your model is not supported by JSONValueTransformer and its categories
  209. * @see arrayOfModelsFromDictionaries:
  210. */
  211. +(NSMutableArray*)arrayOfDictionariesFromModels:(NSArray*)array;
  212. /** @name Comparing models */
  213. /**
  214. * The name of the model's property, which is considered the model's unique identifier.
  215. * You can define Index property by using the Index protocol:
  216. * @property (strong, nonatomic) NSString&lt;Index&gt;* id;
  217. */
  218. -(NSString*)indexPropertyName;
  219. /**
  220. * Overriden NSObject method to compare model objects. Compares the &lt;Index&gt; property of the two models,
  221. * if an index property is defined.
  222. * @param object a JSONModel instance to compare to for equality
  223. */
  224. -(BOOL)isEqual:(id)object;
  225. /**
  226. * Comparision method, which uses the defined &lt;Index&gt; property of the two models, to compare them.
  227. * If there isn't an index property throws an exception. If the Index property does not have a compare: method
  228. * also throws an exception. NSString and NSNumber have compare: methods, and in case the Index property is
  229. * a another custom class, the programmer should create a custom compare: method then.
  230. * @param object a JSONModel instance to compare to
  231. */
  232. -(NSComparisonResult)compare:(id)object;
  233. /** @name Validation */
  234. /**
  235. * Overwrite the validate method in your own models if you need to perform some custom validation over the model data.
  236. * This method gets called at the very end of the JSONModel initializer, thus the model is in the state that you would
  237. * get it back when initialzed. Check the values of any property that needs to be validated and if any invalid values
  238. * are encountered return NO and set the error parameter to an NSError object. If the model is valid return YES.
  239. *
  240. * NB: Only setting the error parameter is not enough to fail the validation, you also need to return a NO value.
  241. *
  242. * @param error a pointer to an NSError object, to pass back an error if needed
  243. * @return a BOOL result, showing whether the model data validates or not. You can use the convenience method
  244. * [JSONModelError errorModelIsInvalid] to set the NSError param if the data fails your custom validation
  245. */
  246. -(BOOL)validate:(NSError**)error;
  247. /** @name Key mapping */
  248. /**
  249. * Overwrite in your models if your property names don't match your JSON key names.
  250. * Lookup JSONKeyMapper docs for more details.
  251. */
  252. +(JSONKeyMapper*)keyMapper;
  253. /**
  254. * Sets a key mapper which affects ALL the models in your project. Use this if you need only one mapper to work
  255. * with your API. For example if you are using the [JSONKeyMapper mapperFromUnderscoreCaseToCamelCase] it is more
  256. * likely that you will need to use it with ALL of your models.
  257. * NB: Custom key mappers take precendence over the global key mapper.
  258. * @param globalKeyMapper a key mapper to apply to all models in your project.
  259. *
  260. * Lookup JSONKeyMapper docs for more details.
  261. */
  262. +(void)setGlobalKeyMapper:(JSONKeyMapper*)globalKeyMapper;
  263. /**
  264. * Indicates whether the property with the given name is Optional.
  265. * To have a model with all of its properties being Optional just return YES.
  266. * This method returns by default NO, since the default behaviour is to have all properties required.
  267. * @param propertyName the name of the property
  268. * @return a BOOL result indicating whether the property is optional
  269. */
  270. +(BOOL)propertyIsOptional:(NSString*)propertyName;
  271. /**
  272. * Indicates whether the property with the given name is Ignored.
  273. * To have a model with all of its properties being Ignored just return YES.
  274. * This method returns by default NO, since the default behaviour is to have all properties required.
  275. * @param propertyName the name of the property
  276. * @return a BOOL result indicating whether the property is ignored
  277. */
  278. +(BOOL)propertyIsIgnored:(NSString*)propertyName;
  279. /**
  280. * Merges values from the given dictionary into the model instance.
  281. * @param dict dictionary with values
  282. * @param useKeyMapping if YES the method will use the model's key mapper and the global key mapper, if NO
  283. * it'll just try to match the dictionary keys to the model's properties
  284. */
  285. -(void)mergeFromDictionary:(NSDictionary*)dict useKeyMapping:(BOOL)useKeyMapping;
  286. @end