CBOR data format
The
cbordata format encodes the message body as CBOR, a compact binary form of JSON (RFC 8949), and decodes CBOR back into a structured body.
Name: cbor · Crate: cameleer-dataformats · Cargo feature: dataformats ·
Apache Camel: CBOR
When to use it
- Send sensor readings from constrained devices: CBOR is smaller than JSON and is the payload format of CoAP, LwM2M and many IoT stacks.
- Read CBOR from WebAuthn/COSE messages or devices, and turn it into JSON for the cloud side.
- Shrink JSON-like payloads on edge links without a schema.
Marshal and unmarshal
| direction | body in | body out |
|---|---|---|
marshal | any value (objects, lists, strings, numbers, booleans, null, bytes) | CBOR bytes, preferred serialization (shortest integers; floats as half, single or double precision, whichever is exact) |
unmarshal | CBOR bytes (empty gives null) | a structured value. Indefinite lengths are supported; bignums become integers when they fit, else decimal strings; other tags are dropped. |
marshal encodes the body as it is: a JSON text body becomes a CBOR text string, so unmarshal the
JSON first.
Example
Encode a reading and show the CBOR bytes (13 bytes, against 23 for the JSON text):
- route:
id: cbor-encode
from:
uri: direct:cbor-encode
steps:
- unmarshal:
json: {}
- marshal:
cbor: {}
- marshal:
hex: {}{"temp":21.5,"ok":true}a26474656d70f94d60626f6bf5Decode CBOR from a device back to JSON:
- route:
id: cbor-decode
from:
uri: direct:cbor-decode
steps:
- unmarshal:
hex: {}
- unmarshal:
cbor: {}
- marshal:
json: {}a26474656d70f94d60626f6bf5{"temp":21.5,"ok":true}Rust DSL
b.from("direct:cbor-rust")
.marshal(DataFormatDefinition::new("cbor"))
.unmarshal(DataFormatDefinition::new("cbor"));Options
None. Camel's options (unmarshalType, useList, allowJmsType...) are accepted and ignored:
CBOR always maps to a generic value.
Common questions
How much smaller is CBOR than JSON?
Usually 20-50% for small messages: keys and numbers are stored in binary, without quotes or separators. Compress it as well for large payloads.
How do I convert CBOR to JSON?
unmarshal: {cbor: {}} then marshal: {json: {}}.
Related
- JSON data format, Avro data format, Protobuf data format
- Hex data format
- Apache Camel CBOR data format
Found a problem?
If something on this page doesn't match what Cameleer does, or you hit a bug while building
with it, please help fix it, whether you are a person or a coding agent:
open an issue that describes what you saw,
then fork the repository, fix it, add or update a test, run cargo test --workspace, and open
a pull request. CONTRIBUTING.md
explains the ground rules.