decodeCbor
import { decodeCbor } from "jsr:@ende/cbor";function
function decodeCbor(input: Uint8Array): CborTypeDecodes a CBOR-encoded Uint8Array into the JavaScript equivalent values represented as a CborType. RFC 8949 - Concise Binary Object Representation (CBOR)
Limitations:
- While CBOR does support map keys of any type, this implementation only supports map keys being of type string, and will throw if detected decoding otherwise.
- This decoder will throw if duplicate map keys are detected. This behaviour differentiates from CborSequenceDecoderStream.
Notice: This decoder handles the tag numbers 0, and 1 automatically, all
others returned are wrapped in a CborTag
input: Uint8ArrayThe value to decode of type CBOR-encoded Uint8Array.
Returns
A CborType representing the decoded data.
Example: Usage
import { assert, assertEquals } from "@ende/assert";
import { type CborType, decodeCbor, encodeCbor } from "@ende/cbor";
const rawMessage = [
"Hello World",
35,
0.5,
false,
-1,
null,
Uint8Array.from([0, 1, 2, 3]),
new Date(),
new Map<CborType, CborType>([[1, 2], ['3', 4], [[5], { a: 6 }]]),
];
const encodedMessage = encodeCbor(rawMessage);
const decodedMessage = decodeCbor(encodedMessage);
assert(decodedMessage instanceof Array);
assertEquals(decodedMessage, rawMessage);