cbor
stableCBOR (Concise Binary Object Representation, RFC 8949) plugin for encoding Zolo values to compact binary or hex strings and decoding them back, with payload validation.
use plugin cbor Plugin snippets can be edited in the Playground, but native capabilities are executed locally with the Zolo CLI.
Functions (5)
From import to result
- 01Import only what you use
- 02Copy the smallest example
- 03Run it with the Zolo CLI
Encode a value to CBOR bytes
Encodes a Zolo value (map, array, string, number, bool, or nil) into CBOR binary bytes. Tables with consecutive integer keys starting at 1 are encoded as CBOR arrays; string-keyed tables become CBOR maps. Errors if the value is missing or encoding fails.
use plugin cbor::{encode, decode}
let payload = #{ name: "Ada", age: 36, tags: ["math", "pioneer"] }
let bytes = encode(payload)
let back = decode(bytes)
print("round-tripped name: {back["name"]}")Decode CBOR bytes back into a value
Decodes CBOR bytes back into the corresponding Zolo value. CBOR arrays become 1-indexed tables and CBOR maps become string-keyed tables. Errors if the argument is not bytes or the payload is malformed.
use plugin cbor::{encode, decode}
let bytes = encode(#{ id: 7, active: true })
let value = decode(bytes)
print("id={value["id"]} active={value["active"]}")Encode a value to a CBOR hex string
Encodes a value to CBOR and returns the result as a lowercase hex string instead of raw bytes. Handy for logging, embedding payloads in text protocols, or comparing encodings.
use plugin cbor::{encode_to_hex}
let hex = encode_to_hex(#{ ok: true })
print("cbor hex: {hex}")Decode a CBOR hex string back into a value
Decodes a hex string produced by encode_to_hex back into a Zolo value. Errors if the string has odd length, contains non-hex characters, or the decoded bytes are not valid CBOR.
use plugin cbor::{encode_to_hex, decode_from_hex}
let hex = encode_to_hex(#{ greeting: "hello" })
let value = decode_from_hex(hex)
print(value["greeting"])Check whether bytes are a valid CBOR payload
Returns true if data is bytes containing a well-formed CBOR payload, and false otherwise (including when the argument is not bytes). Never errors — use it to guard before calling decode.
use plugin cbor::{encode, is_valid, decode}
let bytes = encode(#{ n: 42 })
if is_valid(bytes) {
let value = decode(bytes)
print("n = {value["n"]}")
}