Skip to content

Commit

Permalink
Changed secondary init method to be a class method instead
Browse files Browse the repository at this point in the history
  • Loading branch information
stig committed Nov 16, 2013
1 parent 74910aa commit c053beb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 47 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ SBJson's number one feature is chunk-based parsing. An example best sums it up:
NSLog(@"OOPS: %@", err);
}

id parser = [[SBJsonChunkParser alloc] initWithBlock:block
manyDocuments:YES
arrayItems:NO
errorHandler:eh];
id parser = [SBJsonChunkParser parserWithBlock:block
manyDocuments:YES
arrayItems:NO
errorHandler:eh];

// Note that this input contains multiple top-level JSON documents
NSData *json = [@"[]{}[]{}" dataWithEncoding:NSUTF8StringEncoding];
Expand All @@ -35,10 +35,10 @@ Sometimes you just get a single mammoth array containing lots of smaller
documents. In that case you can get the same effect by setting
arrayItems to YES:

id parser = [[SBJsonChunkParser alloc] initWithBlock:block
manyDocuments:NO
arrayItems:YES
errorHandler:eh];
id parser = [SBJsonChunkParser parserWithBlock:block
manyDocuments:NO
arrayItems:YES
errorHandler:eh];

// Note that this input contains A SINGLE top-level document
NSData *json = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];
Expand Down
34 changes: 21 additions & 13 deletions src/main/objc/SBJsonChunkParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ typedef id (^SBProcessBlock)(id, NSString*);
NSLog(@"OOPS: %@", err);
}
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
manyDocuments:YES
arrayItems:NO
errorHandler:eh];
id parser = [SBJsonChunkParser parserWithBlock:block
manyDocuments:YES
arrayItems:NO
errorHandler:eh];
// Note that this input contains multiple top-level JSON documents
NSData *json = [@"[]{}[]{}" dataWithEncoding:NSUTF8StringEncoding];
Expand All @@ -103,15 +103,23 @@ typedef id (^SBProcessBlock)(id, NSString*);
of this feature. But, all is not lost: if you are parsing a long array you can
get the same effect by setting arrayItems to YES:
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
manyDocuments:NO
arrayItems:YES
errorHandler:eh];
id parser = [SBJsonChunkParser parserWithBlock:block
manyDocuments:NO
arrayItems:YES
errorHandler:eh];
// Note that this input contains A SINGLE top-level document
NSData *json = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];
[parser parse:data];
@note Stream based parsing does mean that you lose some of the correctness
verification you would have with a parser that considered the entire input
before returning an answer. It is technically possible to have some parts
of a document returned *as if they were correct* but then encounter an error
in a later part of the document. You should keep this in mind when
considering whether it would suit your application.
*/
@interface SBJsonChunkParser : NSObject

Expand All @@ -131,14 +139,14 @@ id parser = [[SBJsonChunkParser alloc] initWithBlock:block
@param eh Called if the parser encounters an error.
*/
- (id)initWithBlock:(SBEnumeratorBlock)block
manyDocuments:(BOOL)manyDocs
arrayItems:(BOOL)arrayItems
errorHandler:(SBErrorHandlerBlock)eh;
+ (id)parserWithBlock:(SBEnumeratorBlock)block
manyDocuments:(BOOL)manyDocs
arrayItems:(BOOL)arrayItems
errorHandler:(SBErrorHandlerBlock)eh;


/**
Designated initialiser.
Create a JSON Chunk Parser.
@param block Called for each element. Set *stop to `YES` if you have seen
enough and would like to skip the rest of the elements.
Expand Down
17 changes: 11 additions & 6 deletions src/main/objc/SBJsonChunkParser.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,17 @@ - (id)init {
@throw @"Not Implemented";
}

- (id)initWithBlock:(SBEnumeratorBlock)block
manyDocuments:(BOOL)manyDocs
arrayItems:(BOOL)arrayItems
errorHandler:(SBErrorHandlerBlock)eh {
return [self initWithBlock:block processBlock:nil manyDocuments:manyDocs arrayItems:arrayItems maxDepth:32
errorHandler:eh];
+ (id)parserWithBlock:(SBEnumeratorBlock)block
manyDocuments:(BOOL)manyDocs
arrayItems:(BOOL)arrayItems
errorHandler:(SBErrorHandlerBlock)eh {

return [[self alloc] initWithBlock:block
processBlock:nil
manyDocuments:manyDocs
arrayItems:arrayItems
maxDepth:32
errorHandler:eh];
}

- (id)initWithBlock:(SBEnumeratorBlock)block
Expand Down
40 changes: 20 additions & 20 deletions src/test/objc/StreamSuite.m
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,10 @@ - (void) testParsingWithShortWorkBuffer{
"consectetur adipiscing elit. Donec ultrices ornare gravida. Vestibulum"\
" ante ipsum primisin faucibus orci luctus et ultrices posuere\"}]";

id parser = [[SBJsonChunkParser alloc] initWithBlock:block
manyDocuments:NO
arrayItems:NO
errorHandler:eh];
id parser = [SBJsonChunkParser parserWithBlock:block
manyDocuments:NO
arrayItems:NO
errorHandler:eh];

SBJsonParserStatus status = SBJsonParserWaitingForData;
NSData* data = nil;
Expand All @@ -87,10 +87,10 @@ - (void) testParsingWithShortWorkBuffer{
this data incrementally.
*/
- (void)testMultipleDocuments {
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
manyDocuments:YES
arrayItems:NO
errorHandler:eh];
id parser = [SBJsonChunkParser parserWithBlock:block
manyDocuments:YES
arrayItems:NO
errorHandler:eh];

NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSString *root = [[bundle resourcePath] stringByAppendingPathComponent:@"stream"];
Expand Down Expand Up @@ -125,21 +125,21 @@ - (void)parseArrayOfObjects:(SBJsonChunkParser *)parser {
}

- (void)testSingleArray {
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
manyDocuments:NO
arrayItems:NO
errorHandler:eh];
id parser = [SBJsonChunkParser parserWithBlock:block
manyDocuments:NO
arrayItems:NO
errorHandler:eh];

[self parseArrayOfObjects:parser];
STAssertEquals(arrayCount, (NSUInteger)1, nil);
STAssertEquals(objectCount, (NSUInteger)0, nil);
}

- (void)testSkipArray {
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
manyDocuments:NO
arrayItems:YES
errorHandler:eh];
id parser = [SBJsonChunkParser parserWithBlock:block
manyDocuments:NO
arrayItems:YES
errorHandler:eh];

[self parseArrayOfObjects:parser];
STAssertEquals(arrayCount, (NSUInteger)0, nil);
Expand All @@ -154,10 +154,10 @@ - (void)testStop {
*stop = ++count >= 23;
};

id parser = [[SBJsonChunkParser alloc] initWithBlock:block2
manyDocuments:NO
arrayItems:YES
errorHandler:eh];
id parser = [SBJsonChunkParser parserWithBlock:block2
manyDocuments:NO
arrayItems:YES
errorHandler:eh];

[self parseArrayOfObjects:parser];
STAssertEquals(ary.count, (NSUInteger)23, nil);
Expand Down

0 comments on commit c053beb

Please sign in to comment.