Skip to content
This repository has been archived by the owner on Mar 21, 2024. It is now read-only.

Commit

Permalink
Roll SBJsonStreamParserAdapter into SBJsonStreamParser. Fixes SBJson#83.
Browse files Browse the repository at this point in the history
  • Loading branch information
stig committed Jul 30, 2011
1 parent 15fe45f commit 405bbec
Show file tree
Hide file tree
Showing 13 changed files with 217 additions and 444 deletions.
1 change: 0 additions & 1 deletion Classes/SBJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@
#import "SBJsonParser.h"
#import "SBJsonWriter.h"
#import "SBJsonStreamParser.h"
#import "SBJsonStreamParserAdapter.h"
#import "SBJsonStreamWriter.h"
#import "NSObject+SBJson.h"

6 changes: 1 addition & 5 deletions Classes/SBJsonParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

#import "SBJsonParser.h"
#import "SBJsonStreamParser.h"
#import "SBJsonStreamParserAdapter.h"
#import "SBJsonStreamParserAccumulator.h"

@implementation SBJsonParser
Expand Down Expand Up @@ -60,12 +59,9 @@ - (id)objectWithData:(NSData *)data {

SBJsonStreamParserAccumulator *accumulator = [[[SBJsonStreamParserAccumulator alloc] init] autorelease];

SBJsonStreamParserAdapter *adapter = [[[SBJsonStreamParserAdapter alloc] init] autorelease];
adapter.delegate = accumulator;

SBJsonStreamParser *parser = [[[SBJsonStreamParser alloc] init] autorelease];
parser.maxDepth = self.maxDepth;
parser.delegate = adapter;
parser.delegate = accumulator;

switch ([parser parse:data]) {
case SBJsonStreamParserComplete:
Expand Down
117 changes: 83 additions & 34 deletions Classes/SBJsonStreamParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,67 +44,116 @@ typedef enum {


/**
@brief Delegate for interacting directly with the stream parser
You will most likely find it much more convenient to implement the
SBJsonStreamParserAdapterDelegate protocol instead.
@brief SBJsonStreamParserDelegate protocol adapter
@see SBJsonStreamParser
*/
@protocol SBJsonStreamParserDelegate

/// Called when object start is found
- (void)parserFoundObjectStart:(SBJsonStreamParser*)parser;

/// Called when object key is found
- (void)parser:(SBJsonStreamParser*)parser foundObjectKey:(NSString*)key;

/// Called when object end is found
- (void)parserFoundObjectEnd:(SBJsonStreamParser*)parser;

/// Called when array start is found
- (void)parserFoundArrayStart:(SBJsonStreamParser*)parser;

/// Called when array end is found
- (void)parserFoundArrayEnd:(SBJsonStreamParser*)parser;

/// Called when a boolean value is found
- (void)parser:(SBJsonStreamParser*)parser foundBoolean:(BOOL)x;

/// Called when a null value is found
- (void)parserFoundNull:(SBJsonStreamParser*)parser;

/// Called when a number is found
- (void)parser:(SBJsonStreamParser*)parser foundNumber:(NSNumber*)num;
/**
@brief 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 string is found
- (void)parser:(SBJsonStreamParser*)parser foundString:(NSString*)string;
/**
@brief 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

typedef enum {
SBJsonStreamParserNone,
SBJsonStreamParserArray,
SBJsonStreamParserObject,
} SBJsonStreamParserType;

/**
@brief Parse a stream of JSON data.
Using this class directly you can reduce the apparent latency for each
download/parse cycle of documents over a slow connection. You can start
parsing *and return chunks of the parsed document* before the entire
document is downloaded.
document is downloaded. Using this class is also useful to parse huge
documents on disk bit by bit so you don't have to keep them all in memory.
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.
@code
SBJsonStreamParser *parser = [[[SBJsonStreamParser alloc] init] autorelease];
parser.delegate = self;
parser.supportMultipleDocuments = YES;
// Note that this input contains multiple top-level JSON documents
NSData *json = [@"[]{}[]{}" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data];
@endcode
Using this class is also useful to parse huge documents on disk
bit by bit so you don't have to keep them all in memory.
In the above example @p self will have the following sequence of methods called on it:
@see SBJsonStreamParserAdapter for more information.
@li -parser:foundArray:
@li -parser:foundObject:
@li -parser:foundArray:
@li -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.
@code
SBJsonStreamParser *parser = [[[SBJsonStreamParser alloc] init] autorelease];
parser.delegate = self;
parser.levelsToSkip = 1;
// Note that this input contains A SINGLE top-level document
NSData *json = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data];
@endcode
@see SBJsonStreamParserDelegate
@see @ref objc2json
*/
@interface SBJsonStreamParser : NSObject {
@private
SBJsonTokeniser *tokeniser;

NSUInteger depth;
NSMutableArray *array;
NSMutableDictionary *dict;
NSMutableArray *keyStack;
NSMutableArray *stack;

SBJsonStreamParserType currentType;
}

@property (nonatomic, assign) SBJsonStreamParserState *state; // Private
@property (nonatomic, readonly, retain) NSMutableArray *stateStack; // Private


/**
@brief 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.
@see The StreamParserIntegrationTest.m file for examples
*/
@property NSUInteger levelsToSkip;

/**
@brief Expect multiple documents separated by whitespace
Expand All @@ -126,7 +175,7 @@ typedef enum {
into valid tokens.
@note
Usually this should be an instance of SBJsonStreamParserAdapter, but you can
Usually this should be an instance of SBJsonStreamParser, but you can
substitute your own implementation of the SBJsonStreamParserDelegate protocol if you need to.
*/
@property (assign) id<SBJsonStreamParserDelegate> delegate;
Expand Down
Loading

0 comments on commit 405bbec

Please sign in to comment.