picofuse-objc

Instance Methods | Class Methods | List of all members
NXArchive Class Reference

Writes objects out as JSON or binary, and reads them back. More...

#import <Foundation/Foundation.h>

Inheritance diagram for NXArchive:
Inheritance graph
[legend]
Collaboration diagram for NXArchive:
Collaboration graph
[legend]

Instance Methods

(NXArchiveFormat) - format
 Returns the format.
 
(BOOL) - isEncoding
 Returns YES if it writes, or NO if it reads.
 
(BOOL) - failed
 Returns YES if there's been an error.
 
(NXString *) - failure
 Returns what the first error was, or nil if there hasn't been one.
 
(void) - failWithReason:
 Fails the archive, if it hasn't already, such as when a value which is decoded is out of range for the object, so it's nil.
 
(void) - setMaximumDepth:
 Sets the deepest that objects can be nested, which is 32 to start with. More...
 
(void) - setMaximumLength:
 Sets the longest, in bytes or objects, that a string, array or map which is read can be. More...
 
(BOOL) - encodeBool:forKey:
 Writes a value. More...
 
(BOOL) - encodeInt32:forKey:
 Writes a value. More...
 
(BOOL) - encodeInt64:forKey:
 Writes a value. More...
 
(BOOL) - encodeUnsignedInt32:forKey:
 Writes a value. More...
 
(BOOL) - encodeUnsignedInt64:forKey:
 Writes a value. More...
 
(BOOL) - encodeFloat32:forKey:
 Not a number or infinity fails.
 
(BOOL) - encodeFloat64:forKey:
 Not a number or infinity fails.
 
(BOOL) - encodeObject:forKey:
 Writes an object, which is nil, NXNull, a string, number, array, map, data or date, or adopts NXArchiving. More...
 
(BOOL) - encodeObject:asClass:forKey:
 As encodeObject:forKey:, but if the object is of a subclass of a class, the JSON object says which, as "@class", so it's decoded as that.
 
(BOOL) - containsKey:
 Returns YES if the JSON object being decoded has a key. More...
 
(BOOL) - decodeBoolForKey:
 Reads a value. More...
 
(int32_t) - decodeInt32ForKey:
 Reads a value. More...
 
(int64_t) - decodeInt64ForKey:
 Reads a value. More...
 
(uint32_t) - decodeUnsignedInt32ForKey:
 Reads a value. More...
 
(uint64_t) - decodeUnsignedInt64ForKey:
 Reads a value. More...
 
(float) - decodeFloat32ForKey:
 Reads a value. More...
 
(double) - decodeFloat64ForKey:
 Reads a value. More...
 
(id) - decodeObjectOfClass:forKey:
 Reads an object of a class, or of a subclass if the JSON names one with "@class". More...
 
(NXArray *) - decodeArrayOfClass:forKey:
 Reads an array whose objects are of a class, as decodeObjectOfClass:forKey: reads each. More...
 
(NXMap *) - decodeMapOfClass:forKey:
 Reads a map whose objects are of a class, as decodeObjectOfClass:forKey: reads each. More...
 
- Instance Methods inherited from NXObject
(void) - dealloc
 Frees the instance. More...
 
(NXZone *) - zone
 Returns the zone in which the instance was allocated. More...
 
(id) - retain
 Increases the retain count of the receiver. More...
 
(void) - release
 Decreases the retain count of the receiver. More...
 
(id) - autorelease
 Releases the receiver later, when the current autorelease pool of the thread is drained. More...
 
(id) - copy
 Returns a copy which can't be changed by anything else, which the caller owns and has to release. More...
 
(uintptr_t) - hash
 Returns a number for the object, which is the same for objects which are equal by isEqual:. More...
 
(NXString *) - description
 Returns a string which describes the instance, for logging. More...
 
- Instance Methods inherited from Object
(id) - init
 Initialize the instance, after allocation. More...
 
(Class) - class
 Returns the class of the instance. More...
 
(Class) - superclass
 Returns the superclass of the instance. More...
 
(BOOL) - isEqual:
 Compares the receiver to another object for equality. More...
 
(BOOL) - isKindOfClass:
 Returns a Boolean value that indicates whether the receiver is an instance of a given class. More...
 
(BOOL) - conformsTo:
 Checks if the receiver's class conforms to a protocol. More...
 
- Instance Methods inherited from <ObjectProtocol>
(BOOL) - respondsToSelector:
 Checks if the receiver responds to a selector. More...
 

Class Methods

(id) + JSONEncoderWithStream:indent:
 Returns a new archive which writes JSON to a stream. More...
 
(id) + JSONDecoderWithStream:
 Returns a new archive which reads JSON from a stream. More...
 
(id) + JSONDecoderWithObject:
 Returns a new archive which reads JSON which has been read already, as maps, arrays, strings, numbers and NXNull, such as several files merged.
 
(id) + binaryEncoderWithStream:
 Returns a new archive which writes binary to a stream.
 
(id) + binaryDecoderWithStream:
 Returns a new archive which reads binary from a stream, as each value is decoded.
 
(BOOL) + encodeObject:toStream:format:
 Writes an object to a stream, on one line. More...
 
(id) + decodeObjectOfClass:fromStream:format:
 Reads an object of a class, or a subclass, from a stream. More...
 
- Class Methods inherited from NXObject
(id) + alloc
 Allocates a new instance in the default zone, or if there isn't one, with sys_calloc(). More...
 
(id) + allocWithZone:
 Allocates a new instance in a specific zone. More...
 
(id) + new
 Allocates and initializes a new instance, as [[cls alloc] init] does. More...
 
(NXString *) + description
 Returns the name of the class.
 
- Class Methods inherited from Object
(void) + initialize
 Performs one-time initialization for the class. More...
 
(Class) + class
 Returns the class object. More...
 
(Class) + superclass
 Returns the superclass of the class. More...
 
(const char *) + name
 Returns the name of the class. More...
 
(BOOL) + conformsTo:
 Checks if the class conforms to a protocol. More...
 

Additional Inherited Members

- Protected Attributes inherited from Object
Class isa
 A pointer to the object's class structure. More...
 

Detailed Description

Writes objects out as JSON or binary, and reads them back.

The code which decodes says which class it expects, as with Go's json.Unmarshal, so the JSON is what would be written by hand: an object which adopts NXArchiving is a JSON object of its keys, and strings, numbers, null, arrays and maps are JSON's own. Data is a base64 string, and a date an ISO 8601 string. A map's keys have to be strings, and they're written in order.

Objects are written as a tree: one which is reached twice is written twice, and the maximum depth stops a cycle. The first error sticks: after it, encoding and decoding do nothing and give zero or nil, and failed and failure say what it was. JSON is read whole before anything is decoded, as keys can be in any order. A character above U+FFFF is written as UTF-8, and read from UTF-8 or from a \u escape of a UTF-16 surrogate pair. Floating point numbers are written with picofuse's printf, which writes 9 significant digits at most, so a float32 reads back as it was, but a float64 only to 9 digits.

Binary starts with "PF" and a version, which is 1. It has no keys: values are read back in the order they were written, so decode the same types, in the same order, as were encoded. Numbers are little-endian, and floating point numbers are exact. Each object starts with a 32-bit FNV-1a hash of its class's name, which is checked when it's read, so a subclass is read as itself. The classes which adopt NXArchiving are found the first time one is read, and if two of their names have the same hash, it panics. Renaming a class changes its hash, so archives written before can't be read. Lengths are checked against the maximum before anything is allocated.

Definition at line 77 of file NXArchive.h.

Method Documentation

◆ containsKey:()

- (BOOL) containsKey: (NXString *)  key

Returns YES if the JSON object being decoded has a key.

It's always YES for binary, which has no keys.

◆ decodeArrayOfClass:forKey:()

- (NXArray *) decodeArrayOfClass: (Class)  cls
forKey: (NXString *)  key 

Reads an array whose objects are of a class, as decodeObjectOfClass:forKey: reads each.

A null in the array is NXNull.

◆ decodeBoolForKey:()

- (BOOL) decodeBoolForKey: (NXString *)  key

Reads a value.

The key is nil for the value which is the whole archive.

Returns
The value, or zero if there's no such key, or it's null. If it's of another type, or out of range, the archive fails, and it's zero.

◆ decodeFloat32ForKey:()

- (float) decodeFloat32ForKey: (NXString *)  key

Reads a value.

The key is nil for the value which is the whole archive.

Returns
The value, or zero if there's no such key, or it's null. If it's of another type, or out of range, the archive fails, and it's zero.

◆ decodeFloat64ForKey:()

- (double) decodeFloat64ForKey: (NXString *)  key

Reads a value.

The key is nil for the value which is the whole archive.

Returns
The value, or zero if there's no such key, or it's null. If it's of another type, or out of range, the archive fails, and it's zero.

◆ decodeInt32ForKey:()

- (int32_t) decodeInt32ForKey: (NXString *)  key

Reads a value.

The key is nil for the value which is the whole archive.

Returns
The value, or zero if there's no such key, or it's null. If it's of another type, or out of range, the archive fails, and it's zero.

◆ decodeInt64ForKey:()

- (int64_t) decodeInt64ForKey: (NXString *)  key

Reads a value.

The key is nil for the value which is the whole archive.

Returns
The value, or zero if there's no such key, or it's null. If it's of another type, or out of range, the archive fails, and it's zero.

◆ decodeMapOfClass:forKey:()

- (NXMap *) decodeMapOfClass: (Class)  cls
forKey: (NXString *)  key 

Reads a map whose objects are of a class, as decodeObjectOfClass:forKey: reads each.

Its keys are strings.

◆ decodeObjectOfClass:forKey:()

- (id) decodeObjectOfClass: (Class)  cls
forKey: (NXString *)  key 

Reads an object of a class, or of a subclass if the JSON names one with "@class".

Parameters
clsThe class, or Nil for whatever is there.
keyThe key, or nil for the value which is the whole archive.
Returns
The object, or nil if there's no such key, or it's null. If it isn't of the class, the archive fails, and it's nil.

◆ decodeObjectOfClass:fromStream:format:()

+ (id) decodeObjectOfClass: (Class)  cls
fromStream: (NXStream *)  stream
format: (NXArchiveFormat)  format 

Reads an object of a class, or a subclass, from a stream.

Parameters
clsThe class, or Nil for whatever is there, as maps, arrays, strings and numbers.
streamThe stream to read from.
formatThe format of the stream.
Returns
The object, or nil if it couldn't be read or isn't of the class.

◆ decodeUnsignedInt32ForKey:()

- (uint32_t) decodeUnsignedInt32ForKey: (NXString *)  key

Reads a value.

The key is nil for the value which is the whole archive.

Returns
The value, or zero if there's no such key, or it's null. If it's of another type, or out of range, the archive fails, and it's zero.

◆ decodeUnsignedInt64ForKey:()

- (uint64_t) decodeUnsignedInt64ForKey: (NXString *)  key

Reads a value.

The key is nil for the value which is the whole archive.

Returns
The value, or zero if there's no such key, or it's null. If it's of another type, or out of range, the archive fails, and it's zero.

◆ encodeBool:forKey:()

- (BOOL) encodeBool: (BOOL)  value
forKey: (NXString *)  key 

Writes a value.

The key names it in a JSON object; it's nil for the value which is the whole archive.

Returns
YES, or NO if the archive has failed.

◆ encodeInt32:forKey:()

- (BOOL) encodeInt32: (int32_t)  value
forKey: (NXString *)  key 

Writes a value.

The key names it in a JSON object; it's nil for the value which is the whole archive.

Returns
YES, or NO if the archive has failed.

◆ encodeInt64:forKey:()

- (BOOL) encodeInt64: (int64_t)  value
forKey: (NXString *)  key 

Writes a value.

The key names it in a JSON object; it's nil for the value which is the whole archive.

Returns
YES, or NO if the archive has failed.

◆ encodeObject:forKey:()

- (BOOL) encodeObject: (id)  object
forKey: (NXString *)  key 

Writes an object, which is nil, NXNull, a string, number, array, map, data or date, or adopts NXArchiving.

Returns
YES, or NO if the archive has failed, or the object can't be archived.

◆ encodeObject:toStream:format:()

+ (BOOL) encodeObject: (id)  object
toStream: (NXStream *)  stream
format: (NXArchiveFormat)  format 

Writes an object to a stream, on one line.

Returns
YES, or NO if it couldn't be written.

◆ encodeUnsignedInt32:forKey:()

- (BOOL) encodeUnsignedInt32: (uint32_t)  value
forKey: (NXString *)  key 

Writes a value.

The key names it in a JSON object; it's nil for the value which is the whole archive.

Returns
YES, or NO if the archive has failed.

◆ encodeUnsignedInt64:forKey:()

- (BOOL) encodeUnsignedInt64: (uint64_t)  value
forKey: (NXString *)  key 

Writes a value.

The key names it in a JSON object; it's nil for the value which is the whole archive.

Returns
YES, or NO if the archive has failed.

◆ JSONDecoderWithStream:()

+ (id) JSONDecoderWithStream: (NXStream *)  stream

Returns a new archive which reads JSON from a stream.

It reads all of it when the first value is decoded, so limits can be set first.

◆ JSONEncoderWithStream:indent:()

+ (id) JSONEncoderWithStream: (NXStream *)  stream
indent: (unsigned)  indent 

Returns a new archive which writes JSON to a stream.

Parameters
streamThe stream, which it writes to as values are encoded.
indentThe number of spaces for each level, or zero to write one line.

◆ setMaximumDepth:()

- (void) setMaximumDepth: (unsigned)  depth

Sets the deepest that objects can be nested, which is 32 to start with.

Set it before the first value is encoded or decoded.

◆ setMaximumLength:()

- (void) setMaximumLength: (size_t)  length

Sets the longest, in bytes or objects, that a string, array or map which is read can be.

There's no limit to start with. Set it before the first value is decoded.


The documentation for this class was generated from the following file: