elm-cardano/cbor/1.0.0

Cbor.Encode

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)
type Encoder

An encoder that produces CBOR bytes.

encode : Encoder -> Bytes

Produce CBOR bytes from an encoder.

Primitives

int : Int -> Encoder

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.

bigInt : Sign -> Bytes -> Encoder

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.

  • If the magnitude fits in 8 bytes, encodes as a 64-bit CBOR integer (major type 0 or 1, additional info 27).
  • If the magnitude exceeds 8 bytes, encodes as a bignum (tag 2 for 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.

float : Float -> Encoder

Encode a float using the shortest IEEE 754 form that preserves the value.

bool : Bool -> Encoder

Encode a boolean.

Encode a CBOR null.

Encode a CBOR undefined.

maybe : (a -> Encoder) -> Maybe a -> Encoder

Encode a Maybe value: Nothing becomes CBOR null, Just x is encoded with the given encoder.

string : String -> Encoder

Encode a UTF-8 text string (major type 3).

bytes : Bytes -> Encoder

Encode a byte string (major type 2).

simple : Int -> Encoder

Encode a CBOR simple value (major type 7, values 0–255 excluding 20–23).

Explicit Width Control

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.

Chunked Strings/Bytes

stringChunked : List String -> Encoder

Encode a chunked text string using indefinite-length encoding (major type 3).

bytesChunked : List Bytes -> Encoder

Encode chunked bytes using indefinite-length encoding (major type 2).

Collections

type Sort comparable
= Unsorted
| Sorted (Bytes -> comparable)

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.
list : Length -> (a -> Encoder) -> List a -> Encoder

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 array (major type 4).

map : Sort comparable -> Length -> List ( Encoder, Encoder ) -> Encoder

Encode a CBOR map (major type 5).

Sort comparable
-> Length
-> (k -> Encoder)
-> (v -> Encoder)
-> List ( k, v )

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" ) ]
Sort comparable2
-> Length
-> (k -> Encoder)
-> (v -> Encoder)
-> Dict k v

Encode a Dict as a CBOR map (major type 5).

Encode a CBOR semantic tag (major type 6).

Sort comparable
-> Length
-> (k -> Encoder)
-> List ( k, Maybe Encoder )

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).

Escape Hatches

Encode a CborItem losslessly.

rawUnsafe : Bytes -> Encoder

Inject pre-encoded CBOR bytes without validation.

If the bytes are not valid CBOR, the output is malformed.

Predefined Sort Orders

RFC 8949 §4.2.1 Core Deterministic Encoding.

Lexicographic byte order on encoded keys.

canonicalSort : Sort ( Int, String )

RFC 7049 / RFC 8949 §4.2.3 Canonical CBOR.

Shorter keys first, then lexicographic within same length. (Also used by CTAP2/FIDO canonical encoding.)