Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface JSON

Immutable JSON data. The value is stored internally as a buffer.

JSON is "JavaScript Object Notation", a lightweight data-interchange format. See www.json.org.

To create an instance from an object, obtain a JSONDataType implementation from diffusion.datatypes.json() and call JSONDataType.from.

The encapsulated value can be accessed by calling JSON.get.

CBOR representation

Internally the value is stored and transmitted not as a JSON string, but in CBOR format to reduce memory and network overhead. CBOR (Concise Binary Object Representation) is a standardized format for binary representation of structured data defined by RFC 7049. See www.cbor.io.

Rather than working with JSON strings it is possible, and usually preferable, for applications to work directly with the underlying CBOR-format binary representation. This avoids creating intermediate JSON strings, and allows access to CBOR features such as the byte string data type.

Each JSON value is represented as a single CBOR data item. CBOR supports composite data types just like JSON, so this data item can be an array or a map of other data items. The JSON null value is represented by the CBOR null value.

A particular advantage of working directly with the CBOR-format data is that binary data can be written to the value as a data item using the CBOR byte string type. The data item will be stored as part of the JSON value in binary form and transmitted without further conversion.

since

5.7

Hierarchy

Index

Methods

apply

  • Apply a delta to this JSON value to create a new value.

    Convenient equivalent to: diffusion.datatypes.JSON().deltaType(delta).apply(this, delta);

    throws

    an InvalidDataError if the delta is invalid

    Parameters

    • delta: BinaryDelta

      the binary delta to apply to this value

    Returns JSON

    a new instance derived from applying the delta to this value

asArray

  • asArray(): Uint8Array
  • Get a copy of the array containing this value.

    Returns Uint8Array

    This value in binary form

asBuffer

  • asBuffer(): Buffer
  • Get a copy of the buffer containing this value.

    deprecated

    since version 6.11. Buffer has been replaced by Uint8Array. This will be removed in a future release.

    Returns Buffer

    This value in binary form

copyTo

  • copyTo(target: Uint8Array, offset?: undefined | number): void
  • Copy the binary data to a provided buffer.

    Parameters

    • target: Uint8Array

      the buffer to copy data to

    • Optional offset: undefined | number

      the position in the target buffer at which data will be copied

    Returns void

diff

  • Compare this JSON value with an earlier version to create a delta.

    Convenient equivalent to: diffusion.datatypes.json().deltaType(type).diff(original, this);

    Standard JSON objects may also be provided as the value to diff instead of a JSON instance.

    Example:

    const binaryDelta = jsonValue.diff({ foo : 'bar' });

    Example:

    const jsonDelta = jsonValue.diff({ foo : 'bar' }, 'json');
    throws

    an InvalidDataError if the type is json but the diff could not be parsed

    Parameters

    • original: any

      the value to diff against this

    • Optional type: undefined | string

      the type of delta to generate (default = 'binary')

    Returns BinaryDelta | JSONDelta

    a delta representing the difference between this and the provided value

get

  • get(): any
  • Get this instance's value. Use this method to access the provided data when a JSON instance is received through the API.

    Example:

    session.addStream('foo', diffusion.datatypes.json())
           .on('value', function(path, spec, value) {
        // Get the actual value from the JSON instance
        const data = value.get();
    });
    throws

    an InvalidDataError if the value cannot be read

    Returns any

    the JSON value

jsonDiff

  • Compare this JSON value with an earlier version to create a structural json delta.

    Convenient equivalent to: this.diff(original, 'json');

    Standard JSON objects may also be provided as the value to diff instead of a JSON instance.

    Example:

    const delta = jsonValue.jsonDiff({ foo : 'bar' });

    Parameters

    • original: any

      the value to diff against this

    Returns JSONDelta

    a delta representing the difference between this and the provided value

length

  • length(): number
  • Get the number of bytes

    Returns number

    The length of the data in bytes