Inherits from NSObject
Declared in SBJsonStreamWriter.h

Overview

The Stream Writer class.

Accepts a stream of messages and writes JSON of these to its delegate object.

This class provides a range of high-, mid- and low-level methods. You can mix and match calls to these. For example, you may want to call writeArrayOpen to start an array and then repeatedly call writeObject: with various objects before finishing off with a writeArrayClose call.

Objective-C types are mapped to JSON types in the following way:

  • NSNull –> null
  • NSString –> string
  • NSArray –> array
  • NSDictionary –> object
  • NSNumber’s -initWithBool:YES –> true
  • NSNumber’s -initWithBool:NO –> false
  • NSNumber –> number

NSNumber instances created with the -numberWithBool: method are converted into the JSON boolean “true” and “false” values, and vice versa. Any other NSNumber instances are converted to a JSON number the way you would expect.

@warning: In JSON the keys of an object must be strings. NSDictionary keys need not be, but attempting to convert an NSDictionary with non-string keys into JSON will throw an exception.*

Tasks

Other Methods

Private Methods

Properties

delegate

delegate to receive JSON output Delegate that will receive messages with output.

@property (unsafe_unretained) id<> delegate

Declared In

SBJsonStreamWriter.h

error

Contains the error description after an error has occured.

@property (copy) NSString *error

Declared In

SBJsonStreamWriter.h

humanReadable

Whether we are generating human-readable (multiline) JSON.

@property BOOL humanReadable

Discussion

Set whether or not to generate human-readable JSON. The default is NO, which produces JSON without any whitespace between tokens. If set to YES, generates human-readable JSON with linebreaks after each array value and dictionary key/value pair, indented two spaces per nesting level.

Declared In

SBJsonStreamWriter.h

maxDepth

The maximum recursing depth.

@property NSUInteger maxDepth

Discussion

Defaults to 512. If the input is nested deeper than this the input will be deemed to be malicious and the parser returns nil, signalling an error. (“Nested too deep”.) You can turn off this security feature by setting the maxDepth value to 0.

Declared In

SBJsonStreamWriter.h

sortKeys

Whether or not to sort the dictionary keys in the output.

@property BOOL sortKeys

Discussion

If this is set to YES, the dictionary keys in the JSON output will be in sorted order. (This is useful if you need to compare two structures, for example.) The default is NO.

Declared In

SBJsonStreamWriter.h

sortKeysComparator

An optional comparator to be used if sortKeys is YES.

@property (copy) NSComparator sortKeysComparator

Discussion

If this is nil, sorting will be done via @selector(compare:).

Declared In

SBJsonStreamWriter.h

state

@property (nonatomic, unsafe_unretained) SBJsonStreamWriterState *state

stateStack

@property (nonatomic, readonly, strong) NSMutableArray *stateStack

Instance Methods

appendBytes:length:

- (void)appendBytes:(const void *)bytes length:(NSUInteger)length

writeArray:

Write an NSArray to the JSON stream.

- (BOOL)writeArray:(NSArray *)array

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeArrayClose

Close the current Array being written

- (BOOL)writeArrayClose

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeArrayOpen

Start writing an Array to the stream

- (BOOL)writeArrayOpen

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeBool:

Write a boolean to the stream

- (BOOL)writeBool:(BOOL)x

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeNull

Write a null to the stream

- (BOOL)writeNull

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeNumber:

Write a Number to the stream

- (BOOL)writeNumber:(NSNumber *)n

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeObject:

Write an NSDictionary to the JSON stream.

- (BOOL)writeObject:(NSDictionary *)dict

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeObjectClose

Close the current object being written

- (BOOL)writeObjectClose

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeObjectOpen

Start writing an Object to the stream

- (BOOL)writeObjectOpen

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeString:

Write a String to the stream

- (BOOL)writeString:(NSString *)s

Return Value

YES if successful, or NO on failure

Declared In

SBJsonStreamWriter.h

writeValue:

- (BOOL)writeValue:(id)v