JSONKit.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. //
  2. // JSONKit.h
  3. // http://github.com/johnezang/JSONKit
  4. // Dual licensed under either the terms of the BSD License, or alternatively
  5. // under the terms of the Apache License, Version 2.0, as specified below.
  6. //
  7. /*
  8. Copyright (c) 2011, John Engelhart
  9. All rights reserved.
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the Zang Industries nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  26. TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. /*
  33. Copyright 2011 John Engelhart
  34. Licensed under the Apache License, Version 2.0 (the "License");
  35. you may not use this file except in compliance with the License.
  36. You may obtain a copy of the License at
  37. http://www.apache.org/licenses/LICENSE-2.0
  38. Unless required by applicable law or agreed to in writing, software
  39. distributed under the License is distributed on an "AS IS" BASIS,
  40. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  41. See the License for the specific language governing permissions and
  42. limitations under the License.
  43. */
  44. #include <stddef.h>
  45. #include <stdint.h>
  46. #include <limits.h>
  47. #include <TargetConditionals.h>
  48. #include <AvailabilityMacros.h>
  49. #ifdef __OBJC__
  50. #import <Foundation/NSArray.h>
  51. #import <Foundation/NSData.h>
  52. #import <Foundation/NSDictionary.h>
  53. #import <Foundation/NSError.h>
  54. #import <Foundation/NSObjCRuntime.h>
  55. #import <Foundation/NSString.h>
  56. #endif // __OBJC__
  57. #ifdef __cplusplus
  58. extern "C" {
  59. #endif
  60. // For Mac OS X < 10.5.
  61. #ifndef NSINTEGER_DEFINED
  62. #define NSINTEGER_DEFINED
  63. #if defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
  64. typedef long NSInteger;
  65. typedef unsigned long NSUInteger;
  66. #define NSIntegerMin LONG_MIN
  67. #define NSIntegerMax LONG_MAX
  68. #define NSUIntegerMax ULONG_MAX
  69. #else // defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
  70. typedef int NSInteger;
  71. typedef unsigned int NSUInteger;
  72. #define NSIntegerMin INT_MIN
  73. #define NSIntegerMax INT_MAX
  74. #define NSUIntegerMax UINT_MAX
  75. #endif // defined(__LP64__) || defined(NS_BUILD_32_LIKE_64)
  76. #endif // NSINTEGER_DEFINED
  77. #ifndef _JSONKIT_H_
  78. #define _JSONKIT_H_
  79. #if defined(__GNUC__) && (__GNUC__ >= 4) && defined(__APPLE_CC__) && (__APPLE_CC__ >= 5465)
  80. #define JK_DEPRECATED_ATTRIBUTE __attribute__((deprecated))
  81. #else
  82. #define JK_DEPRECATED_ATTRIBUTE
  83. #endif
  84. #define JSONKIT_VERSION_MAJOR 1
  85. #define JSONKIT_VERSION_MINOR 4
  86. typedef NSUInteger JKFlags;
  87. /*
  88. JKParseOptionComments : Allow C style // and /_* ... *_/ (without a _, obviously) comments in JSON.
  89. JKParseOptionUnicodeNewlines : Allow Unicode recommended (?:\r\n|[\n\v\f\r\x85\p{Zl}\p{Zp}]) newlines.
  90. JKParseOptionLooseUnicode : Normally the decoder will stop with an error at any malformed Unicode.
  91. This option allows JSON with malformed Unicode to be parsed without reporting an error.
  92. Any malformed Unicode is replaced with \uFFFD, or "REPLACEMENT CHARACTER".
  93. */
  94. enum {
  95. JKParseOptionNone = 0,
  96. JKParseOptionStrict = 0,
  97. JKParseOptionComments = (1 << 0),
  98. JKParseOptionUnicodeNewlines = (1 << 1),
  99. JKParseOptionLooseUnicode = (1 << 2),
  100. JKParseOptionPermitTextAfterValidJSON = (1 << 3),
  101. JKParseOptionValidFlags = (JKParseOptionComments | JKParseOptionUnicodeNewlines | JKParseOptionLooseUnicode | JKParseOptionPermitTextAfterValidJSON),
  102. };
  103. typedef JKFlags JKParseOptionFlags;
  104. enum {
  105. JKSerializeOptionNone = 0,
  106. JKSerializeOptionPretty = (1 << 0),
  107. JKSerializeOptionEscapeUnicode = (1 << 1),
  108. JKSerializeOptionEscapeForwardSlashes = (1 << 4),
  109. JKSerializeOptionValidFlags = (JKSerializeOptionPretty | JKSerializeOptionEscapeUnicode | JKSerializeOptionEscapeForwardSlashes),
  110. };
  111. typedef JKFlags JKSerializeOptionFlags;
  112. #ifdef __OBJC__
  113. typedef struct JKParseState JKParseState; // Opaque internal, private type.
  114. // As a general rule of thumb, if you use a method that doesn't accept a JKParseOptionFlags argument, it defaults to JKParseOptionStrict
  115. @interface JSONDecoder : NSObject {
  116. JKParseState *parseState;
  117. }
  118. + (id)decoder;
  119. + (id)decoderWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
  120. - (id)initWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
  121. - (void)clearCache;
  122. // The parse... methods were deprecated in v1.4 in favor of the v1.4 objectWith... methods.
  123. - (id)parseUTF8String:(const unsigned char *)string length:(size_t)length JK_DEPRECATED_ATTRIBUTE; // Deprecated in JSONKit v1.4. Use objectWithUTF8String:length: instead.
  124. - (id)parseUTF8String:(const unsigned char *)string length:(size_t)length error:(NSError **)error JK_DEPRECATED_ATTRIBUTE; // Deprecated in JSONKit v1.4. Use objectWithUTF8String:length:error: instead.
  125. // The NSData MUST be UTF8 encoded JSON.
  126. - (id)parseJSONData:(NSData *)jsonData JK_DEPRECATED_ATTRIBUTE; // Deprecated in JSONKit v1.4. Use objectWithData: instead.
  127. - (id)parseJSONData:(NSData *)jsonData error:(NSError **)error JK_DEPRECATED_ATTRIBUTE; // Deprecated in JSONKit v1.4. Use objectWithData:error: instead.
  128. // Methods that return immutable collection objects.
  129. - (id)objectWithUTF8String:(const unsigned char *)string length:(NSUInteger)length;
  130. - (id)objectWithUTF8String:(const unsigned char *)string length:(NSUInteger)length error:(NSError **)error;
  131. // The NSData MUST be UTF8 encoded JSON.
  132. - (id)objectWithData:(NSData *)jsonData;
  133. - (id)objectWithData:(NSData *)jsonData error:(NSError **)error;
  134. // Methods that return mutable collection objects.
  135. - (id)mutableObjectWithUTF8String:(const unsigned char *)string length:(NSUInteger)length;
  136. - (id)mutableObjectWithUTF8String:(const unsigned char *)string length:(NSUInteger)length error:(NSError **)error;
  137. // The NSData MUST be UTF8 encoded JSON.
  138. - (id)mutableObjectWithData:(NSData *)jsonData;
  139. - (id)mutableObjectWithData:(NSData *)jsonData error:(NSError **)error;
  140. @end
  141. ////////////
  142. #pragma mark Deserializing methods
  143. ////////////
  144. @interface NSString (JSONKitDeserializing)
  145. - (id)objectFromJSONString;
  146. - (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
  147. - (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
  148. - (id)mutableObjectFromJSONString;
  149. - (id)mutableObjectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
  150. - (id)mutableObjectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
  151. @end
  152. @interface NSData (JSONKitDeserializing)
  153. // The NSData MUST be UTF8 encoded JSON.
  154. - (id)objectFromJSONData;
  155. - (id)objectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
  156. - (id)objectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
  157. - (id)mutableObjectFromJSONData;
  158. - (id)mutableObjectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags;
  159. - (id)mutableObjectFromJSONDataWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error;
  160. @end
  161. ////////////
  162. #pragma mark Serializing methods
  163. ////////////
  164. @interface NSString (JSONKitSerializing)
  165. // Convenience methods for those that need to serialize the receiving NSString (i.e., instead of having to serialize a NSArray with a single NSString, you can "serialize to JSON" just the NSString).
  166. // Normally, a string that is serialized to JSON has quotation marks surrounding it, which you may or may not want when serializing a single string, and can be controlled with includeQuotes:
  167. // includeQuotes:YES `a "test"...` -> `"a \"test\"..."`
  168. // includeQuotes:NO `a "test"...` -> `a \"test\"...`
  169. - (NSData *)JSONData; // Invokes JSONDataWithOptions:JKSerializeOptionNone includeQuotes:YES
  170. - (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions includeQuotes:(BOOL)includeQuotes error:(NSError **)error;
  171. - (NSString *)JSONString; // Invokes JSONStringWithOptions:JKSerializeOptionNone includeQuotes:YES
  172. - (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions includeQuotes:(BOOL)includeQuotes error:(NSError **)error;
  173. @end
  174. @interface NSArray (JSONKitSerializing)
  175. - (NSData *)JSONData;
  176. - (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
  177. - (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingDelegate:(id)delegate selector:(SEL)selector error:(NSError **)error;
  178. - (NSString *)JSONString;
  179. - (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
  180. - (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingDelegate:(id)delegate selector:(SEL)selector error:(NSError **)error;
  181. @end
  182. @interface NSDictionary (JSONKitSerializing)
  183. - (NSData *)JSONData;
  184. - (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
  185. - (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingDelegate:(id)delegate selector:(SEL)selector error:(NSError **)error;
  186. - (NSString *)JSONString;
  187. - (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions error:(NSError **)error;
  188. - (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingDelegate:(id)delegate selector:(SEL)selector error:(NSError **)error;
  189. @end
  190. #ifdef __BLOCKS__
  191. @interface NSArray (JSONKitSerializingBlockAdditions)
  192. - (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingBlock:(id(^)(id object))block error:(NSError **)error;
  193. - (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingBlock:(id(^)(id object))block error:(NSError **)error;
  194. @end
  195. @interface NSDictionary (JSONKitSerializingBlockAdditions)
  196. - (NSData *)JSONDataWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingBlock:(id(^)(id object))block error:(NSError **)error;
  197. - (NSString *)JSONStringWithOptions:(JKSerializeOptionFlags)serializeOptions serializeUnsupportedClassesUsingBlock:(id(^)(id object))block error:(NSError **)error;
  198. @end
  199. #endif
  200. #endif // __OBJC__
  201. #endif // _JSONKIT_H_
  202. #ifdef __cplusplus
  203. } // extern "C"
  204. #endif