Class: JSONDataType

diffusion.datatypes. JSONDataType


new JSONDataType()

JSON data type.

Accessed via: diffusion.datatypes.json();

The JSON Data Type provides diffusion.datatypes.JSON values, which are a wrapper around native JSON objects.

For efficiency, the JSON value is serialized in binary form following the CBOR specification.

The implementation provides support for binary deltas.

JSON instances defer parsing of underlying binary data until required. If the data is not valid, an Error may be thrown when diffusion.datatypes.JSON#get is called.

Properties:
Name Type Description
diffusion.datatypes.JSONDataType.JSON diffusion.datatypes.JSON

The JSON data type value class

Since:
  • 5.7

Extends

Methods


canReadAs(valueType)

Test whether this data type is compatible with valueType. Compatibility with a valueType means than any valid binary representation of a value can be read as an instance of valueType.

Every data type should be compatible with the following:

  • Value Type – the class corresponding to the data type's value type.

For a data type with a value type of X, readAs(X, buffer) is equivalent to readValue(buffer).

Parameters:
Name Type Description
valueType function

the type to check

Since:
  • 6.0
Inherited From:
Returns:

true if a binary representation created by this data type can read as an instance of valueType

Type
Boolean

deltaType( [name] [, delta])

Obtain a diffusion.datatypes.DeltaType by name or delta type.

Parameters:
Name Type Argument Description
name String <optional>

The name, as returned by diffusion.datatypes.DeltaType#name

delta Object <optional>

The delta, from which a type-appropriate delta support will be derived

Inherited From:
Returns:

the delta support

Type
diffusion.datatypes.DeltaType
Examples
// Get by name
var deltas = datatype.deltaType("binary");
// Get by type
var deltas = datatype.deltaType(delta);

from(object)

Returns a new diffusion.datatypes.JSON instance from a native JS object.

Passing a string will produce a JSON instance encoding a single string token. To produce a JSON datatype instance from a JSON string, use diffusion.datatypes.JSONDataType#fromJsonString instead.

This is useful in cases where providing the raw value may be ambiguous for SDK methods that infer the datatype from provided arguments, such as Session.messages#sendRequest.

Parameters:
Name Type Description
object Object

The object data

Returns:

a JSON data-type instance

Type
diffusion.datatypes.JSON
Examples
// Value from object
var value = jsondatatype.from({
    foo : "bar",
    baz : [1, 2, 3]
});
// Datatype instance from string
var value = jsondatatype.from("this is a simple string");

fromJsonString(string)

Returns a new diffusion.datatypes.JSON instance from a JSON string.

Precision for numeric types is lost in the translation to the internal CBOR binary form and non-significant white space is not preserved.

Parameters:
Name Type Description
string String

The JSON string

Since:
  • 5.9
Returns:

A JSON data-type instance

Type
diffusion.datatypes.JSON
Example
// Datatype instance from a JSON string.
var value = jsondatatype.fromJsonString("{\"foo\" : \"bar\"}");

// The value contains the parsed object representation
value.get(); // => { foo : "bar" }

name()

The external type identifier.

Inherited From:
Returns:

The name of this datatype.

Type
String

readAs(valueType, buffer [, offset] [, length])

Create a value of a compatible class from binary.

Parameters:
Name Type Argument Default Description
valueType function

the type of the result

buffer Buffer

the binary data

offset Number <optional>
0

the offset to read data from the buffer

length Number <optional>
buffer.length

the length of data to read from the buffer

Since:
  • 6.0
Inherited From:
Throws:

Error if valueType is incompatible with this data type, or buffer does not represent a valid value.

Returns:

the value in the form of the specified type

Type
Object

readValue(input [, offset] [, length])

Parse a value from binary.

Parameters:
Name Type Argument Description
input Buffer

The binary data

offset Number <optional>

The offset to start reading from the provided buffer

length Number <optional>

The length of the data to read

Inherited From:
Throws:

Error if the data is invalid for this type

Returns:

An instance of this data type value

Type
Object

writeValue(value)

Serialise a value to binary

Parameters:
Name Type Description
value Object

The value to serialise

Inherited From:
Throws:

Error if the value can not be serialised

Returns:

The serialised value as a buffer

Type
Buffer