JSONKit.h 9.2 KB

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