Skip to content

Commit c053beb

Browse files
committed
Changed secondary init method to be a class method instead
1 parent 74910aa commit c053beb

File tree

4 files changed

+60
-47
lines changed

4 files changed

+60
-47
lines changed

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ SBJson's number one feature is chunk-based parsing. An example best sums it up:
1515
NSLog(@"OOPS: %@", err);
1616
}
1717

18-
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
19-
manyDocuments:YES
20-
arrayItems:NO
21-
errorHandler:eh];
18+
id parser = [SBJsonChunkParser parserWithBlock:block
19+
manyDocuments:YES
20+
arrayItems:NO
21+
errorHandler:eh];
2222

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

38-
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
39-
manyDocuments:NO
40-
arrayItems:YES
41-
errorHandler:eh];
38+
id parser = [SBJsonChunkParser parserWithBlock:block
39+
manyDocuments:NO
40+
arrayItems:YES
41+
errorHandler:eh];
4242

4343
// Note that this input contains A SINGLE top-level document
4444
NSData *json = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];

src/main/objc/SBJsonChunkParser.h

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ typedef id (^SBProcessBlock)(id, NSString*);
8383
NSLog(@"OOPS: %@", err);
8484
}
8585
86-
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
87-
manyDocuments:YES
88-
arrayItems:NO
89-
errorHandler:eh];
86+
id parser = [SBJsonChunkParser parserWithBlock:block
87+
manyDocuments:YES
88+
arrayItems:NO
89+
errorHandler:eh];
9090
9191
// Note that this input contains multiple top-level JSON documents
9292
NSData *json = [@"[]{}[]{}" dataWithEncoding:NSUTF8StringEncoding];
@@ -103,15 +103,23 @@ typedef id (^SBProcessBlock)(id, NSString*);
103103
of this feature. But, all is not lost: if you are parsing a long array you can
104104
get the same effect by setting arrayItems to YES:
105105
106-
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
107-
manyDocuments:NO
108-
arrayItems:YES
109-
errorHandler:eh];
106+
id parser = [SBJsonChunkParser parserWithBlock:block
107+
manyDocuments:NO
108+
arrayItems:YES
109+
errorHandler:eh];
110110
111111
// Note that this input contains A SINGLE top-level document
112112
NSData *json = [@"[[],{},[],{}]" dataWithEncoding:NSUTF8StringEncoding];
113113
[parser parse:data];
114114
115+
@note Stream based parsing does mean that you lose some of the correctness
116+
verification you would have with a parser that considered the entire input
117+
before returning an answer. It is technically possible to have some parts
118+
of a document returned *as if they were correct* but then encounter an error
119+
in a later part of the document. You should keep this in mind when
120+
considering whether it would suit your application.
121+
122+
115123
*/
116124
@interface SBJsonChunkParser : NSObject
117125

@@ -131,14 +139,14 @@ id parser = [[SBJsonChunkParser alloc] initWithBlock:block
131139
@param eh Called if the parser encounters an error.
132140
133141
*/
134-
- (id)initWithBlock:(SBEnumeratorBlock)block
135-
manyDocuments:(BOOL)manyDocs
136-
arrayItems:(BOOL)arrayItems
137-
errorHandler:(SBErrorHandlerBlock)eh;
142+
+ (id)parserWithBlock:(SBEnumeratorBlock)block
143+
manyDocuments:(BOOL)manyDocs
144+
arrayItems:(BOOL)arrayItems
145+
errorHandler:(SBErrorHandlerBlock)eh;
138146

139147

140148
/**
141-
Designated initialiser.
149+
Create a JSON Chunk Parser.
142150
143151
@param block Called for each element. Set *stop to `YES` if you have seen
144152
enough and would like to skip the rest of the elements.

src/main/objc/SBJsonChunkParser.m

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,17 @@ - (id)init {
7272
@throw @"Not Implemented";
7373
}
7474

75-
- (id)initWithBlock:(SBEnumeratorBlock)block
76-
manyDocuments:(BOOL)manyDocs
77-
arrayItems:(BOOL)arrayItems
78-
errorHandler:(SBErrorHandlerBlock)eh {
79-
return [self initWithBlock:block processBlock:nil manyDocuments:manyDocs arrayItems:arrayItems maxDepth:32
80-
errorHandler:eh];
75+
+ (id)parserWithBlock:(SBEnumeratorBlock)block
76+
manyDocuments:(BOOL)manyDocs
77+
arrayItems:(BOOL)arrayItems
78+
errorHandler:(SBErrorHandlerBlock)eh {
79+
80+
return [[self alloc] initWithBlock:block
81+
processBlock:nil
82+
manyDocuments:manyDocs
83+
arrayItems:arrayItems
84+
maxDepth:32
85+
errorHandler:eh];
8186
}
8287

8388
- (id)initWithBlock:(SBEnumeratorBlock)block

src/test/objc/StreamSuite.m

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ - (void) testParsingWithShortWorkBuffer{
6262
"consectetur adipiscing elit. Donec ultrices ornare gravida. Vestibulum"\
6363
" ante ipsum primisin faucibus orci luctus et ultrices posuere\"}]";
6464

65-
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
66-
manyDocuments:NO
67-
arrayItems:NO
68-
errorHandler:eh];
65+
id parser = [SBJsonChunkParser parserWithBlock:block
66+
manyDocuments:NO
67+
arrayItems:NO
68+
errorHandler:eh];
6969

7070
SBJsonParserStatus status = SBJsonParserWaitingForData;
7171
NSData* data = nil;
@@ -87,10 +87,10 @@ - (void) testParsingWithShortWorkBuffer{
8787
this data incrementally.
8888
*/
8989
- (void)testMultipleDocuments {
90-
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
91-
manyDocuments:YES
92-
arrayItems:NO
93-
errorHandler:eh];
90+
id parser = [SBJsonChunkParser parserWithBlock:block
91+
manyDocuments:YES
92+
arrayItems:NO
93+
errorHandler:eh];
9494

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

127127
- (void)testSingleArray {
128-
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
129-
manyDocuments:NO
130-
arrayItems:NO
131-
errorHandler:eh];
128+
id parser = [SBJsonChunkParser parserWithBlock:block
129+
manyDocuments:NO
130+
arrayItems:NO
131+
errorHandler:eh];
132132

133133
[self parseArrayOfObjects:parser];
134134
STAssertEquals(arrayCount, (NSUInteger)1, nil);
135135
STAssertEquals(objectCount, (NSUInteger)0, nil);
136136
}
137137

138138
- (void)testSkipArray {
139-
id parser = [[SBJsonChunkParser alloc] initWithBlock:block
140-
manyDocuments:NO
141-
arrayItems:YES
142-
errorHandler:eh];
139+
id parser = [SBJsonChunkParser parserWithBlock:block
140+
manyDocuments:NO
141+
arrayItems:YES
142+
errorHandler:eh];
143143

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

157-
id parser = [[SBJsonChunkParser alloc] initWithBlock:block2
158-
manyDocuments:NO
159-
arrayItems:YES
160-
errorHandler:eh];
157+
id parser = [SBJsonChunkParser parserWithBlock:block2
158+
manyDocuments:NO
159+
arrayItems:YES
160+
errorHandler:eh];
161161

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

0 commit comments

Comments
 (0)