Note: in this specification CBOR data items are subsequently referred to as "CBOR objects".
"new" operator with a wrapper object
is flagged as an error.
The following table lists the wrapper objects and their relation to
the CBOR objects (here expressed in
CDDL [RFC8610| CDDL | Wrapper Object | JavaScript | Notes |
|---|---|---|---|
int |
CBOR.Int | Number |
1 |
bigint |
CBOR.BigInt | BigInt |
1 |
float |
CBOR.Float | Number |
2, 3 |
| CBOR.NonFinite | BigInt |
3 |
|
bstr |
CBOR.Bytes | Uint8Array |
|
tstr |
CBOR.String | String |
|
bool |
CBOR.Boolean | Boolean |
|
null |
CBOR.Null | ||
[] |
CBOR.Array | ||
{} |
CBOR.Map | ||
#6.n |
CBOR.Tag | ||
#7.n |
CBOR.Simple |
The wrapper concept adds strict type checking to the API. That is, if an application expects a CBOR integer and calls getInt32(), a CborException will be thrown if the referenced object is not an instance of CBOR.Int or if the CBOR integer is out of range for signed 32-bit objects.
Also see Common Wrapper Methods and Time Objects.| Syntax | CBOR.Int(value) | |
|---|---|---|
| Description | Constructor. Creates a CBOR integer object.
Also see Integer Numbers. | |
| Parameter | Type | Description |
| value | Number | Integer to be wrapped. |
| Returns | Description | |
| CBOR.Int | Object. | |
| Syntax | getInt53() |
|---|---|
| Description | Get CBOR integer. If the return value is outside the range Number.MIN_SAFE_INTEGER (-9007199254740991) to Number.MAX_SAFE_INTEGER (9007199254740991), a CborException is thrown. Also see getBigInt(). Since 53-bit integers are specific to JavaScript, this method should be used with caution in cross-platform scenarios. |
| Returns | Description |
| Number | Decoded integer. |
| Syntax | getInt8() |
|---|---|
| Description | Get CBOR integer. |
| Returns | Description |
| Number | Decoded integer. |
| Syntax | getUint8() |
|---|---|
| Description | Get CBOR integer. |
| Returns | Description |
| Number | Decoded integer. |
| Syntax | getInt16() |
|---|---|
| Description | Get CBOR integer. |
| Returns | Description |
| Number | Decoded integer. |
| Syntax | getUint16() |
|---|---|
| Description | Get CBOR integer. |
| Returns | Description |
| Number | Decoded integer. |
| Syntax | getInt32() |
|---|---|
| Description | Get CBOR integer. |
| Returns | Description |
| Number | Decoded integer. |
| Syntax | getUint32() |
|---|---|
| Description | Get CBOR integer. |
| Returns | Description |
| Number | Decoded integer. |
| Syntax | getInt64() |
|---|---|
| Description | Get CBOR integer. If the return value is outside the range -0x8000000000000000 to 0x7fffffffffffffff, a CborException is thrown. |
| Returns | Description |
| BigInt | Decoded big integer. |
| Syntax | getUint64() |
|---|---|
| Description | Get CBOR integer. |
| Returns | Description |
| BigInt | Decoded big integer. |
| Syntax | getBigInt() |
|---|---|
| Description | Get CBOR integer of any size.
Also see Integer Numbers. |
| Returns | Description |
| BigInt | Decoded big integer. |
| Syntax | CBOR.BigInt(value) | |
|---|---|---|
| Description | Constructor. Creates a CBOR big integer object.
Also see Integer Numbers. | |
| Parameter | Type | Description |
| value | BigInt | Big integer to be wrapped. |
| Returns | Description | |
| CBOR.BigInt | Object. | |
| Syntax | getInt64() |
|---|---|
| Description | Get CBOR integer. If the return value is outside the range -0x8000000000000000 to 0x7fffffffffffffff, a CborException is thrown. |
| Returns | Description |
| BigInt | Decoded big integer. |
| Syntax | getUint64() |
|---|---|
| Description | Get CBOR integer. |
| Returns | Description |
| BigInt | Decoded big integer. |
| Syntax | getBigInt() |
|---|---|
| Description | Get CBOR integer of any size.
Also see Integer Numbers. |
| Returns | Description |
| BigInt | Decoded big integer. |
| Syntax | CBOR.Float(value) | |
|---|---|---|
| Description | Constructor. Creates a CBOR float object.
Also see Floating-Point Numbers.
| |
| Parameter | Type | Description |
| value | Number | Floating-point number to be wrapped. |
| Returns | Description | |
| CBOR.Float | Object. | |
| Syntax | getFloat16() |
|---|---|
| Description | Get CBOR floating-point value. If the CBOR object is not a 16-bit IEEE 754 item, a CborException is thrown. |
| Returns | Description |
| Number | Decoded floating-point number. |
| Syntax | getFloat32() |
|---|---|
| Description | Get CBOR floating-point value. If the CBOR object is not a 16-bit or 32-bit IEEE 754 item, a CborException is thrown. |
| Returns | Description |
| Number | Decoded floating-point number. |
| Syntax | getFloat64() |
|---|---|
| Description | Get CBOR floating-point value. |
| Returns | Description |
| Number | Decoded floating-point number. |
| Syntax | CBOR.Float.createExtendedFloat(value) | |
|---|---|---|
| Description | Constructor. Creates a CBOR float object.
Unlike CBOR.Float(),
this method also supports one of
the non-finite values, Number.NaN,
Number.POSITIVE_INFINITY,
and Number.NEGATIVE_INFINITY.
Also see Non-Finite Numbers. | |
| Parameter | Type | Description |
| value | Number | Floating-point number to be wrapped. |
| Returns | Description | |
| CBOR.Wrapper | Instantiated CBOR.Float or CBOR.NonFinite object. | |
| Syntax | getExtendedFloat64() |
|---|---|
| Description | Get CBOR floating-point value.
Also see CBOR.Float.createExtendedFloat().
Note that this method makes it transparent for applications if the returned
value is a "regular" float, or one of
the non-finite values, Number.NaN,
Number.POSITIVE_INFINITY,
or Number.NEGATIVE_INFINITY. |
| Returns | Description |
| Number | Decoded floating-point number. |
| Name | Type | Description |
|---|---|---|
| length | Number | Length in bytes of the underlying CBOR IEEE 754 type. |
| Syntax | CBOR.NonFinite(value) | |
|---|---|---|
| Description | Constructor. Creates a CBOR non-finite float object.
The argument must be a valid
16, 32, or 64-bit
non-finite number in IEEE 754 encoding.
Also see Non-Finite Numbers. | |
| Parameter | Type | Description |
| value | BigInt | Non-finite floating-point number to be wrapped. |
| Returns | Description | |
| CBOR.NonFinite | Object. | |
| Syntax | getNonFinite() |
|---|---|
| Description | Get actual non-finite object (value).
This method returns the value of a non-finite object. The value is
provided in the most compact form based on CBOR serialization rules. |
| Returns | Description |
| BigInt | Decoded non-finite number as a BigInt. |
| Syntax | getNonFinite64() |
|---|---|
| Description | Get expanded non-finite object (value).
This method returns the value of a non-finite object after it has been
expanded to 64 bits. That is, a received 7c01 will be
returned as 7ff0040000000000. |
| Returns | Description |
| BigInt | Decoded non-finite number as a BigInt. |
| Syntax | isNaN() |
|---|---|
| Description | Check if non-finite object is a NaN.
This method returns true for all conformant
NaN variants, else false is returned. |
| Returns | Description |
| Boolean | Result. |
| Syntax | isSimple() |
|---|---|
| Description | Check if non-finite object is simple.
This method returns true if the non-finite object is a
Number.NaN,
Number.POSITIVE_INFINITY, or
Number.NEGATIVE_INFINITY,
else false is returned. |
| Returns | Description |
| Boolean | Result. |
| Syntax | getSign() |
|---|---|
| Description | Get sign bit of non-finite object.
This method returns
true if the sign bit is 1,
else false is returned.
Also see setSign(). |
| Returns | Description |
| Boolean | Result. |
| Syntax | setSign(sign) | |
|---|---|---|
| Description | Set sign bit of non-finite object.
The sign bit is expressed as a
boolean.
true = 1, false = 0.
Also see getSign(). | |
| Parameter | Type | Description |
| sign | Boolean | Sign bit. |
| Returns | Description | |
| this | Current object. | |
| Syntax | CBOR.NonFinite.createPayload(payload) | |
|---|---|---|
| Description | Creates a payload object.
For details turn to Payload option. | |
| Parameter | Type | Description |
| payload | BigInt | Payload data. |
| Returns | Description | |
| CBOR.NonFinite | Instantiated CBOR.NonFinite object. | |
| Syntax | getPayload() |
|---|---|
| Description | Get payload data.
This method is the "consumer" counterpart to
CBOR.NonFinite.createPayload(). |
| Returns | Description |
| BigInt | Payload. |
| Name | Type | Description |
|---|---|---|
| length | Number | Length in bytes of the underlying CBOR IEEE 754 type. |
| Syntax | CBOR.String(textString) | |
|---|---|---|
| Description | Constructor. Creates a CBOR text-string (tstr) object. | |
| Parameter | Type | Description |
| textString | String | String to be wrapped. |
| Returns | Description | |
| CBOR.String | Object. | |
| Syntax | getString() |
|---|---|
| Description | Get CBOR text-string. |
| Returns | Description |
| String | Decoded text-string. |
| Syntax | CBOR.Bytes(byteString) | |
|---|---|---|
| Description | Constructor. Creates a CBOR byte-string (bstr) object. | |
| Parameter | Type | Description |
| byteString | Uint8Array | Binary data to be wrapped. |
| Returns | Description | |
| CBOR.Bytes | Object. | |
| Syntax | getBytes() |
|---|---|
| Description | Get CBOR byte-string. |
| Returns | Description |
| Uint8Array | Decoded byte-string. |
| Syntax | CBOR.Boolean(value) | |
|---|---|---|
| Description | Constructor. Creates a CBOR boolean (bool) object. | |
| Parameter | Type | Description |
| value | Boolean | Boolean to be wrapped. |
| Returns | Description | |
| CBOR.Boolean | Object. | |
| Syntax | getBoolean() |
|---|---|
| Description | Get CBOR boolean. |
| Returns | Description |
| Boolean | Decoded boolean. |
| Syntax | CBOR.Null() |
|---|---|
| Description | Constructor. Creates a CBOR null object.
Also see isNull(). |
| Returns | Description |
| CBOR.Null | Object. |
| Syntax | CBOR.Array() |
|---|---|
| Description | Constructor. Creates an empty CBOR array object. |
| Returns | Description |
| CBOR.Array | Object. |
| Syntax | add(object) | |
|---|---|---|
| Description | Add CBOR object to the array. | |
| Parameter | Type | Description |
| object | CBOR.Wrapper | Object to be appended to the array. |
| Returns | Description | |
| this | Current object. | |
| Syntax | get(index) | |
|---|---|---|
| Description | Get CBOR object at a specific position in the array. | |
| Parameter | Type | Description |
| index | Number | Array index (0..length-1). |
| Returns | Description | |
| CBOR.Wrapper | Retrieved object. | |
| Syntax | remove(index) | |
|---|---|---|
| Description | Remove CBOR object at a specific position in the array. | |
| Parameter | Type | Description |
| index | Number | Array index (0..length-1). |
| Returns | Description | |
| CBOR.Wrapper | Previous object. | |
| Syntax | update(index, object) | |
|---|---|---|
| Description | Update CBOR object at a specific position in the array. | |
| Parameter | Type | Description |
| index | Number | Array index (0..length-1). |
| object | CBOR.Wrapper | Update object. |
| Returns | Description | |
| CBOR.Wrapper | Previous object. | |
| Syntax | insert(index, object) | |
|---|---|---|
| Description | Insert CBOR object before an object at a specific position in the array. | |
| Parameter | Type | Description |
| index | Number | Array index (0..length).
If index
is equal to length, object is appended. |
| object | CBOR.Wrapper | Update object. |
| Returns | Description | |
| this | Current object. | |
| Syntax | toArray() |
|---|---|
| Description | Copy array. |
| Returns | Description |
| [CBOR.Wrapper...] | JavaScript array holding a copy of current CBOR.Wrapper objects. |
| Syntax | encodeAsSequence() |
|---|---|
| Description | Return the objects in the array as a CBOR sequence
using Deterministic Encoding.
See also
CBOR.initDecoder(). |
| Returns | Description |
| Uint8Array | CBOR encoded data. |
| Name | Type | Description |
|---|---|---|
| length | Number | Number of objects in the array. |
| Syntax | CBOR.Map() |
|---|---|
| Description | Constructor. Creates an empty CBOR map object. |
| Returns | Description |
| CBOR.Map | Object. |
| Syntax | set(key, object) | |
|---|---|---|
| Description | Set map entry.
If key is already defined, a CborException is thrown.
Note: key order is of no importance since
Deterministic Encoding
performs the required map sorting automatically.
Also see setSortingMode().
Note: this implementation
presumes that key objects are immutable.
That is, the following code will throw a CborException:
let key = CBOR.Array();By defining key objects inline
(chaining) or by preset variable declarations, key objects
of any complexity can be used. | |
| Parameter | Type | Description |
| key | CBOR.Wrapper | Key (name). |
| object | CBOR.Wrapper | Object (value). |
| Returns | Description | |
| this | Current object. | |
| Syntax | setDynamic(dynamic) | |
|---|---|---|
| Description | Set map entries using a function or =>.
This method permits using
chaining with dynamic or optional map elements.
Consult test file dynamic.js for examples. | |
| Parameter | Type | Description |
| dynamic | function|=> | Function or => operator with one parameter holding this. |
| Returns | Description | |
| this | Current object. | |
| Syntax | merge(map) | |
|---|---|---|
| Description | Merge maps. Performs a set() operation for each member of the map argument. | |
| Parameter | Type | Description |
| map | CBOR.Map | CBOR map wrapper object. |
| Returns | Description | |
| this | Current object. | |
| Syntax | update(key, object, existing) | |
|---|---|---|
| Description | Update map entry. | |
| Parameter | Type | Description |
| key | CBOR.Wrapper | Key (name). |
| object | CBOR.Wrapper | Object (value). |
| existing | Boolean | If existing is true,
key must be defined, else a CborException is thrown.
If existing is false,
a map entry will be created for key if not already defined. |
| Returns | Description | |
| CBOR.Wrapper | Previous object. May be null. | |
| Syntax | get(key) | |
|---|---|---|
| Description | Get map entry. If key is undefined, a CborException is thrown. | |
| Parameter | Type | Description |
| key | CBOR.Wrapper | Key (name). |
| Returns | Description | |
| CBOR.Wrapper | Retrieved object. | |
| Syntax | getConditionally(key, defaultObject) | |
|---|---|---|
| Description | Get map entry conditionally. | |
| Parameter | Type | Description |
| key | CBOR.Wrapper | Key (name). |
| defaultObject | CBOR.Wrapper | Object to return if key is undefined.
Note:
defaultObject may be null. |
| Returns | Description | |
| CBOR.Wrapper | Retrieved or default object. | |
| Syntax | containsKey(key) | |
|---|---|---|
| Description | Check map for key presence. | |
| Parameter | Type | Description |
| key | CBOR.Wrapper | Key (name). |
| Returns | Description | |
| Boolean | true or false. | |
| Syntax | remove(key) | |
|---|---|---|
| Description | Remove map entry. If key is undefined, a CborException is thrown. | |
| Parameter | Type | Description |
| key | CBOR.Wrapper | Key (name). |
| Returns | Description | |
| CBOR.Wrapper | Removed object (value). | |
| Syntax | getKeys() |
|---|---|
| Description | Get map keys. |
| Returns | Description |
| [CBOR.Wrapper...] | JavaScript array holding a copy of current key objects. |
| Syntax | setSortingMode(preSortedKeys) | |
|---|---|---|
| Description | Set the sorting mode of the
CBOR.Map() object during
set() operations.
Typical usage:
let map = CBOR.Map().setSortingMode(true) | |
| Parameter | Type | Description |
| preSortedKeys | Boolean | If true,
keys must be provided in (CBOR wise) ascending order
which can improve performance for maps having a huge number of keys.
Improper key order causes a CborException to be thrown.
By default, map keys are sorted internally. |
| Returns | Description | |
| this | Current object. | |
| Name | Type | Description |
|---|---|---|
| length | Number | Number of map entries. |
| Syntax | CBOR.Tag(tagNumber, object) | |
|---|---|---|
| Description | Constructor. Creates a CBOR tag object.
The CBOR tag constructor accepts any valid parameters but performs
thorough syntax checks on tag 0
(CBOR date/time),
tag 1
(CBOR epoch time),
and tag 1010 [COTX | |
| Parameter | Type | Description |
| tagNumber | BigInt | Tag number. |
| object | CBOR.Wrapper | Object to be wrapped in a tag. |
| Returns | Description | |
| CBOR.Tag | Object. | |
| Syntax | getTagNumber() |
|---|---|
| Description | Get CBOR tag number. |
| Returns | Description |
| BigInt | Decoded tag number. |
| Syntax | get() |
|---|---|
| Description | Get tagged CBOR object. |
| Returns | Description |
| CBOR.Wrapper | Retrieved object. |
| Name | Type | Description |
|---|---|---|
| cotxId | String | COTX [COTX
Only valid for COTX tags.
See also getTagNumber(). |
| Name | Type | Description |
|---|---|---|
| cotxObject | CBOR.Wrapper | COTX [COTX
Only valid for COTX tags.
See also getTagNumber(). |
| Syntax | CBOR.Simple(value) | |
|---|---|---|
| Description | Constructor. Creates a CBOR simple value object.
A primary use case for simple
types in the range of 0-19 and 32-255,
is serving as a limited set of unique and reserved labels (keys)
in CBOR maps. | |
| Parameter | Type | Description |
| value | Number | Simple value. |
| Returns | Description | |
| CBOR.Simple | Object. | |
| Syntax | getSimple() |
|---|---|
| Description | Get simple value. |
| Returns | Description |
| Number | Returned simple value. |
0 and 1 for this purpose.
CBOR.js provides built-in decoders for these tags (CBOR.Tag).
That is, if the current object is a tag 0, it is sufficient
calling getDateTime().
Note that these methods are also available without the tag construct.
| Syntax | getDateTime() |
|---|---|
| Description | Get DateTime object.
This method performs a
getString().
The returned string is subsequently used to initiate a JavaScript Date object.
If not all of the following conditions are met, a CborException is thrown:
Also see CBOR.createDateTime(). |
| Returns | Description |
| Date | Date object |
| Syntax | getEpochTime() |
|---|---|
| Description | Get EpochTime object.
Depending on the type of the current object, this method performs a
getInt53() or a
getFloat64().
The returned number is subsequently used to initiate a JavaScript Date object.
If not all of the following conditions are met, a CborException is thrown:
Also see CBOR.createEpochTime(). |
| Returns | Description |
| Date | Date object |
| Syntax | encode() |
|---|---|
| Description | Encode (aka "serialize") this object.
Note: this method always return CBOR data using
Deterministic Encoding.
See also encodeAsSequence(). |
| Returns | Description |
| Uint8Array | CBOR encoded data. |
| Syntax | clone() |
|---|---|
| Description | Create a new instance of this object, initialized with the original CBOR content. |
| Returns | Description |
| CBOR.Wrapper | Deep copy of this object. |
| Syntax | equals(object) | |
|---|---|---|
| Description | Compare this object with another CBOR object. The result is true if and only if object is not null and is a CBOR.Wrapper, and the associated binary encodings (as provided by encode()) are equivalent. | |
| Parameter | Type | Description |
| object | CBOR.Wrapper | Argument to compare with. |
| Returns | Description | |
| Boolean | true if this object is equal to object,
otherwise false. | |
| Syntax | toDiag(prettyPrint) | |
|---|---|---|
| Description | Render this object in Diagnostic Notation.
In similarity to encode(), this method always produce
data in Deterministic Encoding, irrespective to how
the data was created.
Also see: toString().
If this object (as well as possible
child objects), conforms to the subset of data types supported by JSON,
this method can also be used to generate JSON data. | |
| Parameter | Type | Description |
| prettyPrint | Boolean | If prettyPrint is true,
additional white space is inserted between individual objects
in maps and arrays, to make the result easier to read. |
| Returns | Description | |
| String | Textual version of the wrapped CBOR content. | |
| Syntax | toString() |
|---|---|
| Description | Render this object in Diagnostic Notation.
Equivalent to calling toDiag()
with a true argument. |
| Returns | Description |
| String | Textual version of the wrapped CBOR content. |
| Syntax | isNull() |
|---|---|
| Description | Check for CBOR null.
Note: if checkForUnread()
is used, this object will only be regarded as "read"
if it actually is a CBOR
Also see CBOR.Null().null item. |
| Returns | Description |
| Boolean | Returns true if this object
holds a CBOR null item, otherwise false
is returned. |
| Syntax | checkForUnread() |
|---|---|
| Description | Check if this object including possible child objects has been read
(like calling getInt53()).
If not all of the associated objects have been read, a CborException is thrown.
The purpose of this method is to detect possible misunderstandings between parties
using CBOR based protocols. Together with the strict type checking performed
by the CBOR.js API, a programmatic counterpart to schema-based decoding
can be achieved.
Note that array get(),
map get(), and
tag get()
only locate objects,
and thus do not count as "read".
To cope with elements that are redundant,
scan() can be used:
let array = CBOR.diagDecode(`[3, {}]`); |
| Returns | Description |
| this | Current object. |
| Syntax | scan() |
|---|---|
| Description | Scan this object as well as possible child objects in order to make them appear as "read". This is only meaningful in conjunction with checkForUnread(). |
| Returns | Description |
| this | Current object. |
| Syntax | CBOR.decode(cbor) | |
|---|---|---|
| Description | Decode (aka "deserialize") CBOR object.
This method is equivalent to:
CBOR.initDecoder(cbor, 0).decodeWithOptions() | |
| Parameter | Type | Description |
| cbor | Uint8Array | CBOR binary data holding exactly one CBOR object. |
| Returns | Description | |
| CBOR.Wrapper | Object. | |
| Syntax | CBOR.initDecoder(cbor, options) | |
|---|---|---|
| Description | Create a customized CBOR decoder.
This decoding method presumes that the actual
decoding is performed by one or more (for sequences only) calls to
Decoder.decodeWithOptions().
Note that irrespective of options, the decoder maintains parsed data in the form required for
Deterministic Encoding.
Also see
Decoder.getByteCount(). | |
| Parameter | Type | Description |
| cbor | Uint8Array | The CBOR data (bytes) to be decoded. |
| options | Number | The decoder options.
Multiple options can be combined using the binary OR-operator
("|").
A zero (0) sets the decoder default mode.
The options are defined by the following constants:
CBOR.SEQUENCE_MODE:
If the CBOR.SEQUENCE_MODE
option is defined, the following apply:
See also encodeAsSequence().
CBOR.LENIENT_MAP_DECODING:
By default, the decoder requires
that CBOR maps conform to the
Deterministic Encoding
rules.
The CBOR.LENIENT_MAP_DECODING option makes the decoder
accept CBOR maps with arbitrary key ordering.
Note that duplicate keys still cause a CborException to be thrown.
CBOR.LENIENT_NUMBER_DECODING:
By default, the decoder requires
that CBOR numbers conform to the
Deterministic Encoding rules.
The CBOR.LENIENT_NUMBER_DECODING option makes the decoder
accept different representations of CBOR int/bigint
and float objects, only limited by [RFC8949 |
| Returns | Description | |
| Decoder | Decoder object to be used with Decoder.decodeWithOptions(). | |
| Syntax | Decoder.decodeWithOptions() |
|---|---|
| Description | Decode CBOR data with options.
Unsupported or malformed CBOR data cause a CborException to be thrown. |
| Returns | Description |
| CBOR.Wrapper | Object or null (for EOF sequences only). |
| Syntax | Decoder.getByteCount() |
|---|---|
| Description | Get decoder byte count.
This is equivalent to the position of the next item to be read. |
| Returns | Description |
| Number | The number of bytes read so far. |
| Syntax | CBOR.diagDecode(cborText) | |
|---|---|---|
| Description | Decode a CBOR object provided in Diagnostic Notation.
Also see CBOR.diagDecodeSequence().
This method always returns CBOR data using
Deterministic Encoding.
This method can also be used for decoding
JSON data. | |
| Parameter | Type | Description |
| cborText | String | CBOR in textual format. |
| Returns | Description | |
| CBOR.Wrapper | Object. | |
| Syntax | CBOR.diagDecodeSequence(cborText) | |
|---|---|---|
| Description | Decode CBOR objects provided in Diagnostic Notation.
Unlike CBOR.diagDecode(),
this method also accepts CBOR sequences, using a comma
character (',') as a separator.
Note: empty sequences are permitted. | |
| Parameter | Type | Description |
| cborText | String | CBOR in textual format. |
| Returns | Description | |
| [CBOR.Wrapper...] | JavaScript array holding zero or more objects. | |
| Syntax | CBOR.addArrays(a, b) | |
|---|---|---|
| Description | Add two arrays. | |
| Parameter | Type | Description |
| a | Uint8Array | First array. |
| b | Uint8Array | Second array. |
| Returns | Description | |
| Uint8Array | Concatenation of array a and b. | |
| Syntax | CBOR.compareArrays(a, b) | |
|---|---|---|
| Description | Compare two arrays lexicographically. | |
| Parameter | Type | Description |
| a | Uint8Array | First array. |
| b | Uint8Array | Second array. |
| Returns | Description | |
| Number | If a and b are identical, 0 is retuned. If a > b, a positive number is returned. If a < b, a negative number is returned. | |
| Syntax | CBOR.toHex(byteArray) | |
|---|---|---|
| Description | Encode binary string to hexadecimal. | |
| Parameter | Type | Description |
| byteArray | Uint8Array | Zero or more bytes to be encoded. |
| Returns | Description | |
| String | Hexadecimal encoded data. | |
| Syntax | CBOR.fromHex(hexString) | |
|---|---|---|
| Description | Decode hexadecimal data into binary. | |
| Parameter | Type | Description |
| hexString | String | String with zero or more hexadecimal pairs. Each pair represents one byte. |
| Returns | Description | |
| Uint8Array | The resulting binary (bytes). | |
| Syntax | CBOR.toBase64Url(byteArray) | |
|---|---|---|
| Description | Encode binary string to base64Url. | |
| Parameter | Type | Description |
| byteArray | Uint8Array | Zero or more bytes to be encoded. |
| Returns | Description | |
| String | Base64Url encoded data. | |
| Syntax | CBOR.fromBase64Url(base64) | |
|---|---|---|
| Description | Decode base64Url encoded data into binary. Note that this method is permissive; it accepts base64 encoded data as well as data with or without '=' padding. | |
| Parameter | Type | Description |
| base64 | String | String in base64Url notation. The string may be empty. |
| Returns | Description | |
| Uint8Array | The resulting binary (bytes). | |
| Syntax | CBOR.toBigInt(byteArray) | |
|---|---|---|
| Description | Convert binary string to BigInt. | |
| Parameter | Type | Description |
| byteArray | Uint8Array | Zero or more bytes representing an unsigned number. |
| Returns | Description | |
| BigInt | Resulting BigInt number. | |
| Syntax | CBOR.fromBigInt(value) | |
|---|---|---|
| Description | Convert BigInt into binary string. | |
| Parameter | Type | Description |
| value | BigInt | Value to be converted must be greater or equal to zero. |
| Returns | Description | |
| Uint8Array | The resulting binary (one or more bytes). | |
| Syntax | CBOR.createDateTime(instant, millis, utc) | |
|---|---|---|
| Description | Create DateTime object.
This method creates a date/time string in the ISO format
described in section 5.6 of [RFC3339
If the instant object is outside
the range
"0000-01-01T00:00:00Z" to
"9999-12-31T23:59:59Z",
a CborException is thrown.
If millis is
true the date/time string will feature
milliseconds (.nnn) as well.Sample code:
let iso = CBOR.createDateTime(new Date(), true, false);
Also see getDateTime(). | |
| Parameter | Type | Description |
| instant | Date | Time source object. |
| millis | Boolean | If millis is true,
the milliseconds of the instant object will be
featured in the created time object. Note: if the millisecond
part of the instant object is zero,
millis is considered to be false.
If millis is
false, the millisecond part of the instant
object will not be used, but may after rounding,
add a second to the created time object. |
| utc | Boolean | If utc is true,
the UTC time zone (denoted by a terminating Z) will be used,
else the local time followed by the UTC offset
(±hh:mm) will be used. |
| Returns | Description | |
| CBOR.String | Wrapper holding a DateTime object. | |
| Syntax | CBOR.createEpochTime(instant, millis) | |
|---|---|---|
| Description | Create EpochTime object.
This method creates an Epoch [TIME
If the instant object is outside
the range
"1970-01-01T00:00:00Z" to
"9999-12-31T23:59:59Z",
a CborException is thrown.
If millis is
true a CBOR.Float
object holding seconds with a milliseconds fraction will be created,
else a CBOR.Int
object holding seconds will be created.Sample code:
let epoch = CBOR.createEpochTime(new Date(), false);
Also see getEpochTime(). | |
| Parameter | Type | Description |
| instant | Date | Time source object. |
| millis | Boolean | If millis is true,
the milliseconds of the instant object will be
featured in the created time object. Note: if the millisecond
part of the instant object is zero,
millis is considered to be false.
If millis is
false, the millisecond part of the instant
object will not be used, but may after rounding,
add a second to the created time object. |
| Returns | Description | |
| CBOR.Wrapper | Wrapper holding an EpochTime object. | |
9007199254740991 (253-1), must
for such objects turn to BigInt,
otherwise run-time errors may occur.
Note that in all places in the CBOR.js API where an integer
in the form of a JavaScript Number is expected, Number.isInteger(value)
must be true, else a CborException will be thrown.
Note that selecting between CBOR.BigInt or CBOR.Int only affects the maximum numeric range with respect to JavaScript limits, not the CBOR encoding.
During CBOR decoding, the selection between
CBOR.BigInt
and CBOR.Int is governed by the decoded value
with respect to JavaScript limits.
Since integers within the range -9007199254740991 to 9007199254740991,
will during decoding return a CBOR.Int although they could be
associated by a BigInt variable, the methods
getInt64(), getUint64(),
and getBigInt(),
are supported by the CBOR.Int wrapper as well.
The selection between the CBOR unsigned and negative
integer variants is dealt with by the CBOR.js implementation,
transparent to the API.
For applications depending on non-finite numbers
(NaN and Infinity),
turn to Non-Finite Numbers.
However, through the CBOR.diagDecode() method, CBOR data may also be provided in diagnostic notation, making CBOR useful for local "config" and test data files as well.
Due to the Deterministic Encoding scheme used by CBOR.js, CBOR data can be bidirectionally converted between its native (binary) format and diagnostic notation without getting corrupted. Note though that text-binary-text "roundtrips" do not necessarily return identical text: 0x10 used as diagnostic notation input will return 16 as diagnostic notation output.
The following table shows how CBOR objects should be represented in diagnostic notation:| CDDL | Syntax | Comment | Notes |
|---|---|---|---|
| / comment text / | Multi-line comment. Multi-line comments are treated as whitespace and may thus also be used between CBOR objects. | 6 | |
| # comment text | Single-line comment. Single-line comments are terminated by a newline character ('\n') or EOF. Single-line comments may also terminate lines holding regular CBOR items. | 6 | |
integer |
{sign}{ 0b | 0o | 0x} n |
Arbitrary sized integers without fractional components or exponents.
For input data in diagnostic notation, binary, octal, and hexadecimal notation
is also supported by prepending numbers with 0b, 0o, and 0x respectively.
The latter also permit arbitrary insertions of '_' characters between digits to
enable grouping of data like
0b100_000000001.
|
1, 2 |
float |
{sign}n.n{e ± n} | Floating point values must include a decimal point and an optional exponent. | 1, 2 |
| float'hex data' |
Any valid 16, 32, or 64-bit float value, including NaN with payloads
like float'7ff0800000000001'.
|
||
| NaN |
Not a number in the default encoding (f97e00).
|
||
| {sign}Infinity | Infinity. | 2 | |
bstr |
h'hex data' | Byte data provided in hexadecimal notation. Each byte must be represented by two hexadecimal digits. | 3 |
| b64'base64 data' | Byte data provided in base64 or base64URL notation. Padding with '=' characters is optional. | 3, 6 | |
| 'text' | Byte data provided as UTF-8 encoded text. | 4, 5, 6 | |
| << object... >> | Construct holding zero or more comma-separated CBOR objects that are subsequently wrapped in a byte string. | 6 | |
tstr |
"text" | UTF-8 encoded text string. | 4, 5 |
bool |
true | false | Boolean value. | |
null |
null | Null value. | |
[] |
[ object... ] | Array with zero or more comma-separated CBOR objects. | |
{} |
{ key:value... } | Map with zero or more comma-separated key/value pairs. Key and value pairs are expressed as CBOR objects, separated by a ':' character. | |
#6.nnn |
n( object ) | Tag holding a CBOR object. | 1 |
#7.nnn |
simple(n) | Simple value. | 1 |
| , | Separator character for CBOR sequences. |
While there are different ways you can encode certain CBOR objects, this is non-trivial to support in general purpose platform-based tools, not to mention the limited utility of such measures. Therefore the CBOR.js API implements specific (non-variant) encodings, aka "Deterministic Encoding". Fortunately, the selected encoding scheme is backward compatible with most existing CBOR decoders. However, for decoding (only), compliance with deterministic encoding rules may be relaxed, enabling the processing of CBOR data created by applications using "legacy" encoding schemes as well.
The determinism scheme is described in [CBOR::Core].
CborException,
derived from the JavaScript Error object.
CBOR::Coreimplementations for other software platforms.