Expanded open file functionality to use available apps.
[pithos-ios] / Classes / SBJSON.m
1 /*
2  Copyright (C) 2007-2009 Stig Brautaset. All rights reserved.
3  
4  Redistribution and use in source and binary forms, with or without
5  modification, are permitted provided that the following conditions are met:
6  
7  * Redistributions of source code must retain the above copyright notice, this
8    list of conditions and the following disclaimer.
9  
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.
13  
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.
17  
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.
28  */
29
30 #import "SBJSON.h"
31
32 @implementation SBJSON
33
34 - (id)init {
35     self = [super init];
36     if (self) {
37         jsonWriter = [SBJsonWriter new];
38         jsonParser = [SBJsonParser new];
39         [self setMaxDepth:512];
40
41     }
42     return self;
43 }
44
45 - (void)dealloc {
46     [jsonWriter release];
47     [jsonParser release];
48     [super dealloc];
49 }
50
51 #pragma mark Writer 
52
53
54 - (NSString *)stringWithObject:(id)obj {
55     NSString *repr = [jsonWriter stringWithObject:obj];
56     if (repr)
57         return repr;
58     
59     [errorTrace release];
60     errorTrace = [[jsonWriter errorTrace] mutableCopy];
61     return nil;
62 }
63
64 /**
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.
67  
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)
71  
72 @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
73  */
74 - (NSString*)stringWithObject:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error {
75     
76     NSString *json = allowScalar ? [jsonWriter stringWithFragment:value] : [jsonWriter stringWithObject:value];
77     if (json)
78         return json;
79
80     [errorTrace release];
81     errorTrace = [[jsonWriter errorTrace] mutableCopy];
82     
83     if (error)
84         *error = [errorTrace lastObject];
85     return nil;
86 }
87
88 /**
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.
91  
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)
94  
95  @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
96  */
97 - (NSString*)stringWithFragment:(id)value error:(NSError**)error {
98     return [self stringWithObject:value
99                       allowScalar:YES
100                             error:error];
101 }
102
103 /**
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.
106  
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)
109  */
110 - (NSString*)stringWithObject:(id)value error:(NSError**)error {
111     return [self stringWithObject:value
112                       allowScalar:NO
113                             error:error];
114 }
115
116 #pragma mark Parsing
117
118 - (id)objectWithString:(NSString *)repr {
119     id obj = [jsonParser objectWithString:repr];
120     if (obj)
121         return obj;
122
123     [errorTrace release];
124     errorTrace = [[jsonParser errorTrace] mutableCopy];
125     
126     return nil;
127 }
128
129 /**
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.
132  
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)
136  
137  @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed.
138  */
139 - (id)objectWithString:(id)value allowScalar:(BOOL)allowScalar error:(NSError**)error {
140
141     id obj = allowScalar ? [jsonParser fragmentWithString:value] : [jsonParser objectWithString:value];
142     if (obj)
143         return obj;
144     
145     [errorTrace release];
146     errorTrace = [[jsonParser errorTrace] mutableCopy];
147
148     if (error)
149         *error = [errorTrace lastObject];
150     return nil;
151 }
152
153 /**
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.
156  
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)
159  
160  @deprecated Given we bill ourselves as a "strict" JSON library, this method should be removed. 
161  */
162 - (id)fragmentWithString:(NSString*)repr error:(NSError**)error {
163     return [self objectWithString:repr
164                       allowScalar:YES
165                             error:error];
166 }
167
168 /**
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.
171  
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)
174  */
175 - (id)objectWithString:(NSString*)repr error:(NSError**)error {
176     return [self objectWithString:repr
177                       allowScalar:NO
178                             error:error];
179 }
180
181
182
183 #pragma mark Properties - parsing
184
185 - (NSUInteger)maxDepth {
186     return jsonParser.maxDepth;
187 }
188
189 - (void)setMaxDepth:(NSUInteger)d {
190      jsonWriter.maxDepth = jsonParser.maxDepth = d;
191 }
192
193
194 #pragma mark Properties - writing
195
196 - (BOOL)humanReadable {
197     return jsonWriter.humanReadable;
198 }
199
200 - (void)setHumanReadable:(BOOL)x {
201     jsonWriter.humanReadable = x;
202 }
203
204 - (BOOL)sortKeys {
205     return jsonWriter.sortKeys;
206 }
207
208 - (void)setSortKeys:(BOOL)x {
209     jsonWriter.sortKeys = x;
210 }
211
212 @end