new JSONDataType()
JSON data type.
Accessed via:
diffusion.datatypes.json();
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.
- Since:
-
- 5.7
Extends
Methods
-
deltaType(name, delta) → {diffusion.datatypes.DeltaType}
-
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 dela support will be derived - Inherited From:
Returns:
the delta supportExamples
// Get by name var deltas = datatype.deltaType("binary");
// Get by type var deltas = datatype.deltaType(delta);
-
from(object) → {diffusion.datatypes.JSON}
-
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.
Parameters:
Name Type Description object
Object The object data Returns:
a JSON data-type instanceExamples
// Datatype instance 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) → {diffusion.datatypes.JSON}
-
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 instanceExample
// Datatype instance from a JSON string. var value = jsondatatype.fromJsonString("{\"foo\" : \"bar\"}"); // The value contains the parsed object representation value.get(); // => { foo : "bar" }
-
name() → {String}
-
The external type identifier.
- Inherited From:
Returns:
The name of this datatype.- Type
- String
-
readValue(input, offset, length) → {Object}
-
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 typeReturns:
An instance of this data type value- Type
- Object
-
writeValue(value) → {Buffer}
-
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 serialisedReturns:
The serialised value as a buffer- Type
- Buffer