root / json-framework-3.2.0 / Classes / SBJsonStreamParserAdapter.m @ 3ebe9884
History | View | Annotate | Download (4.4 kB)
1 |
/* |
---|---|
2 |
Copyright (c) 2010, Stig Brautaset. |
3 |
All rights reserved. |
4 |
|
5 |
Redistribution and use in source and binary forms, with or without |
6 |
modification, are permitted provided that the following conditions are |
7 |
met: |
8 |
|
9 |
Redistributions of source code must retain the above copyright |
10 |
notice, this list of conditions and the following disclaimer. |
11 |
|
12 |
Redistributions in binary form must reproduce the above copyright |
13 |
notice, this list of conditions and the following disclaimer in the |
14 |
documentation and/or other materials provided with the distribution. |
15 |
|
16 |
Neither the name of the the author nor the names of its contributors |
17 |
may be used to endorse or promote products derived from this software |
18 |
without specific prior written permission. |
19 |
|
20 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
21 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
22 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
23 |
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
24 |
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
25 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
26 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
27 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
28 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
29 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
30 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
31 |
*/ |
32 |
|
33 |
#if !__has_feature(objc_arc) |
34 |
#error "This source file must be compiled with ARC enabled!" |
35 |
#endif |
36 |
|
37 |
#import "SBJsonStreamParserAdapter.h" |
38 |
|
39 |
@interface SBJsonStreamParserAdapter () |
40 |
|
41 |
- (void)pop; |
42 |
- (void)parser:(SBJsonStreamParser*)parser found:(id)obj; |
43 |
|
44 |
@end |
45 |
|
46 |
|
47 |
|
48 |
@implementation SBJsonStreamParserAdapter |
49 |
|
50 |
@synthesize delegate; |
51 |
@synthesize levelsToSkip; |
52 |
|
53 |
#pragma mark Housekeeping |
54 |
|
55 |
- (id)init { |
56 |
self = [super init]; |
57 |
if (self) { |
58 |
keyStack = [[NSMutableArray alloc] initWithCapacity:32]; |
59 |
stack = [[NSMutableArray alloc] initWithCapacity:32]; |
60 |
|
61 |
currentType = SBJsonStreamParserAdapterNone; |
62 |
} |
63 |
return self; |
64 |
} |
65 |
|
66 |
|
67 |
#pragma mark Private methods |
68 |
|
69 |
- (void)pop { |
70 |
[stack removeLastObject]; |
71 |
array = nil; |
72 |
dict = nil; |
73 |
currentType = SBJsonStreamParserAdapterNone; |
74 |
|
75 |
id value = [stack lastObject]; |
76 |
|
77 |
if ([value isKindOfClass:[NSArray class]]) { |
78 |
array = value; |
79 |
currentType = SBJsonStreamParserAdapterArray; |
80 |
} else if ([value isKindOfClass:[NSDictionary class]]) { |
81 |
dict = value; |
82 |
currentType = SBJsonStreamParserAdapterObject; |
83 |
} |
84 |
} |
85 |
|
86 |
- (void)parser:(SBJsonStreamParser*)parser found:(id)obj { |
87 |
NSParameterAssert(obj); |
88 |
|
89 |
switch (currentType) { |
90 |
case SBJsonStreamParserAdapterArray: |
91 |
[array addObject:obj]; |
92 |
break; |
93 |
|
94 |
case SBJsonStreamParserAdapterObject: |
95 |
NSParameterAssert(keyStack.count); |
96 |
[dict setObject:obj forKey:[keyStack lastObject]]; |
97 |
[keyStack removeLastObject]; |
98 |
break; |
99 |
|
100 |
case SBJsonStreamParserAdapterNone: |
101 |
if ([obj isKindOfClass:[NSArray class]]) { |
102 |
[delegate parser:parser foundArray:obj]; |
103 |
} else { |
104 |
[delegate parser:parser foundObject:obj]; |
105 |
} |
106 |
break; |
107 |
|
108 |
default: |
109 |
break; |
110 |
} |
111 |
} |
112 |
|
113 |
|
114 |
#pragma mark Delegate methods |
115 |
|
116 |
- (void)parserFoundObjectStart:(SBJsonStreamParser*)parser { |
117 |
if (++depth > self.levelsToSkip) { |
118 |
dict = [NSMutableDictionary new]; |
119 |
[stack addObject:dict]; |
120 |
currentType = SBJsonStreamParserAdapterObject; |
121 |
} |
122 |
} |
123 |
|
124 |
- (void)parser:(SBJsonStreamParser*)parser foundObjectKey:(NSString*)key_ { |
125 |
[keyStack addObject:key_]; |
126 |
} |
127 |
|
128 |
- (void)parserFoundObjectEnd:(SBJsonStreamParser*)parser { |
129 |
if (depth-- > self.levelsToSkip) { |
130 |
id value = dict; |
131 |
[self pop]; |
132 |
[self parser:parser found:value]; |
133 |
} |
134 |
} |
135 |
|
136 |
- (void)parserFoundArrayStart:(SBJsonStreamParser*)parser { |
137 |
if (++depth > self.levelsToSkip) { |
138 |
array = [NSMutableArray new]; |
139 |
[stack addObject:array]; |
140 |
currentType = SBJsonStreamParserAdapterArray; |
141 |
} |
142 |
} |
143 |
|
144 |
- (void)parserFoundArrayEnd:(SBJsonStreamParser*)parser { |
145 |
if (depth-- > self.levelsToSkip) { |
146 |
id value = array; |
147 |
[self pop]; |
148 |
[self parser:parser found:value]; |
149 |
} |
150 |
} |
151 |
|
152 |
- (void)parser:(SBJsonStreamParser*)parser foundBoolean:(BOOL)x { |
153 |
[self parser:parser found:[NSNumber numberWithBool:x]]; |
154 |
} |
155 |
|
156 |
- (void)parserFoundNull:(SBJsonStreamParser*)parser { |
157 |
[self parser:parser found:[NSNull null]]; |
158 |
} |
159 |
|
160 |
- (void)parser:(SBJsonStreamParser*)parser foundNumber:(NSNumber*)num { |
161 |
[self parser:parser found:num]; |
162 |
} |
163 |
|
164 |
- (void)parser:(SBJsonStreamParser*)parser foundString:(NSString*)string { |
165 |
[self parser:parser found:string]; |
166 |
} |
167 |
|
168 |
@end |