Statistics
| Branch: | Revision:

root / json-framework-3.2.0 / Classes / SBJsonStreamParserAdapter.h @ 3ebe9884

History | View | Annotate | Download (5 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
#import <Foundation/Foundation.h>
34
#import "SBJsonStreamParser.h"
35

    
36
typedef enum {
37
        SBJsonStreamParserAdapterNone,
38
        SBJsonStreamParserAdapterArray,
39
        SBJsonStreamParserAdapterObject,
40
} SBJsonStreamParserAdapterType;
41

    
42
/**
43
 Delegate for getting objects & arrays from the stream parser adapter
44

45
 */
46
@protocol SBJsonStreamParserAdapterDelegate
47

    
48
/**
49
 Called if a JSON array is found
50
 
51
 This method is called if a JSON array is found.
52
 
53
 */
54
- (void)parser:(SBJsonStreamParser*)parser foundArray:(NSArray*)array;
55

    
56
/**
57
 Called when a JSON object is found
58

59
 This method is called if a JSON object is found.
60
 */
61
- (void)parser:(SBJsonStreamParser*)parser foundObject:(NSDictionary*)dict;
62

    
63
@end
64

    
65
/**
66
 SBJsonStreamParserDelegate protocol adapter
67

68
 Rather than implementing the SBJsonStreamParserDelegate protocol yourself you will
69
 most likely find it much more convenient to use an instance of this class and
70
 implement the SBJsonStreamParserAdapterDelegate protocol instead.
71

72
 The default behaviour is that the delegate only receives one call from
73
 either the -parser:foundArray: or -parser:foundObject: method when the
74
 document is fully parsed. However, if your inputs contains multiple JSON
75
 documents and you set the parser's -supportMultipleDocuments property to YES
76
 you will get one call for each full method.
77

78
     SBJsonStreamParserAdapter *adapter = [[[SBJsonStreamParserAdapter alloc] init] autorelease];
79
     adapter.delegate = self;
80

81
     SBJsonStreamParser *parser = [[[SBJsonStreamParser alloc] init] autorelease];
82
     parser.delegate = adapter;
83
     parser.supportMultipleDocuments = YES;
84

85
     // Note that this input contains multiple top-level JSON documents
86
     NSData *json = [@"[]{}[]{}" dataWithEncoding:NSUTF8StringEncoding];
87
     [parser parse:data];
88

89
 In the above example self will have the following sequence of methods called on it:
90

91
 - -parser:foundArray:
92
 - -parser:foundObject:
93
 - -parser:foundArray:
94
 - -parser:foundObject:
95

96
 Often you won't have control over the input you're parsing, so can't make use of
97
 this feature. But, all is not lost: this class will let you get the same effect by
98
 allowing you to skip one or more of the outer enclosing objects. Thus, the next
99
 example results in the same sequence of -parser:foundArray: / -parser:foundObject:
100
 being called on your delegate.
101

102
     SBJsonStreamParserAdapter *adapter = [[[SBJsonStreamParserAdapter alloc] init] autorelease];
103
     adapter.delegate = self;
104
     adapter.levelsToSkip = 1;
105

106
     SBJsonStreamParser *parser = [[[SBJsonStreamParser alloc] init] autorelease];
107
     parser.delegate = adapter;
108

109
     // Note that this input contains A SINGLE top-level document
110
     NSData *json = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];
111
     [parser parse:data];
112

113
*/
114
@interface SBJsonStreamParserAdapter : NSObject <SBJsonStreamParserDelegate> {
115
@private
116
        NSUInteger depth;
117
    NSMutableArray *array;
118
        NSMutableDictionary *dict;
119
        NSMutableArray *keyStack;
120
        NSMutableArray *stack;
121
        
122
        SBJsonStreamParserAdapterType currentType;
123
}
124

    
125
/**
126
 How many levels to skip
127
 
128
 This is useful for parsing huge JSON documents, or documents coming in over a very slow link.
129
 
130
 If you set this to N it will skip the outer N levels and call the -parser:foundArray:
131
 or -parser:foundObject: methods for each of the inner objects, as appropriate.
132

133
*/
134
@property NSUInteger levelsToSkip;
135

    
136
/**
137
 Your delegate object
138
 Set this to the object you want to receive the SBJsonStreamParserAdapterDelegate messages.
139
 */
140
@property (unsafe_unretained) id<SBJsonStreamParserAdapterDelegate> delegate;
141

    
142
@end