proposals
proposals copied to clipboard
Standardize JS API to encode/decode CBOR
Introduction
Concise Binary Object Representation, aka. CBOR (RFC 8949) is a data format widely deployed on the web. However there is no standardization of JS API to encode/decode it.
Use Cases
A consensus around a standard CBOR JS API specification might be used by web browsers or any other JS implementation to expose native CBOR support[^1].
Goals
The goal of this proposal is to define a standard JS API to encode/decode CBOR data format. It includes:
- the prototype of functions for encoding/decoding (based on know-how from existing JS implementations),
- the mapping and convertion between JS notation and CBOR representation considering the non-normative advices from RFC 8949 Section 6: "Converting Data between CBOR and JSON".
Non-goals
This proposal does not consider the RFC 8742: "Concise Binary Object Representation (CBOR) Sequences".
Proposed Solution
Based on existing implementations, the proposed solution defines the simple JS API of the form:
encoded = CBOR.encode(data)
➔data = CBOR.decode(encoded)
Encoding CBOR
The CBOR.encode()
method converts a JavaScript object or value to its CBOR representation in an ArrayBuffer object.
This method follow the recommendations from RFC 8949 Section 6.2: "Converting from JSON to CBOR " to encode JS types into CBOR representation.
Syntax
CBOR.encode(value)
Decoding CBOR
The CBOR.decode()
method converts the CBOR representation within an ArrayBuffer object to a JavaScript object or value.
This method follow the recommendations from RFC 8949 Section 6.1: "Converting from CBOR to JSON" to decode JS types from CBOR representation.
Syntax
CBOR.decode(value)
Let’s Discuss
A few remaining questions:
- Is it the right place for this kind of proposal ?[^2]
- Should we map (some) CBOR tags to their equivalent in JS ? For instance date/time, URI,...
Previous discussion on discourse: https://discourse.wicg.io/t/proposal-native-cbor-or-messagepack-support/2011
[^1]: CBOR is already implemented in some browsers (Chrome, Firefox, and Edge) thanks to the new W3C Specification Web Authentication (WebAuthn). [^2]: Request for Mozilla Position has been asked here.