Skip to content

Commit

Permalink
Use accumulator object for the writer as well, to avoid superfluous d…
Browse files Browse the repository at this point in the history
…ata attribute on the writer
  • Loading branch information
stig committed May 9, 2011
1 parent dbc7dab commit 4833d35
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
18 changes: 18 additions & 0 deletions Classes/SBJsonStreamWriterAccumulator.h
@@ -0,0 +1,18 @@
//
// SBJsonStreamWriterAccumulator.h
// JSON
//
// Created by Stig Brautaset on 10/05/2011.
// Copyright 2011 Morgan Stanley. All rights reserved.
//

#import "SBJsonStreamWriter.h"

@interface SBJsonStreamWriterAccumulator : NSObject <SBJsonStreamWriterDelegate> {
@private
NSMutableData *data;
}

@property (readonly, copy) NSData* data;

@end
35 changes: 35 additions & 0 deletions Classes/SBJsonStreamWriterAccumulator.m
@@ -0,0 +1,35 @@
//
// SBJsonStreamWriterAccumulator.m
// JSON
//
// Created by Stig Brautaset on 10/05/2011.
// Copyright 2011 Morgan Stanley. All rights reserved.
//

#import "SBJsonStreamWriterAccumulator.h"


@implementation SBJsonStreamWriterAccumulator

@synthesize data;

- (id)init {
self = [super init];
if (self) {
data = [[NSMutableData alloc] initWithCapacity:8096u];
}
return self;
}

- (void)dealloc {
[data release];
[super dealloc];
}

#pragma SBJsonStreamWriterDelegate

- (void)writer:(SBJsonStreamWriter *)writer appendBytes:(const void *)bytes length:(NSUInteger)length {
[data appendBytes:bytes length:length];
}

@end
1 change: 0 additions & 1 deletion Classes/SBJsonWriter.h
Expand Up @@ -55,7 +55,6 @@
@interface SBJsonWriter : NSObject {

@protected
NSMutableData *_data;
NSString *error;
NSUInteger maxDepth;

Expand Down
20 changes: 7 additions & 13 deletions Classes/SBJsonWriter.m
Expand Up @@ -29,8 +29,10 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

#import "SBJsonWriter.h"
#import "SBJsonStreamWriter.h"
#import "SBJsonStreamWriterAccumulator.h"

@interface SBJsonWriter () <SBJsonStreamWriterDelegate>

@interface SBJsonWriter ()
@property (copy) NSString *error;
@end

Expand All @@ -46,14 +48,12 @@ - (id)init {
self = [super init];
if (self) {
self.maxDepth = 512;
_data = [[NSMutableData alloc] initWithCapacity:1024u];
}
return self;
}

- (void)dealloc {
[error release];
[_data release];
[super dealloc];
}

Expand All @@ -79,13 +79,14 @@ - (NSString*)stringWithObject:(id)value error:(NSError**)error_ {

- (NSData*)dataWithObject:(id)object {
self.error = nil;
[_data setLength:0];

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

SBJsonStreamWriter *streamWriter = [[[SBJsonStreamWriter alloc] init] autorelease];
streamWriter.sortKeys = self.sortKeys;
streamWriter.maxDepth = self.maxDepth;
streamWriter.humanReadable = self.humanReadable;
streamWriter.delegate = self;
streamWriter.delegate = accumulator;

BOOL ok = NO;
if ([object isKindOfClass:[NSDictionary class]])
Expand All @@ -102,18 +103,11 @@ - (NSData*)dataWithObject:(id)object {
}

if (ok)
return [[_data copy] autorelease];
return accumulator.data;

self.error = streamWriter.error;
return nil;
}

#pragma mark SBJsonStreamWriterDelegate

- (void)writer:(SBJsonStreamWriter *)writer appendBytes:(const void *)bytes length:(NSUInteger)length {
[_data appendBytes:bytes length:length];
}



@end
12 changes: 12 additions & 0 deletions JSON.xcodeproj/project.pbxproj
Expand Up @@ -92,6 +92,10 @@
BC2BC06B136CE7450073BC4B /* JsonTokeniserTest.m in Sources */ = {isa = PBXBuildFile; fileRef = BCA822CF127D6A9900DD3FD0 /* JsonTokeniserTest.m */; };
BC2BC06C136CE7450073BC4B /* StreamParserAdapterDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = BC9602FD12A4564E0024BDA2 /* StreamParserAdapterDelegate.m */; };
BC2BC06D136CE7450073BC4B /* StreamParserAdapterTest.m in Sources */ = {isa = PBXBuildFile; fileRef = BC96030912A45E3D0024BDA2 /* StreamParserAdapterTest.m */; };
BC78D188137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.h in Headers */ = {isa = PBXBuildFile; fileRef = BC78D186137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.h */; };
BC78D189137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.h in Headers */ = {isa = PBXBuildFile; fileRef = BC78D186137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.h */; };
BC78D18A137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.m in Sources */ = {isa = PBXBuildFile; fileRef = BC78D187137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.m */; };
BC78D18B137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.m in Sources */ = {isa = PBXBuildFile; fileRef = BC78D187137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.m */; };
BC8778E7136CDE4400549A6D /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = BC8778E5136CDE4400549A6D /* InfoPlist.strings */; };
BC8778F2136CDE4400549A6D /* JSON.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BC8778DA136CDE4400549A6D /* JSON.framework */; };
BC8778F8136CDE4400549A6D /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = BC8778F6136CDE4400549A6D /* InfoPlist.strings */; };
Expand Down Expand Up @@ -160,6 +164,8 @@
BC651A6D125A502700280F5C /* SBJsonStreamWriter.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SBJsonStreamWriter.m; sourceTree = "<group>"; };
BC74B6EB0FC9FAD0000BD3DD /* MaxDepthTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MaxDepthTest.m; sourceTree = "<group>"; };
BC74B7AD0FCA8B89000BD3DD /* ProxyTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ProxyTest.m; sourceTree = "<group>"; };
BC78D186137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SBJsonStreamWriterAccumulator.h; sourceTree = "<group>"; };
BC78D187137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SBJsonStreamWriterAccumulator.m; sourceTree = "<group>"; };
BC81A69512AF019F005C034C /* StreamParserIntegrationTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = StreamParserIntegrationTest.m; path = Tests/StreamParserIntegrationTest.m; sourceTree = SOURCE_ROOT; };
BC8778DA136CDE4400549A6D /* JSON.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = JSON.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BC8778E1136CDE4400549A6D /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -367,6 +373,8 @@
535157A40F6EE4A800C8AABD /* SBJsonWriter.m */,
BC651A6C125A502700280F5C /* SBJsonStreamWriter.h */,
BC651A6D125A502700280F5C /* SBJsonStreamWriter.m */,
BC78D186137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.h */,
BC78D187137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.m */,
BCCEBEFB12B3E14500263D0F /* SBJsonStreamWriterState.h */,
BCCEBEFC12B3E14500263D0F /* SBJsonStreamWriterState.m */,
BCA822C8127D68F600DD3FD0 /* SBJsonTokeniser.h */,
Expand Down Expand Up @@ -652,6 +660,7 @@
BC2BC05E136CE6EB0073BC4B /* SBJsonTokeniser.h in Headers */,
BC2BC061136CE6EB0073BC4B /* SBJsonStreamParserState.h in Headers */,
BCC77C181376526700EFEB38 /* SBJsonStreamParserAccumulator.h in Headers */,
BC78D189137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand All @@ -671,6 +680,7 @@
BC024D59136CE06A001FD388 /* SBJsonTokeniser.h in Headers */,
BC024D5C136CE06A001FD388 /* SBJsonStreamParserState.h in Headers */,
BCC77C171376526700EFEB38 /* SBJsonStreamParserAccumulator.h in Headers */,
BC78D188137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -884,6 +894,7 @@
BC2BC057136CE6D40073BC4B /* SBJsonStreamParserState.m in Sources */,
BC2BC058136CE6D40073BC4B /* SBJsonStreamParserAdapter.m in Sources */,
BCC77C1A1376526700EFEB38 /* SBJsonStreamParserAccumulator.m in Sources */,
BC78D18B137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -918,6 +929,7 @@
BC87790A136CDF9D00549A6D /* SBJsonStreamParserState.m in Sources */,
BC87790B136CDF9D00549A6D /* SBJsonStreamParserAdapter.m in Sources */,
BCC77C191376526700EFEB38 /* SBJsonStreamParserAccumulator.m in Sources */,
BC78D18A137851BF00F5DBA3 /* SBJsonStreamWriterAccumulator.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down

0 comments on commit 4833d35

Please sign in to comment.