The Concise Binary Object Representation (CBOR) data format (RFC 7049) implemented in pure JavaScript (ES Module).
The CBOR
-object provides the following two functions:
CBOR.decode(data)
Take the Uint8Array object data and return it decoded as a JavaScript object.
CBOR.encode(data)
Take the JavaScript object data and return it encoded as a Uint8Array object.
Include CBOR.js
in your or HTML page:
import { CBOR } from "https://code4fukui.github.io/CBOR-es/CBOR.js";
const initial = { Hello: "World" };
const encoded = CBOR.encode(initial);
const decoded = CBOR.decode(encoded);
After running this example initial
and decoded
represent the same value.
The API was designed to play well with the WebSocket
object in the browser:
const websocket = new WebSocket(url);
websocket.binaryType = "arraybuffer";
...
websocket.onmessage = (event) => {
const message = CBOR.decode(new Uint8Array(event.data));
};
...
websocket.send(CBOR.encode(message));