Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface JSONDataType

JSON data type.

Accessed via: diffusion.datatypes.json();

The JSON Data Type provides 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 JSON.get is called.

since

5.7

Hierarchy

Index

Properties

JSON

JSON: object

The JSON data type value class

Type declaration

Methods

canReadAs

  • canReadAs(valueType: object): boolean
  • 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).

    since

    6.0

    Parameters

    • valueType: object

      the type to check

    Returns boolean

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

deltaType

  • Obtain a DeltaType by name or delta type.

    Example:

    // Get by name
    var deltas = datatype.deltaType("binary");

    Example:

    // Get by type
    var deltas = datatype.deltaType(delta);

    Parameters

    • Optional name: undefined | string

      the name, as returned by DeltaType.name

    Returns DeltaType<JSON, any, JSON>

    the delta type

from

  • from(object: any): JSON
  • Returns a new 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 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 Messages.sendRequest.

    Example:

    // Value from object
    var value = jsondatatype.from({
        foo : "bar",
        baz : [1, 2, 3]
    });

    Example:

    // Datatype instance from string
    var value = jsondatatype.from("this is a simple string");

    Parameters

    • object: any

      the object data

    Returns JSON

    a JSON data-type instance

fromJsonString

  • fromJsonString(json: string): JSON
  • Returns a new 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.

    Example:

    // Datatype instance from a JSON string.
    var value = jsondatatype.fromJsonString("{\"foo\" : \"bar\"}");
    
    // The value contains the parsed object representation
    value.get(); // => { foo : "bar" }
    since

    5.9

    Parameters

    • json: string

      the JSON string

    Returns JSON

    a JSON data-type instance

name

  • name(): string
  • The external type identifier.

    Returns string

    the name of this datatype

readAs

  • readAs<T>(valueType: object, buffer: Uint8Array, offset?: undefined | number, length?: undefined | number): T | null
  • readAs<T>(valueType: object, buffer: JSON): T | null
  • Create a value of a compatible class from binary.

    throws

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

    since

    6.0

    Type parameters

    • T

    Parameters

    • valueType: object

      the type of the result

    • buffer: Uint8Array

      the binary data

    • Optional offset: undefined | number

      the offset to start reading from the provided buffer (default = 0)

    • Optional length: undefined | number

      the length of the data to read (default = input.length)

    Returns T | null

    the value in the form of the specified type

  • Type parameters

    • T

    Parameters

    • valueType: object
    • buffer: JSON

    Returns T | null

readValue

  • readValue(input: Uint8Array, offset?: undefined | number, length?: undefined | number): JSON | null
  • readValue(input: JSON): JSON | null
  • Parse a value from binary.

    throws

    an InvalidDataError error if the data is invalid for this type

    Parameters

    • input: Uint8Array

      the binary data

    • Optional offset: undefined | number

      the offset to start reading from the provided buffer (default = 0)

    • Optional length: undefined | number

      the length of the data to read (default = input.length)

    Returns JSON | null

    an instance of this data type value

  • Parameters

    Returns JSON | null

writeValue

  • writeValue(value: any | undefined | null): Buffer
  • Serialise a value to binary

    throws

    an IllegalArgumentError if the value cannot be serialised

    deprecated

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

    Parameters

    • value: any | undefined | null

      the value to serialise. For primitive and JSON datatypes the value can be undefined or null. In this case a null value will be serialised.

    Returns Buffer

    the serialised value as a buffer

writeValueToArray

  • writeValueToArray(value: any | undefined | null): Uint8Array
  • Serialise a value to binary

    throws

    an IllegalArgumentError if the value cannot be serialised

    Parameters

    • value: any | undefined | null

      the value to serialise. For primitive and JSON datatypes the value can be undefined or null. In this case a null value will be serialised.

    Returns Uint8Array

    the serialised value as a buffer