Inherits from NSObject
Declared in JPJsonWriter.h
JPJsonWriter.mm

Overview

A JPJsonWriter class will be used to serialize a Foundation object into a JSON text.

JPJsonWriter Options

  • JPJsonWriterPrettyPrint

    If this option is set, a more conveniently readable output is generated through inserting tabs, new lines and indents.

  • JPJsonWriterSortKeys

    If this option is set, JSON objects will be sorted by key.

  • JPJsonWriterEscapeUnicodeCharacters

    If this option is set, the generator escapes Unicode characters in JSON strings if they cannot be represented in one code unit. This option should only be used when encoding in UTF-8. If this option is set, the output effectively becomes ASCII compatible if encoded in UTF-8. (Feature not implemented in this version)

  • JPJsonWriterWriteBOM

    If this option is set, the generator prefixes the output with a BOM according the current Unicode encoding.

  • JPJsonWriterEscapeSolidus

    If this option is set, the generator will escape the “solidus” (/) character.

JPJsonWriter (Extension) defines some additional methods which help in serializing objects.

Tasks

Other Methods

Extension Methods

Class Methods

dataWithObject:encoding:options:error:

Serializes the Foundation object with the specified Unicode encoding into a NSData object and returns it.

+ (NSData *)dataWithObject:(id)object encoding:(JPUnicodeEncoding)encoding options:(JPJsonWriterOptions)options error:(__autoreleasing NSError **)error

Parameters

object

A hierarchy of Foundation objects which all implement the JPJsonSerializableProtocol. The root object shall be a either a NSDictionary, NSArray or a corresponding subclass or any other object which implements `JPJsonSerializableProtocol and poses as a dictionary or array.

encoding

One of the constants defined in enum JPUnicodeEncoding which specifies the Unicode encoding scheme for the generated output.

Warning: Currently, only UTF-8 encoding is supported.

options

A bit mask specifying options for serializing. For possible values see “JPJsonWriterOptions”.

error

A pointer to a NSError object. If this is not NULL, and if an error occurred during serializing the parameter error contains an NSError object describing the issue.

Return Value

A NSData object representing the JSON in parameter object as text, or nil if an error occurred.

Declared In

JPJsonWriter.h

dataWithObject:options:error:

Serializes the Foundation object as UTF-8 Unicode encoded string into a NSData object and returns it.

+ (NSData *)dataWithObject:(id)object options:(JPJsonWriterOptions)options error:(__autoreleasing NSError **)error

Parameters

object

A hierarchy of Foundation objects which all implement the JPJsonSerializableProtocol. The root object shall be a either a NSDictionary, NSArray or a corresponding subclass or any other object which implements `JPJsonSerializableProtocol and poses as a dictionary or array.

options

A bit mask specifying options for serializing. For possible values see “JPJsonWriterOptions”.

error

A pointer to a NSError object. If this is not NULL, and if an error occurred during serializing the parameter error contains an NSError object describing the issue.

Return Value

A NSData object representing the JSON in parameter object as text, or nil if an error occurred.

Declared In

JPJsonWriter.h

serializeObject:toStream:encoding:options:error:

Serializes the Foundation object with the specified Unicode encoding and options.

+ (NSUInteger)serializeObject:(id)object toStream:(NSOutputStream *)stream encoding:(JPUnicodeEncoding)encoding options:(JPJsonWriterOptions)options error:(__autoreleasing NSError **)error

Parameters

object

A hierarchy of Foundation objects which all implement the JPJsonSerializableProtocol. The root object shall be a either a NSDictionary, NSArray or a corresponding subclass or any other object which implements `JPJsonSerializableProtocol and poses as a dictionary or array.

stream

An output stream. The stream should be unopened.

encoding

One of the constants defined in enum JPUnicodeEncoding which specifies the Unicode encoding scheme for the generated output.

Warning: Currently, only UTF-8 encoding is supported.

options

A bit mask specifying options for serializing. For possible values see “JPJsonWriterOptions”.

error

A pointer to a NSError object. If this is not NULL, and if an error occurred during serializing the parameter error contains an NSError object describing the issue.

Return Value

The number of bytes written into the stream, or zero if an error occurred.

Declared In

JPJsonWriter.h

serializeObject:toStream:options:error:

Serializes the Foundation object as UTF-8 Unicode encoded string into the stream and returns the number of bytes written.

+ (NSUInteger)serializeObject:(id)object toStream:(NSOutputStream *)stream options:(JPJsonWriterOptions)options error:(__autoreleasing NSError **)error

Parameters

object

A hierarchy of Foundation objects which all implement the JPJsonSerializableProtocol. The root object shall be a either a NSDictionary, NSArray or a corresponding subclass or any other object which implements `JPJsonSerializableProtocol and poses as a dictionary or array.

stream

An output stream. The stream must be opened. On return, the stream is closed.

options

A bit mask specifying options for serializing. For possible values see “JPJsonWriterOptions”.

error

A pointer to a NSError object. If this is not NULL, and if an error occurred during serializing the parameter error contains an NSError object describing the issue.

Return Value

The number of bytes written into the stream, or zero if an error occurred.

Declared In

JPJsonWriter.h

serializeObjectAsJSONArray:buffer:encoding:options:level:

Serialize an object which shall be represented as a JSON Array. This method already implements the boiler plate code necessary to serialize an object as a JSON Array.

+ (int)serializeObjectAsJSONArray:(id)object buffer:(id<JPJsonStreambufferProtocol>)streambuf encoding:(JPUnicodeEncoding)encoding options:(JPJsonWriterOptions)options level:(int)level

Parameters

object

The “array like” object which shall be serialized

streambuf

A stream-buffer into which the characters will be written.

encoding

The output encoding of the character sequence for the serialized object.

options

Options that tailor the format of the output.

level

The current level of the hierarchy of the object structure. This is used only when additional format characters shall be inserted into the JSON text, for example when “pretty printing”.

Return Value

Returns 0 on success, otherwise -1.

Parmeter object shall respond to message count and shall implement the protocol NSFastEnumeration.

Declared In

JPJsonWriterExtensions.h

serializeObjectAsJSONObject:buffer:encoding:options:level:

Serialize an object which shall be represented as a JSON Object. This method already implements the boiler plate code necessary to serialize an object as a JSON Array.

+ (int)serializeObjectAsJSONObject:(id)object buffer:(id<JPJsonStreambufferProtocol>)streambuf encoding:(JPUnicodeEncoding)encoding options:(JPJsonWriterOptions)options level:(int)level

Parameters

object

The “dictionary like” object which shall be serialized

streambuf

A stream-buffer into which the characters will be written.

encoding

The output encoding of the character sequence for the serialized object.

options

Options that tailor the format of the output.

level

The current level of the hierarchy of the object structure. This is used only when additional format characters shall be inserted into the JSON text, for example when “pretty printing”.

Return Value

Returns zero on success, otherwise -1.

Parameter object shall respond to message count and message objectForKey: and shall implement the protocol NSFastEnumeration.

Declared In

JPJsonWriterExtensions.h