|
| 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 | +/* |
| 8 | + Copyright (c) 2010, John Engelhart |
| 9 | + |
| 10 | + All rights reserved. |
| 11 | + |
| 12 | + Redistribution and use in source and binary forms, with or without |
| 13 | + modification, are permitted provided that the following conditions are met: |
| 14 | + |
| 15 | + * Redistributions of source code must retain the above copyright |
| 16 | + notice, this list of conditions and the following disclaimer. |
| 17 | + |
| 18 | + * Redistributions in binary form must reproduce the above copyright |
| 19 | + notice, this list of conditions and the following disclaimer in the |
| 20 | + documentation and/or other materials provided with the distribution. |
| 21 | + |
| 22 | + * Neither the name of the Zang Industries nor the names of its |
| 23 | + contributors may be used to endorse or promote products derived from |
| 24 | + this software without specific prior written permission. |
| 25 | + |
| 26 | + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 27 | + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 28 | + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 29 | + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 30 | + OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 31 | + SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 32 | + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 33 | + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 34 | + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 35 | + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 36 | + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 37 | +*/ |
| 38 | + |
| 39 | +#include <stddef.h> |
| 40 | +#include <stdint.h> |
| 41 | +#include <limits.h> |
| 42 | +#include <TargetConditionals.h> |
| 43 | +#include <AvailabilityMacros.h> |
| 44 | + |
| 45 | +#ifdef __OBJC__ |
| 46 | +#import <Foundation/NSError.h> |
| 47 | +#import <Foundation/NSObjCRuntime.h> |
| 48 | +#import <Foundation/NSString.h> |
| 49 | +#endif // __OBJC__ |
| 50 | + |
| 51 | +#ifdef __cplusplus |
| 52 | +extern "C" { |
| 53 | +#endif |
| 54 | + |
| 55 | + |
| 56 | +// For Mac OS X < 10.5. |
| 57 | +#ifndef NSINTEGER_DEFINED |
| 58 | +#define NSINTEGER_DEFINED |
| 59 | +#if defined(__LP64__) || defined(NS_BUILD_32_LIKE_64) |
| 60 | +typedef long NSInteger; |
| 61 | +typedef unsigned long NSUInteger; |
| 62 | +#define NSIntegerMin LONG_MIN |
| 63 | +#define NSIntegerMax LONG_MAX |
| 64 | +#define NSUIntegerMax ULONG_MAX |
| 65 | +#else // defined(__LP64__) || defined(NS_BUILD_32_LIKE_64) |
| 66 | +typedef int NSInteger; |
| 67 | +typedef unsigned int NSUInteger; |
| 68 | +#define NSIntegerMin INT_MIN |
| 69 | +#define NSIntegerMax INT_MAX |
| 70 | +#define NSUIntegerMax UINT_MAX |
| 71 | +#endif // defined(__LP64__) || defined(NS_BUILD_32_LIKE_64) |
| 72 | +#endif // NSINTEGER_DEFINED |
| 73 | + |
| 74 | + |
| 75 | +#ifndef _JSONKIT_H_ |
| 76 | +#define _JSONKIT_H_ |
| 77 | + |
| 78 | +typedef NSUInteger JKHash; |
| 79 | +typedef NSUInteger JKFlags; |
| 80 | +typedef NSUInteger JKTokenType; |
| 81 | +typedef JKFlags JKManagedBufferFlags; |
| 82 | +typedef JKFlags JKObjectStackFlags; |
| 83 | + |
| 84 | +typedef struct { |
| 85 | + unsigned char *ptr; |
| 86 | + size_t length; |
| 87 | +} JKPtrRange; |
| 88 | + |
| 89 | +typedef struct { |
| 90 | + const unsigned char *ptr; |
| 91 | + size_t length; |
| 92 | +} JKConstPtrRange; |
| 93 | + |
| 94 | +typedef struct { |
| 95 | + size_t location, length; |
| 96 | +} JKRange; |
| 97 | + |
| 98 | +typedef struct { |
| 99 | + JKPtrRange bytes; |
| 100 | + JKManagedBufferFlags flags; |
| 101 | + size_t roundSizeUpToMultipleOf; |
| 102 | +} JKManagedBuffer; |
| 103 | + |
| 104 | +typedef struct { |
| 105 | + void **objects, **keys; |
| 106 | + size_t count, index, roundSizeUpToMultipleOf; |
| 107 | + JKObjectStackFlags flags; |
| 108 | +} JKObjectStack; |
| 109 | + |
| 110 | +typedef struct { |
| 111 | + JKPtrRange bytes; |
| 112 | +} JKBuffer; |
| 113 | + |
| 114 | +typedef struct { |
| 115 | + JKConstPtrRange bytes; |
| 116 | +} JKConstBuffer; |
| 117 | + |
| 118 | +typedef NSUInteger JKValueType; |
| 119 | + |
| 120 | +typedef struct { |
| 121 | + JKConstPtrRange ptrRange; |
| 122 | + JKHash hash; |
| 123 | + JKValueType type; |
| 124 | + union { |
| 125 | + long long longLongValue; |
| 126 | + unsigned long long unsignedLongLongValue; |
| 127 | + double doubleValue; |
| 128 | + } number; |
| 129 | +} JKTokenValue; |
| 130 | + |
| 131 | +typedef struct { |
| 132 | + JKConstPtrRange tokenPtrRange; |
| 133 | + JKTokenType type; |
| 134 | + JKTokenValue value; |
| 135 | + JKManagedBuffer tokenBuffer; |
| 136 | +} JKParseToken; |
| 137 | + |
| 138 | + |
| 139 | +typedef struct { |
| 140 | + void *object; |
| 141 | + JKHash hash; |
| 142 | + size_t size; |
| 143 | + unsigned char *bytes; |
| 144 | + JKValueType type; |
| 145 | + unsigned char age; |
| 146 | +} JKTokenCacheItem; |
| 147 | + |
| 148 | +typedef struct { |
| 149 | + JKTokenCacheItem *items; |
| 150 | + size_t count, clockIdx; |
| 151 | +} JKTokenCache; |
| 152 | + |
| 153 | +/* |
| 154 | + JKParseOptionComments : Allow C style // and /_* ... *_/ (without a _, obviously) comments in JSON. |
| 155 | + JKParseOptionUnicodeNewlines : Allow Unicode recommended (?:\r\n|[\n\v\f\r\x85\p{Zl}\p{Zp}]) newlines. |
| 156 | + JKParseOptionLooseUnicode : Normally the decoder will stop with an error at any malformed Unicode. |
| 157 | + This option allows JSON with malformed Unicode to be parsed without reporting an error. |
| 158 | + Any malformed Unicode is replaced with \uFFFD, or "REPLACEMENT CHARACTER". |
| 159 | + */ |
| 160 | + |
| 161 | +enum { |
| 162 | + JKParseOptionNone = 0, |
| 163 | + JKParseOptionStrict = 0, |
| 164 | + JKParseOptionComments = (1 << 0), |
| 165 | + JKParseOptionUnicodeNewlines = (1 << 1), |
| 166 | + JKParseOptionLooseUnicode = (1 << 2), |
| 167 | + JKParseOptionPermitTextAfterValidJSON = (1 << 3), |
| 168 | + JKParseOptionValidFlags = (JKParseOptionComments | JKParseOptionUnicodeNewlines | JKParseOptionLooseUnicode | JKParseOptionPermitTextAfterValidJSON), |
| 169 | +}; |
| 170 | +typedef JKFlags JKParseOptionFlags; |
| 171 | + |
| 172 | +typedef struct { |
| 173 | + JKParseOptionFlags parseOptionFlags; |
| 174 | + JKConstBuffer stringBuffer; |
| 175 | + size_t atIndex, lineNumber, lineStartIndex; |
| 176 | + size_t prev_atIndex, prev_lineNumber, prev_lineStartIndex; |
| 177 | + int errorIsPrev; |
| 178 | + JKParseToken token; |
| 179 | + JKObjectStack objectStack; |
| 180 | + JKTokenCache cache; |
| 181 | + NSError *error; |
| 182 | +} JKParseState; |
| 183 | + |
| 184 | + |
| 185 | +#ifdef __OBJC__ |
| 186 | + |
| 187 | +// As a general rule of thumb, if you use a method that doesn't accept a JKParseOptionFlags argument, it defaults to JKParseOptionStrict |
| 188 | + |
| 189 | +@interface JSONDecoder : NSObject { |
| 190 | + JKParseState parseState; |
| 191 | +} |
| 192 | ++ (id)decoder; |
| 193 | ++ (id)decoderWithParseOptions:(JKParseOptionFlags)parseOptionFlags; |
| 194 | +- (id)initWithParseOptions:(JKParseOptionFlags)parseOptionFlags; |
| 195 | +- (id)parseUTF8String:(const unsigned char *)string length:(size_t)length; |
| 196 | +- (id)parseUTF8String:(const unsigned char *)string length:(size_t)length error:(NSError **)error; |
| 197 | +- (void)clearCache; |
| 198 | +@end |
| 199 | + |
| 200 | +@interface NSString (JSONKit) |
| 201 | +- (id)objectFromJSONString; |
| 202 | +- (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags; |
| 203 | +- (id)objectFromJSONStringWithParseOptions:(JKParseOptionFlags)parseOptionFlags error:(NSError **)error; |
| 204 | +@end |
| 205 | + |
| 206 | +#endif // __OBJC__ |
| 207 | + |
| 208 | +#endif // _JSONKIT_H_ |
| 209 | + |
| 210 | +#ifdef __cplusplus |
| 211 | +} // extern "C" |
| 212 | +#endif |
0 commit comments