-
Notifications
You must be signed in to change notification settings - Fork 6
/
SBJsonStreamParserAdapter.h
142 lines (107 loc) · 4.97 KB
/
SBJsonStreamParserAdapter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright (c) 2010, Stig Brautaset.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
Neither the name of the the author nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import <Foundation/Foundation.h>
#import "SBJsonStreamParser.h"
typedef enum {
SBJsonStreamParserAdapterNone,
SBJsonStreamParserAdapterArray,
SBJsonStreamParserAdapterObject,
} SBJsonStreamParserAdapterType;
/**
Delegate for getting objects & arrays from the stream parser adapter
*/
@protocol SBJsonStreamParserAdapterDelegate
/**
Called if a JSON array is found
This method is called if a JSON array is found.
*/
- (void)parser:(SBJsonStreamParser*)parser foundArray:(NSArray*)array;
/**
Called when a JSON object is found
This method is called if a JSON object is found.
*/
- (void)parser:(SBJsonStreamParser*)parser foundObject:(NSDictionary*)dict;
@end
/**
SBJsonStreamParserDelegate protocol adapter
Rather than implementing the SBJsonStreamParserDelegate protocol yourself you will
most likely find it much more convenient to use an instance of this class and
implement the SBJsonStreamParserAdapterDelegate protocol instead.
The default behaviour is that the delegate only receives one call from
either the -parser:foundArray: or -parser:foundObject: method when the
document is fully parsed. However, if your inputs contains multiple JSON
documents and you set the parser's -supportMultipleDocuments property to YES
you will get one call for each full method.
SBJsonStreamParserAdapter *adapter = [[[SBJsonStreamParserAdapter alloc] init] autorelease];
adapter.delegate = self;
SBJsonStreamParser *parser = [[[SBJsonStreamParser alloc] init] autorelease];
parser.delegate = adapter;
parser.supportMultipleDocuments = YES;
// Note that this input contains multiple top-level JSON documents
NSData *json = [@"[]{}[]{}" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data];
In the above example self will have the following sequence of methods called on it:
- -parser:foundArray:
- -parser:foundObject:
- -parser:foundArray:
- -parser:foundObject:
Often you won't have control over the input you're parsing, so can't make use of
this feature. But, all is not lost: this class will let you get the same effect by
allowing you to skip one or more of the outer enclosing objects. Thus, the next
example results in the same sequence of -parser:foundArray: / -parser:foundObject:
being called on your delegate.
SBJsonStreamParserAdapter *adapter = [[[SBJsonStreamParserAdapter alloc] init] autorelease];
adapter.delegate = self;
adapter.levelsToSkip = 1;
SBJsonStreamParser *parser = [[[SBJsonStreamParser alloc] init] autorelease];
parser.delegate = adapter;
// Note that this input contains A SINGLE top-level document
NSData *json = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data];
*/
@interface SBJsonStreamParserAdapter : NSObject <SBJsonStreamParserDelegate> {
@private
NSUInteger depth;
NSMutableArray *array;
NSMutableDictionary *dict;
NSMutableArray *keyStack;
NSMutableArray *stack;
SBJsonStreamParserAdapterType currentType;
}
/**
How many levels to skip
This is useful for parsing huge JSON documents, or documents coming in over a very slow link.
If you set this to N it will skip the outer N levels and call the -parser:foundArray:
or -parser:foundObject: methods for each of the inner objects, as appropriate.
*/
@property NSUInteger levelsToSkip;
/**
Your delegate object
Set this to the object you want to receive the SBJsonStreamParserAdapterDelegate messages.
*/
@property (unsafe_unretained) id<SBJsonStreamParserAdapterDelegate> delegate;
@end