JSONValueTransformer.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // JSONValueTransformer.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. #import "JSONModelArray.h"
  16. /////////////////////////////////////////////////////////////////////////////////////////////
  17. #pragma mark - extern definitions
  18. /**
  19. * Boolean function to check for null values. Handy when you need to both check
  20. * for nil and [NSNUll null]
  21. */
  22. extern BOOL isNull(id value);
  23. /////////////////////////////////////////////////////////////////////////////////////////////
  24. #pragma mark - JSONValueTransformer interface
  25. /**
  26. * **You don't need to call methods of this class manually.**
  27. *
  28. * Class providing methods to transform values from one class to another.
  29. * You are given a number of built-in transformers, but you are encouraged to
  30. * extend this class with your own categories to add further value transformers.
  31. * Just few examples of what can you add to JSONValueTransformer: hex colors in JSON to UIColor,
  32. * hex numbers in JSON to NSNumber model properties, base64 encoded strings in JSON to UIImage properties, and more.
  33. *
  34. * The class is invoked by JSONModel while transforming incoming
  35. * JSON types into your target class property classes, and vice versa.
  36. * One static copy is create and store in the JSONModel class scope.
  37. */
  38. @interface JSONValueTransformer : NSObject
  39. @property (strong, nonatomic, readonly) NSDictionary* primitivesNames;
  40. /** @name Resolving cluster class names */
  41. /**
  42. * This method returns the umbrella class for any standard class cluster members.
  43. * For example returns NSString when given as input NSString, NSMutableString, __CFString and __CFConstantString
  44. * The method currently looksup a pre-defined list.
  45. * @param sourceClass the class to get the umbrella class for
  46. * @return Class
  47. */
  48. +(Class)classByResolvingClusterClasses:(Class)sourceClass;
  49. #pragma mark - NSMutableString <-> NSString
  50. /** @name Transforming to Mutable copies */
  51. /**
  52. * Transforms a string value to a mutable string value
  53. * @param string incoming string
  54. * @return mutable string
  55. */
  56. -(NSMutableString*)NSMutableStringFromNSString:(NSString*)string;
  57. #pragma mark - NSMutableArray <-> NSArray
  58. /**
  59. * Transforms an array to a mutable array
  60. * @param array incoming array
  61. * @return mutable array
  62. */
  63. -(NSMutableArray*)NSMutableArrayFromNSArray:(NSArray*)array;
  64. #pragma mark - NS(Mutable)Array <- JSONModelArray
  65. /**
  66. * Transforms an array to a JSONModelArray
  67. * @param array incoming array
  68. * @return JSONModelArray
  69. */
  70. -(NSArray*)NSArrayFromJSONModelArray:(JSONModelArray*)array;
  71. -(NSMutableArray*)NSMutableArrayFromJSONModelArray:(JSONModelArray*)array;
  72. #pragma mark - NSMutableDictionary <-> NSDictionary
  73. /**
  74. * Transforms a dictionary to a mutable dictionary
  75. * @param dict incoming dictionary
  76. * @return mutable dictionary
  77. */
  78. -(NSMutableDictionary*)NSMutableDictionaryFromNSDictionary:(NSDictionary*)dict;
  79. #pragma mark - NSSet <-> NSArray
  80. /** @name Transforming Sets */
  81. /**
  82. * Transforms an array to a set
  83. * @param array incoming array
  84. * @return set with the array's elements
  85. */
  86. -(NSSet*)NSSetFromNSArray:(NSArray*)array;
  87. /**
  88. * Transforms an array to a mutable set
  89. * @param array incoming array
  90. * @return mutable set with the array's elements
  91. */
  92. -(NSMutableSet*)NSMutableSetFromNSArray:(NSArray*)array;
  93. /**
  94. * Transforms a set to an array
  95. * @param set incoming set
  96. * @return an array with the set's elements
  97. */
  98. -(NSArray*)JSONObjectFromNSSet:(NSSet*)set;
  99. /**
  100. * Transforms a mutable set to an array
  101. * @param set incoming mutable set
  102. * @return an array with the set's elements
  103. */
  104. -(NSArray*)JSONObjectFromNSMutableSet:(NSMutableSet*)set;
  105. #pragma mark - BOOL <-> number/string
  106. /** @name Transforming JSON types */
  107. /**
  108. * Transforms a number object to a bool number object
  109. * @param number the number to convert
  110. * @return the resulting number
  111. */
  112. -(NSNumber*)BOOLFromNSNumber:(NSNumber*)number;
  113. /**
  114. * Transforms a number object to a bool number object
  115. * @param string the string value to convert, "0" converts to NO, everything else to YES
  116. * @return the resulting number
  117. */
  118. -(NSNumber*)BOOLFromNSString:(NSString*)string;
  119. /**
  120. * Transforms a BOOL value to a bool number object
  121. * @param number an NSNumber value coming from the model
  122. * @return the result number
  123. */
  124. -(NSNumber*)JSONObjectFromBOOL:(NSNumber*)number;
  125. #pragma mark - string <-> number
  126. /**
  127. * Transforms a string object to a number object
  128. * @param string the string to convert
  129. * @return the resulting number
  130. */
  131. -(NSNumber*)NSNumberFromNSString:(NSString*)string;
  132. /**
  133. * Transforms a number object to a string object
  134. * @param number the number to convert
  135. * @return the resulting string
  136. */
  137. -(NSString*)NSStringFromNSNumber:(NSNumber*)number;
  138. /**
  139. * Transforms a string object to a nsdecimalnumber object
  140. * @param string the string to convert
  141. * @return the resulting number
  142. */
  143. -(NSDecimalNumber*)NSDecimalNumberFromNSString:(NSString*)string;
  144. /**
  145. * Transforms a nsdecimalnumber object to a string object
  146. * @param number the number to convert
  147. * @return the resulting string
  148. */
  149. -(NSString*)NSStringFromNSDecimalNumber:(NSDecimalNumber*)number;
  150. #pragma mark - string <-> url
  151. /** @name Transforming URLs */
  152. /**
  153. * Transforms a string object to an NSURL object
  154. * @param string the string to convert
  155. * @return the resulting url object
  156. */
  157. -(NSURL*)NSURLFromNSString:(NSString*)string;
  158. /**
  159. * Transforms an NSURL object to a string
  160. * @param url the url object to convert
  161. * @return the resulting string
  162. */
  163. -(NSString*)JSONObjectFromNSURL:(NSURL*)url;
  164. #pragma mark - string <-> time zone
  165. /** @name Transforming NSTimeZone */
  166. /**
  167. * Transforms a string object to an NSTimeZone object
  168. * @param string the string to convert
  169. * @return the resulting NSTimeZone object
  170. */
  171. - (NSTimeZone *)NSTimeZoneFromNSString:(NSString*)string;
  172. /**
  173. * Transforms an NSTimeZone object to a string
  174. * @param timeZone the time zone object to convert
  175. * @return the resulting string
  176. */
  177. - (NSString *)JSONObjectFromNSTimeZone:(NSTimeZone *)timeZone;
  178. #pragma mark - string <-> date
  179. /** @name Transforming Dates */
  180. /**
  181. * The following two methods are not public. This way if there is a category on converting
  182. * dates it'll override them. If there isn't a category the default methods found in the .m
  183. * file will be invoked. If these are public a warning is produced at the point of overriding
  184. * them in a category, so they have to stay hidden here.
  185. */
  186. //-(NSDate*)NSDateFromNSString:(NSString*)string;
  187. //-(NSString*)JSONObjectFromNSDate:(NSDate*)date;
  188. #pragma mark - number <-> date
  189. /**
  190. * Transforms a number to an NSDate object
  191. * @param number the number to convert
  192. * @return the resulting date
  193. */
  194. - (NSDate*)NSDateFromNSNumber:(NSNumber*)number;
  195. @end