Permalink
Browse files

Revert "Roll SBJsonStreamParserAdapter into SBJsonStreamParser. Fixes #…

…83."

This reverts commit 405bbec.
  • Loading branch information...
1 parent cb0c9c1 commit ac51dd81fdb654dfa852f10312cf79534df07630 @stig stig committed Aug 28, 2011
View
@@ -78,6 +78,7 @@
#import "SBJsonParser.h"
#import "SBJsonWriter.h"
#import "SBJsonStreamParser.h"
+#import "SBJsonStreamParserAdapter.h"
#import "SBJsonStreamWriter.h"
#import "NSObject+SBJson.h"
View
@@ -29,6 +29,7 @@ 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
@@ -59,9 +60,12 @@ - (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 = accumulator;
+ parser.delegate = adapter;
switch ([parser parse:data]) {
case SBJsonStreamParserComplete:
@@ -44,116 +44,67 @@ typedef enum {
/**
- @brief SBJsonStreamParserDelegate protocol adapter
-
- @see SBJsonStreamParser
+ @brief Delegate for interacting directly with the stream parser
+
+ You will most likely find it much more convenient to implement the
+ SBJsonStreamParserAdapterDelegate protocol instead.
*/
@protocol SBJsonStreamParserDelegate
-/**
- @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 object start is found
+- (void)parserFoundObjectStart:(SBJsonStreamParser*)parser;
-/**
- @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;
+/// 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;
+
+/// Called when a string is found
+- (void)parser:(SBJsonStreamParser*)parser foundString:(NSString*)string;
@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. 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
+ document is downloaded.
- In the above example @p self will have the following sequence of methods called on it:
+ 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.
- @li -parser:foundArray:
- @li -parser:foundObject:
- @li -parser:foundArray:
- @li -parser:foundObject:
+ @see SBJsonStreamParserAdapter for more information.
- 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
@@ -175,7 +126,7 @@ typedef enum {
into valid tokens.
@note
- Usually this should be an instance of SBJsonStreamParser, but you can
+ Usually this should be an instance of SBJsonStreamParserAdapter, but you can
substitute your own implementation of the SBJsonStreamParserDelegate protocol if you need to.
*/
@property (assign) id<SBJsonStreamParserDelegate> delegate;
Oops, something went wrong.

0 comments on commit ac51dd8

Please sign in to comment.