JSONValueTransformer.h 6.3 KB

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