SBJsonStreamParserAdapter Class Reference
Inherits from | NSObject |
Conforms to | SBJsonStreamParserDelegate |
Declared in | SBJsonStreamParserAdapter.h |
Overview
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];
Tasks
-
levelsToSkip
How many levels to skip
property -
delegate
Your delegate object Set this to the object you want to receive the SBJsonStreamParserAdapterDelegate messages.
property
Properties
delegate
Your delegate object Set this to the object you want to receive the SBJsonStreamParserAdapterDelegate messages.
@property (unsafe_unretained) id<> delegate
Declared In
SBJsonStreamParserAdapter.h
levelsToSkip
How many levels to skip
@property NSUInteger levelsToSkip
Discussion
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.
Declared In
SBJsonStreamParserAdapter.h