logo

CBOR.js - JavaScript API

Abstract

This document describes a JavaScript API for encoding and decoding data in the CBOR format. Through platform independent definitions of deterministic encoding and a textual representation of CBOR data, compatible implementations could, together with the rich set of data types offered by CBOR, provide a viable alternative for applications that currently use JSON.

Table of Contents

1.  Introduction
2.  CBOR Wrapper Objects
2.1.  CBOR.Int
2.1.1.  CBOR.Int Methods
2.1.1.1.  getInt()
2.1.1.2.  getBigInt()
2.2.  CBOR.BigInt
2.2.1.  CBOR.BigInt Methods
2.2.1.1.  getBigInt()
2.3.  CBOR.Float
2.3.1.  CBOR.Float Methods
2.3.1.1.  getFloat()
2.3.2.  CBOR.Float Properties
2.3.2.1.  length
2.4.  CBOR.String
2.4.1.  CBOR.String Methods
2.4.1.1.  getString()
2.5.  CBOR.Bytes
2.5.1.  CBOR.Bytes Methods
2.5.1.1.  getBytes()
2.6.  CBOR.Boolean
2.6.1.  CBOR.Boolean Methods
2.6.1.1.  getBoolean()
2.7.  CBOR.Null
2.8.  CBOR.Array
2.8.1.  CBOR.Array Methods
2.8.1.1.  add()
2.8.1.2.  get()
2.8.1.3.  toArray()
2.8.1.4.  getArray()
2.8.2.  CBOR.Array Properties
2.8.2.1.  length
2.9.  CBOR.Map
2.9.1.  CBOR.Map Methods
2.9.1.1.  set()
2.9.1.2.  get()
2.9.1.3.  getConditional()
2.9.1.4.  containsKey()
2.9.1.5.  remove()
2.9.1.6.  getKeys()
2.9.1.7.  getMap()
2.9.1.8.  setSortingMode()
2.9.2.  CBOR.Map Properties
2.9.2.1.  length
2.10.  CBOR.Tag
2.10.1.  CBOR.Tag Methods
2.10.1.1.  getTagNumber()
2.10.1.2.  getTaggedObject()
2.10.1.3.  getTag()
3.  Common Wrapper Methods
3.1.  encode()
3.2.  clone()
3.3.  equals()
3.4.  toDiag()
3.5.  toString()
3.6.  isNull()
3.7.  checkForUnread()
3.8.  scan()
4.  Decoding CBOR
4.1.  CBOR.decode()
4.2.  CBOR.initExtended()
4.3.  CBOR.decodeExtended()
4.4.  CBOR.diagDecode()
4.5.  CBOR.diagDecodeSequence()
5.  Utility Methods
5.1.  CBOR.addArrays()
5.2.  CBOR.compareArrays()
5.3.  CBOR.toHex()
5.4.  CBOR.fromHex()
5.5.  CBOR.toBase64Url()
5.6.  CBOR.fromBase64Url()
6.  JavaScript Number Considerations
6.1.  Integer Numbers
6.2.  Floating Point Numbers
7.  Diagnostic Notation
8.  Deterministic Encoding
9.  Version

1.  Introduction

This document describes a JavaScript API for encoding and decoding data in the CBOR [RFC8949link] format. The API loosely mimics the "JSON" object by exposing a single global object, unsurprisingly named "CBOR". To minimize the need for application developers having detailed knowledge of CBOR, the CBOR.js API provides a set of high level wrapper objects. The wrapper objects are used for encoding CBOR data items, as well as being the result of CBOR decoding. The CBOR.js API provides some specific features including: Note: in this specification CBOR data items are subsequently referred to as CBOR objects.
Although this document describes a JavaScript API, it could also serve as a guide for CBOR implementations for other software platforms, with the goal of creating the foundation for cross platform interoperability. Due to this consideration, the level of CBOR support provided by the API is of the type "general purpose", avoiding constructs that are specific to JavaScript, like undefined and typed arrays beyond Uint8Array.

2.  CBOR Wrapper Objects

This section describes the wrapper objects and their specific methods. Note that using the "new" operator with the wrapper objects is flagged as an error. The following table lists the wrapper objects and their relation to the CBOR objects supported by this specification:
CBOR ObjectWrapper ObjectJavaScript
integer CBOR.Int Number [1]
bignum CBOR.BigInt BigInt [1]
floating point CBOR.Float Number [2]
byte string CBOR.Bytes Uint8Array
text string CBOR.String String
true/false CBOR.Boolean Boolean
null CBOR.Null
array CBOR.Array
map CBOR.Map
tag CBOR.Tag
  1. See also Integer Numbers.
  2. See also Floating Point Numbers.

Note that this implementation does not support CBOR "simple" values beyond true, false, null, and the three floating point variants.

See also Common Wrapper Methods.
2.1.  CBOR.Int
SyntaxCBOR.Int(value)
DescriptionConstructor. Creates a CBOR integer wrapper object. See also Integer Numbers.
ParameterTypeDescription
valueNumberInteger to be wrapped.
ReturnsDescription
CBOR.IntWrapper object.
2.1.1.  CBOR.Int Methods
2.1.1.1.  getInt()
SyntaxgetInt()
DescriptionReads CBOR integer in the range of -253-1 to 253-1. See also getBigInt().
ReturnsDescription
NumberDecoded integer.
2.1.1.2.  getBigInt()
SyntaxgetBigInt()
DescriptionReads CBOR integer of any size. See also Integer Numbers.
ReturnsDescription
BigIntDecoded big integer.
2.2.  CBOR.BigInt
SyntaxCBOR.BigInt(value)
DescriptionConstructor. Creates a CBOR big integer wrapper object. See also Integer Numbers.
ParameterTypeDescription
valueBigIntBig integer to be wrapped.
ReturnsDescription
CBOR.BigIntWrapper object.
2.2.1.  CBOR.BigInt Methods
2.2.1.1.  getBigInt()
SyntaxgetBigInt()
DescriptionReads CBOR integer of any size. See also Integer Numbers.
ReturnsDescription
BigIntDecoded big integer.
2.3.  CBOR.Float
SyntaxCBOR.Float(value)
DescriptionConstructor. Creates a CBOR floating point wrapper object. See also Floating Point Numbers.
ParameterTypeDescription
valueNumberFloating point number to be wrapped.
ReturnsDescription
CBOR.FloatWrapper object.
2.3.1.  CBOR.Float Methods
2.3.1.1.  getFloat()
SyntaxgetFloat()
DescriptionReads CBOR floating point object.
ReturnsDescription
NumberDecoded floating point number.
2.3.2.  CBOR.Float Properties
2.3.2.1.  length
NameTypeDescription
lengthNumberLength in bytes of the underlying CBOR IEEE 754 type.
2.4.  CBOR.String
SyntaxCBOR.String(textString)
DescriptionConstructor. Creates a CBOR text string wrapper object.
ParameterTypeDescription
textStringStringString to be wrapped.
ReturnsDescription
CBOR.StringWrapper object.
2.4.1.  CBOR.String Methods
2.4.1.1.  getString()
SyntaxgetString()
DescriptionReads CBOR text string.
ReturnsDescription
StringDecoded text string.
2.5.  CBOR.Bytes
SyntaxCBOR.Bytes(byteString)
DescriptionConstructor. Creates a CBOR byte string wrapper object.
ParameterTypeDescription
byteStringUint8ArrayBinary data to be wrapped.
ReturnsDescription
CBOR.BytesWrapper object.
2.5.1.  CBOR.Bytes Methods
2.5.1.1.  getBytes()
SyntaxgetBytes()
DescriptionReads CBOR byte string.
ReturnsDescription
Uint8ArrayDecoded byte string.
2.6.  CBOR.Boolean
SyntaxCBOR.Boolean(value)
DescriptionConstructor. Creates a CBOR boolean wrapper object.
ParameterTypeDescription
valueBooleanBoolean to be wrapped.
ReturnsDescription
CBOR.BooleanWrapper object.
2.6.1.  CBOR.Boolean Methods
2.6.1.1.  getBoolean()
SyntaxgetBoolean()
DescriptionReads CBOR boolean.
ReturnsDescription
BooleanDecoded boolean.
2.7.  CBOR.Null
SyntaxCBOR.Null()
DescriptionConstructor. Creates a CBOR null wrapper object. See also isNull().
ReturnsDescription
CBOR.NullWrapper object.
2.8.  CBOR.Array
SyntaxCBOR.Array()
DescriptionConstructor. Creates a CBOR array wrapper object.
ReturnsDescription
CBOR.ArrayWrapper object.
2.8.1.  CBOR.Array Methods
2.8.1.1.  add()
Syntaxadd(object)
DescriptionAdds CBOR wrapper object to the array.
ParameterTypeDescription
objectCBOR.WrapperObject to be appended to the current array.
ReturnsDescription
thisCurrent object.
2.8.1.2.  get()
Syntaxget(index)
DescriptionFetches CBOR wrapper object. If index is out of range, an exception is thrown.
ParameterTypeDescription
indexNumberIndex (0..length-1) of object.
ReturnsDescription
CBOR.WrapperRetrieved object.
2.8.1.3.  toArray()
SyntaxtoArray()
DescriptionCopies array.
ReturnsDescription
[...]JavaScript array holding a copy of current CBOR.Wrapper objects.
2.8.1.4.  getArray()
SyntaxgetArray()
DescriptionGet array object.
Note: this method is redundant due to the dynamic nature of the JavaScript typing system. However, if you intend using checkForUnread(), this method must be called before accessing array elements.
ReturnsDescription
CBOR.ArrayCurrent object.
2.8.2.  CBOR.Array Properties
2.8.2.1.  length
NameTypeDescription
lengthNumberNumber of objects in the array.
2.9.  CBOR.Map
SyntaxCBOR.Map()
DescriptionConstructor. Creates a CBOR map wrapper object.
ReturnsDescription
CBOR.MapWrapper object.
2.9.1.  CBOR.Map Methods
2.9.1.1.  set()
Syntaxset(key, value)
DescriptionSets map entry. If key entry is already defined, an exception is thrown.
Note: key order is of no importance since Deterministic Encoding performs the required map sorting automatically. See also setSortingMode() and CBOR.initExtended().
ParameterTypeDescription
keyCBOR.WrapperKey (name) wrapper object.
valueCBOR.WrapperValue wrapper object.
ReturnsDescription
thisCurrent object.
2.9.1.2.  get()
Syntaxget(key)
DescriptionGets map entry. If key entry is undefined, an exception is thrown.
ParameterTypeDescription
keyCBOR.WrapperKey (name) wrapper object.
ReturnsDescription
CBOR.WrapperRetrieved object.
2.9.1.3.  getConditional()
SyntaxgetConditional(key, defaultValue)
DescriptionGets map entry conditionally.
ParameterTypeDescription
keyCBOR.WrapperKey (name) wrapper object.
defaultValueCBOR.WrapperValue to return if key entry is undefined. defaultValue may be null.
ReturnsDescription
CBOR.WrapperRetrieved or default object.
2.9.1.4.  containsKey()
SyntaxcontainsKey(key)
DescriptionChecks map for key presence.
ParameterTypeDescription
keyCBOR.WrapperKey (name) wrapper object.
ReturnsDescription
Booleantrue or false.
2.9.1.5.  remove()
Syntaxremove(key)
DescriptionRemoves map entry. If key entry is undefined, an exception is thrown.
ParameterTypeDescription
keyCBOR.WrapperKey (name) wrapper object.
ReturnsDescription
CBOR.WrapperRemoved object (value component).
2.9.1.6.  getKeys()
SyntaxgetKeys()
DescriptionGet map keys.
ReturnsDescription
[...]JavaScript array holding a copy of current CBOR.Wrapper map keys.
2.9.1.7.  getMap()
SyntaxgetMap()
DescriptionGet map object.
Note: this method is redundant due to the dynamic nature of the JavaScript typing system. However, if you intend using checkForUnread(), this method must be called before accessing map keys.
ReturnsDescription
CBOR.MapCurrent object.
2.9.1.8.  setSortingMode()
SyntaxsetSortingMode(preSortedKeys)
DescriptionSets the current sorting mode of the CBOR.Map() object during set() operations.
Typical usage:
let map = CBOR.Map().setSortingMode(true)
This method may be called multiple times which could be useful if you have a moderate set of unsorted meta data keys combined with a sorted large table-like set of keys. Note that this method has no effect on decoding operations.
ParameterTypeDescription
preSortedKeysBooleanIf 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 will cause an exception to be thrown. By default map keys are sorted internally.
ReturnsDescription
thisCurrent object.
2.9.2.  CBOR.Map Properties
2.9.2.1.  length
NameTypeDescription
lengthNumberNumber of map entries.
2.10.  CBOR.Tag
SyntaxCBOR.Tag(tagNumber, object)
DescriptionConstructor. Creates a CBOR tag wrapper object.
ParameterTypeDescription
tagNumberBigIntTag number.
objectCBOR.WrapperObject to be wrapped in a tag.
ReturnsDescription
CBOR.TagWrapper object.
2.10.1.  CBOR.Tag Methods
2.10.1.1.  getTagNumber()
SyntaxgetTagNumber()
DescriptionReads CBOR tag number.
ReturnsDescription
BigIntDecoded tag number.
2.10.1.2.  getTaggedObject()
SyntaxgetTaggedObject()
DescriptionReads tagged CBOR object.
ReturnsDescription
CBOR.WrapperRetrieved object.
2.10.1.3.  getTag()
SyntaxgetTag()
DescriptionGet tag object.
Note: this method is redundant due to the dynamic nature of the JavaScript typing system. However, if you intend using checkForUnread(), this method must be called before accessing the tag object.
ReturnsDescription
CBOR.TagCurrent object.

3.  Common Wrapper Methods

The CBOR wrapper objects support a set of common methods, described in this sub section.
3.1.  encode()
Syntaxencode()
Description Encodes this object.
Note: this method always return CBOR data using Deterministic Encoding.
ReturnsDescription
Uint8ArrayCBOR encoded data.
3.2.  clone()
Syntaxclone()
DescriptionCreates a new instance of this object, initialized with the original CBOR content.
ReturnsDescription
CBOR.WrapperCopy of object.
3.3.  equals()
Syntaxequals(object)
DescriptionCompares this object with another object with respect to CBOR data.
ParameterTypeDescription
objectCBOR.WrapperThe object to compare with.
ReturnsDescription
Booleantrue if this object is equal to object, otherwise false.
3.4.  toDiag()
SyntaxtoDiag(prettyPrint)
DescriptionRenders this object in Diagnostic Notation. In similarity to encode(), this method always produce data in Deterministic Encoding, irrespective to how the data was created. See also: toString().
ParameterTypeDescription
prettyPrintBooleanIf prettyPrint is true, additional white space is inserted between elements to make the result easier to read.
ReturnsDescription
StringTextual version of the wrapped CBOR content.
3.5.  toString()
SyntaxtoString()
DescriptionRenders this object in Diagnostic Notation. Equivalent to calling toDiag() with a true argument.
ReturnsDescription
StringTextual version of the wrapped CBOR content.
3.6.  isNull()
SyntaxisNull()
DescriptionChecks for CBOR null.
Note: if checkForUnread() is used, the current object will only be regarded as "read" if it actually is a CBOR null item.
See also CBOR.Null().
ReturnsDescription
BooleanReturns true if the current object holds a CBOR null item, otherwise false is returned.
3.7.  checkForUnread()
SyntaxcheckForUnread()
DescriptionChecks if the current object including possible child objects has been read (like calling getInt()). If any of the associated objects have not been read, an exception will be 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 processing can be achieved.
Note that array get(), map get(), and tag getTaggedObject() only locate objects, and thus do not count as "read". Also see getArray(), getMap(), getTag(), and scan().
ReturnsDescription
thisCurrent object.
3.8.  scan()
Syntaxscan()
DescriptionScans current object as well as possible child objects in order to make them appear as "read". This is only meaningful in conjunction with checkForUnread().
ReturnsDescription
thisCurrent object.

4.  Decoding CBOR

CBOR decoding comes in two flavors, binary and Diagnostic Notation. Both decoders store the result in the same set of CBOR.Wrapper objects, used for encoding CBOR data. This section lists the decoder methods.
4.1.  CBOR.decode()
SyntaxCBOR.decode(cbor)
DescriptionDecodes a CBOR object. This method assumes that the CBOR data adheres to Deterministic Encoding, otherwise exceptions will be thrown. Any additional data after the CBOR object will also cause exceptions to be thrown.
ParameterTypeDescription
cborUint8ArrayThe data (bytes) to be decoded.
ReturnsDescription
CBOR.WrapperCBOR wrapper object.
4.2.  CBOR.initExtended()
SyntaxCBOR.initExtended(cbor, sequenceFlag, nonDeterministic)
DescriptionInitiates an extended CBOR decoder. This decoding method presumes that the actual decoding is performed by one or more (for sequences only) calls to CBOR.decodeExtended().
ParameterTypeDescription
cborUint8ArrayThe data (bytes) to be decoded.
sequenceFlagBooleanIf true, CBOR.decodeExtended() will return immediately after decoding a CBOR object, otherwise any additional data after the CBOR object will cause exceptions to be thrown.
nonDeterministicBooleanIf true the decoder will accept CBOR code which violates the Deterministic Encoding rules. This option may be needed for dealing with "legacy" CBOR implementations. The flag disables the strict map sorting requirement and the preferred serialization of numbers (=shortest).
ReturnsDescription
DecoderInternal decoder object for usage with CBOR.decodeExtended().
4.3.  CBOR.decodeExtended()
SyntaxCBOR.decodeExtended(decoder)
DescriptionDecodes a CBOR object. If the sequenceFlag in the call to CBOR.initExtended() is true, each call to CBOR.decodeExtended() causes the internal decoder to move to the next (possible but unprocessed) CBOR object. When there is no more data to decode, null is returned.
ParameterTypeDescription
decoderDecoderInternal decoder object.
ReturnsDescription
CBOR.WrapperCBOR wrapper object or null.
4.4.  CBOR.diagDecode()
SyntaxCBOR.diagDecode(cborText)
DescriptionDecodes a CBOR object provided in Diagnostic Notation. See also CBOR.diagDecodeSequence().
ParameterTypeDescription
cborTextStringCBOR in textual format.
ReturnsDescription
CBOR.WrapperCBOR wrapper object.
4.5.  CBOR.diagDecodeSequence()
SyntaxCBOR.diagDecodeSequence(cborText)
DescriptionDecodes a CBOR object provided in Diagnostic Notation. Unlike CBOR.diagDecode(), this method also accepts CBOR sequences, using a comma character (',') as a separator.
ParameterTypeDescription
cborTextStringCBOR in textual format.
ReturnsDescription
[...]Array holding one or more CBOR wrapper objects.

5.  Utility Methods

The following utility methods are unrelated to CBOR but have been included here because they are a part of the current CBOR.js implementation.
5.1.  CBOR.addArrays()
SyntaxCBOR.addArrays(a, b)
DescriptionAdds two arrays.
ParameterTypeDescription
aUint8ArrayFirst array.
bUint8ArraySecond array.
ReturnsDescription
Uint8ArrayConcatenation of array a and b.
5.2.  CBOR.compareArrays()
SyntaxCBOR.compareArrays(a, b)
DescriptionCompares two arrays lexicographically.
ParameterTypeDescription
aUint8ArrayFirst array.
bUint8ArraySecond array.
ReturnsDescription
NumberIf a and b are identical, 0 is retuned. If a > b, a positive number is returned. If a < b, a negative number is returned.
5.3.  CBOR.toHex()
SyntaxCBOR.toHex(byteArray)
DescriptionEncodes binary data to hexadecimal.
ParameterTypeDescription
byteArrayUint8ArrayZero or more bytes to be encoded.
ReturnsDescription
StringHexadecimal encoded data.
5.4.  CBOR.fromHex()
SyntaxCBOR.fromHex(hexString)
DescriptionDecodes hexadecimal data into binary.
ParameterTypeDescription
hexStringStringString with zero or more hexadecimal pairs. Each pair represents one byte.
ReturnsDescription
Uint8ArrayThe resulting binary (bytes).
5.5.  CBOR.toBase64Url()
SyntaxCBOR.toBase64Url(byteArray)
DescriptionEncodes binary data to base64Url.
ParameterTypeDescription
byteArrayUint8ArrayZero or more bytes to be encoded.
ReturnsDescription
StringBase64Url encoded data.
5.6.  CBOR.fromBase64Url()
SyntaxCBOR.fromBase64Url(base64)
DescriptionDecodes base64Url encoded data into binary. Note that this method is permissive; it accepts base64 encoded data as well as data with or without '=' padding.
ParameterTypeDescription
base64StringString in base64Url notation. The string may be empty.
ReturnsDescription
Uint8ArrayThe resulting binary (bytes).

6.  JavaScript Number Considerations

Since JavaScript natively only supports Number (backed by 64-bit IEEE 754 floating point data), and BigInt (offering unlimited integers), it is important to understand how these number types are mapped to CBOR which offers two variants of integers and three variants of floating point types.
6.1.  Integer Numbers
Due to the Number.MAX_SAFE_INTEGER limitation, CBOR protocols using integer elements where the magnitude may exceed 253-1, must for such elements turn to BigInt, otherwise run-time errors may occur when accessing integer data using getInt() or during creation of integers using CBOR.Int(). That is, for creation of such integers, CBOR.BigInt() is the proper solution, while access requires getBigInt(), which for convenience reasons is supported by both of the integer wrapper types.

Note that selecting 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.

The selection between the CBOR unsigned and negative integer variants is dealt with by the CBOR.js implementation; transparent to the API.

6.2.  Floating Point Numbers
Although CBOR encoding depends on 16, 32, and 64-bit IEEE 754 variants, the CBOR.js implementation makes this transparent; the API only exposes the Number type. See also CBOR.Float().

7.  Diagnostic Notation

Creating CBOR data in diagnostic notation (as described in section 8 of RFC 8949), is provided by the toString() method.

However, through the CBOR.diagDecode() method, CBOR data may also be provided in diagnostic (textual) 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:
CBOR ObjectSyntaxNotesDescription
/ comment text / 7 Multi-line comment. Multi-line comments are treated as whitespace and may thus also be used between CBOR objects.
# comment text 7 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.
integer {sign}{0b|0o|0x}n 1, 2 Arbitrary sized integers without fractional components or exponents. See also CBOR integer encoding.
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.
bignum
floating point {sign}n.n{n} 1, 2 Floating point values must include a decimal point and an optional exponent. See also CBOR floating point encoding.
NaN Not a number. See also CBOR floating point encoding.
{sign}Infinity 2 Infinity. See also CBOR floating point encoding.
byte string h'hex data' 3, 6 Byte data provided in hexadecimal notation. Each byte must be represented by two hexadecimal digits.
b64'base64 data' 3, 6, 7 Byte data provided in base64 or base64URL notation. Padding with '=' characters is optional.
'text' 4, 5, 7 Byte data provided as UTF-8 encoded text.
<< object >> 7 Construct holding a CBOR object which is subsequently embedded in a byte string.
text string "text" 4, 5 UTF-8 encoded text string.
true true Boolean value.
falsefalse
null null Null value.
array [ objects ] Array with zero or more comma separated CBOR objects.
map { key:value } Map with zero or more comma separated key/value pairs. Keys and values are expressed as CBOR objects.
tag n( object ) 1 Tag holding a CBOR object.
, 7 Separator character for CBOR sequences.
  1. The letter n in the Syntax column denotes one or more digits.
  2. The optional {sign} must be a single hyphen ('-') character.
  3. Whitespace characters (' ', '\t', '\r', '\n') inside of string quotes are ignored.
  4. Whitespace characters ('\t', '\r', '\n') inside of string quotes become a part of the text. To avoid getting newline characters ('\n') included in multi-line text strings, a line continuation marker consisting of a backslash ('\') immediately preceding the newline may be used.
  5. Text strings may also include JavaScript compatible escape sequences ('\'', '\"', '\\', '\b', '\f', '\n', '\r', '\t', '\uhhhh').
  6. Zero-length strings ('') return byte strings of length zero.
  7. Only applicable for input. That is, toString() does not produce this item.

8.  Deterministic Encoding

This section is primarily targeting implementers of CBOR tools.

While there are different ways you can encode certain CBOR objects, this is non-trivial to support in a generic platform API without adding complexity and that for little provable gain. Therefore the CBOR.js API defines specific (non-variant) encodings, aka "Deterministic Encoding". The chosen encoding model is compatible with most existing CBOR decoders.

However, to not limit CBOR.js to new applications only, deterministic encoding checking may optionally be turned off for accepting input from "legacy" CBOR encoders.

The encoding scheme adheres to section 4.2 of RFC 8949, but adds a few constraints (denoted by RFC+), where the RFC offers choices. The encoding rules are as follows:

9.  Version

Version: 1.0.3, 2023-12-05