CBOR encoding combinators.
Build encoders for your domain types, then produce bytes. Length and sort decisions are made at construction time — no separate strategy parameter is needed.
import Cbor exposing (Length(..))
import Cbor.Encode as CE
type alias Person =
{ name : String, age : Int }
encodePerson : Person -> CE.Encoder
encodePerson p =
CE.keyedRecord CE.Unsorted Definite CE.int
[ ( 0, Just (CE.string p.name) )
, ( 1, Just (CE.int p.age) )
]
CE.encode (encodePerson alice)Encode an integer using the shortest form.
Only reliable for values in [-(2^53 - 1), 2^53 - 1] (JavaScript's safe
integer range). Outside that range, Elm's Int loses precision and the
encoded bytes will be wrong. For arbitrarily large integers, use bigInt
with a raw bytes representation.
Encode an integer that does not fit in a 32-bit CBOR argument.
For values that fit in Encode.int, prefer int instead. This function
is for magnitudes that exceed 32 bits.
You should even prefer Encode.int on numbers up to 2^53-1.
Positive, tag 3 for Negative) wrapping the byte string.The bytes are the unsigned magnitude in big-endian byte order.
For Negative, the actual mathematical value is −1 − magnitude,
matching the CBOR spec for both major type 1 and tag 3.
Leading zero bytes are stripped before encoding.
Encode a Maybe value: Nothing becomes CBOR null, Just x is encoded
with the given encoder.
Encode an integer with a specific wire width.
The value is encoded in the given width regardless of whether a shorter encoding exists. Useful for lossless round-tripping.
Encode a float with a specific IEEE 754 width.
Encode a chunked text string using indefinite-length encoding (major type 3).
Encode chunked bytes using indefinite-length encoding (major type 2).
Sort strategy for map keys.
Unsorted preserves insertion order (fast path — no key serialization).Sorted toComparable serializes each key to Bytes, extracts a
comparable via toComparable, and sorts entries by that value.Encode a list of items using the same element encoder.
This is a helper function for array when all elements are of the same type.
Encode a CBOR map (major type 5).
Encode an associative list as a CBOR map (major type 5).
This is a convenience wrapper around map that takes encoder
functions for keys and values instead of pre-built encoder pairs.
CE.associativeList CE.Unsorted
Definite
CE.int
CE.string
[ ( 1, "Alice" ), ( 2, "Bob" ) ]Encode a Dict as a CBOR map (major type 5).
Encode a keyed record as a CBOR map with a shared key encoder.
Nothing entries are omitted from the output — this distinguishes
"field absent" from "field is null."
Concatenate multiple CBOR items without a wrapping array.
Produces a CBOR Sequence (RFC 8742).
Inject pre-encoded CBOR bytes without validation.
If the bytes are not valid CBOR, the output is malformed.
RFC 8949 §4.2.1 Core Deterministic Encoding.
Lexicographic byte order on encoded keys.
RFC 7049 / RFC 8949 §4.2.3 Canonical CBOR.
Shorter keys first, then lexicographic within same length. (Also used by CTAP2/FIDO canonical encoding.)
An encoder that produces CBOR bytes.