Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100755 101 lines (75 sloc) 3.16 kb
dde58df7 »
2012-11-19 Initial commit
1 /*
2 Copyright (C) 2009,2010 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 "SBJsonParser.h"
31 #import "SBJsonStreamParser.h"
32 #import "SBJsonStreamParserAdapter.h"
33 #import "SBJsonStreamParserAccumulator.h"
34
35 @implementation SBJsonParser
36
37 @synthesize maxDepth;
38 @synthesize error;
39
40 - (id)init {
41 self = [super init];
42 if (self)
43 self.maxDepth = 32u;
44 return self;
45 }
46
47
48 #pragma mark Methods
49
50 - (id)objectWithData:(NSData *)data {
51
52 if (!data) {
53 self.error = @"Input was 'nil'";
54 return nil;
55 }
56
57 SBJsonStreamParserAccumulator *accumulator = [[SBJsonStreamParserAccumulator alloc] init];
58
59 SBJsonStreamParserAdapter *adapter = [[SBJsonStreamParserAdapter alloc] init];
60 adapter.delegate = accumulator;
61
62 SBJsonStreamParser *parser = [[SBJsonStreamParser alloc] init];
63 parser.maxDepth = self.maxDepth;
64 parser.delegate = adapter;
65
66 switch ([parser parse:data]) {
67 case SBJsonStreamParserComplete:
68 return accumulator.value;
69 break;
70
71 case SBJsonStreamParserWaitingForData:
72 self.error = @"Unexpected end of input";
73 break;
74
75 case SBJsonStreamParserError:
76 self.error = parser.error;
77 break;
78 }
79
80 return nil;
81 }
82
83 - (id)objectWithString:(NSString *)repr {
84 return [self objectWithData:[repr dataUsingEncoding:NSUTF8StringEncoding]];
85 }
86
87 - (id)objectWithString:(NSString*)repr error:(NSError**)error_ {
88 id tmp = [self objectWithString:repr];
89 if (tmp)
90 return tmp;
91
92 if (error_) {
93 NSDictionary *ui = [NSDictionary dictionaryWithObjectsAndKeys:error, NSLocalizedDescriptionKey, nil];
94 *error_ = [NSError errorWithDomain:@"org.brautaset.SBJsonParser.ErrorDomain" code:0 userInfo:ui];
95 }
96
97 return nil;
98 }
99
100 @end
Something went wrong with that request. Please try again.