2 Copyright (C) 2007-2009 Stig Brautaset. All rights reserved.
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright notice, this
8 list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
14 * Neither the name of the author nor the names of its contributors may be used
15 to endorse or promote products derived from this software without specific
16 prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
22 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 @implementation SBJSON
37 jsonWriter = [SBJsonWriter new];
38 jsonParser = [SBJsonParser new];
39 [self setMaxDepth:512];
54 - (NSString *)stringWithObject:(id)obj {
55 NSString *repr = [jsonWriter stringWithObject:obj];
60 errorTrace = [[jsonWriter errorTrace] mutableCopy];
65 Returns a string containing JSON representation of the passed in value, or nil on error.
66 If nil is returned and @p error is not NULL, @p *error can be interrogated to find the cause of the error.
68 @param value any instance that can be represented as a JSON fragment
69 @param allowScalar wether to return json fragments for scalar objects
70 @param error used to return an error by reference (pass NULL if this is not desired)
72 @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
74 - (NSString*)stringWithObject:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error {
76 NSString *json = allowScalar ? [jsonWriter stringWithFragment:value] : [jsonWriter stringWithObject:value];
81 errorTrace = [[jsonWriter errorTrace] mutableCopy];
84 *error = [errorTrace lastObject];
89 Returns a string containing JSON representation of the passed in value, or nil on error.
90 If nil is returned and @p error is not NULL, @p error can be interrogated to find the cause of the error.
92 @param value any instance that can be represented as a JSON fragment
93 @param error used to return an error by reference (pass NULL if this is not desired)
95 @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
97 - (NSString*)stringWithFragment:(id)value error:(NSError**)error {
98 return [self stringWithObject:value
104 Returns a string containing JSON representation of the passed in value, or nil on error.
105 If nil is returned and @p error is not NULL, @p error can be interrogated to find the cause of the error.
107 @param value a NSDictionary or NSArray instance
108 @param error used to return an error by reference (pass NULL if this is not desired)
110 - (NSString*)stringWithObject:(id)value error:(NSError**)error {
111 return [self stringWithObject:value
118 - (id)objectWithString:(NSString *)repr {
119 id obj = [jsonParser objectWithString:repr];
123 [errorTrace release];
124 errorTrace = [[jsonParser errorTrace] mutableCopy];
130 Returns the object represented by the passed-in string or nil on error. The returned object can be
131 a string, number, boolean, null, array or dictionary.
133 @param value the json string to parse
134 @param allowScalar whether to return objects for JSON fragments
135 @param error used to return an error by reference (pass NULL if this is not desired)
137 @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
139 - (id)objectWithString:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error {
141 id obj = allowScalar ? [jsonParser fragmentWithString:value] : [jsonParser objectWithString:value];
145 [errorTrace release];
146 errorTrace = [[jsonParser errorTrace] mutableCopy];
149 *error = [errorTrace lastObject];
154 Returns the object represented by the passed-in string or nil on error. The returned object can be
155 a string, number, boolean, null, array or dictionary.
157 @param repr the json string to parse
158 @param error used to return an error by reference (pass NULL if this is not desired)
160 @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
162 - (id)fragmentWithString:(NSString*)repr error:(NSError**)error {
163 return [self objectWithString:repr
169 Returns the object represented by the passed-in string or nil on error. The returned object
170 will be either a dictionary or an array.
172 @param repr the json string to parse
173 @param error used to return an error by reference (pass NULL if this is not desired)
175 - (id)objectWithString:(NSString*)repr error:(NSError**)error {
176 return [self objectWithString:repr
183 #pragma mark Properties - parsing
185 - (NSUInteger)maxDepth {
186 return jsonParser.maxDepth;
189 - (void)setMaxDepth:(NSUInteger)d {
190 jsonWriter.maxDepth = jsonParser.maxDepth = d;
194 #pragma mark Properties - writing
196 - (BOOL)humanReadable {
197 return jsonWriter.humanReadable;
200 - (void)setHumanReadable:(BOOL)x {
201 jsonWriter.humanReadable = x;
205 return jsonWriter.sortKeys;
208 - (void)setSortKeys:(BOOL)x {
209 jsonWriter.sortKeys = x;