Class: JSON

diffusion.datatypes. JSON


new JSON()

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

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

The encapsulated value can be accessed by calling diffusion.datatypes.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

Extends

Methods


apply(delta)

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

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

Parameters:
Name Type Description
delta Object

The binary delta to apply to this value

Overrides:
Throws:

Error if the delta is invalid

Returns:

A new instance derived from applying the delta to this value

Type
DataTypes.JSON

diff(original [, type])

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 diffusion.datatypes.JSON instance.

Parameters:
Name Type Argument Default Description
original diffusion.datatypes.JSON | Object

The value to diff against this

type String <optional>
"binary"

The type of delta to generate

Overrides:
Returns:

A delta representing the difference between this and the provided value

Type
diffusion.datatypes.BinaryDelta
Examples
var binaryDelta = jsonValue.diff({ foo : "bar" });
var jsonDelta = jsonValue.diff({ foo : "bar" }, "json");

get()

Get this instance's value. Use this method to access the provided data when a diffusion.datatypes.JSON instance is received through the API.

Overrides:
Returns:

The JSON value

Type
Object
Example
session.subscribe("foo").asType(diffusion.datatypes.json()).on('value', function(path, spec, value) {
    // Get the actual value from the JSON instance
    var data = value.get();
});

jsonDiff(original)

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 diffusion.datatypes.JSON instance.

Parameters:
Name Type Description
original diffusion.datatypes.JSON | Object

The value to diff against this

Returns:

A delta representing the difference between this and the provided value

Type
diffusion.datatypes.JSONDelta
Example
var delta = jsonValue.jsonDiff({ foo : "bar" });