JSONValueTransformer.h 7.8 KB

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