SBJsonStreamWriter Class Reference
| 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
-
stateproperty -
stateStackproperty -
delegatedelegate to receive JSON output Delegate that will receive messages with output.
property -
maxDepthThe maximum recursing depth.
property -
humanReadableWhether we are generating human-readable (multiline) JSON.
property -
sortKeysWhether or not to sort the dictionary keys in the output.
property -
sortKeysComparatorAn optional comparator to be used if sortKeys is YES.
property -
errorContains the error description after an error has occured.
property -
– writeObject:Write an NSDictionary to the JSON stream.
-
– writeArray:Write an NSArray to the JSON stream.
-
– writeObjectOpenStart writing an Object to the stream
-
– writeObjectCloseClose the current object being written
-
– writeArrayOpenStart writing an Array to the stream
-
– writeArrayCloseClose the current Array being written
-
– writeNullWrite a null to the stream
-
– writeBool:Write a boolean to the stream
-
– writeNumber:Write a Number to the stream
-
– writeString:Write a String to the stream
Private Methods
Properties
delegate
delegate to receive JSON output Delegate that will receive messages with output.
@property (unsafe_unretained) id<> delegate
Declared In
SBJsonStreamWriter.herror
Contains the error description after an error has occured.
@property (copy) NSString *error
Declared In
SBJsonStreamWriter.hhumanReadable
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.hmaxDepth
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.hsortKeys
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.hsortKeysComparator
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.hInstance Methods
writeArray:
Write an NSArray to the JSON stream.
- (BOOL)writeArray:(NSArray *)array
Return Value
YES if successful, or NO on failure
Declared In
SBJsonStreamWriter.hwriteArrayClose
Close the current Array being written
- (BOOL)writeArrayClose
Return Value
YES if successful, or NO on failure
Declared In
SBJsonStreamWriter.hwriteArrayOpen
Start writing an Array to the stream
- (BOOL)writeArrayOpen
Return Value
YES if successful, or NO on failure
Declared In
SBJsonStreamWriter.hwriteBool:
Write a boolean to the stream
- (BOOL)writeBool:(BOOL)x
Return Value
YES if successful, or NO on failure
Declared In
SBJsonStreamWriter.hwriteNull
Write a null to the stream
- (BOOL)writeNull
Return Value
YES if successful, or NO on failure
Declared In
SBJsonStreamWriter.hwriteNumber:
Write a Number to the stream
- (BOOL)writeNumber:(NSNumber *)n
Return Value
YES if successful, or NO on failure
Declared In
SBJsonStreamWriter.hwriteObject:
Write an NSDictionary to the JSON stream.
- (BOOL)writeObject:(NSDictionary *)dict
Return Value
YES if successful, or NO on failure
Declared In
SBJsonStreamWriter.hwriteObjectClose
Close the current object being written
- (BOOL)writeObjectClose
Return Value
YES if successful, or NO on failure
Declared In
SBJsonStreamWriter.hwriteObjectOpen
Start writing an Object to the stream
- (BOOL)writeObjectOpen
Return Value
YES if successful, or NO on failure
Declared In
SBJsonStreamWriter.h