Search documentation of jhn_stdlib

# jhn_cbor (jhn_stdlib v5.10.3)

View Source

A CBOR library based on:

Concise Binary Object Representation (CBOR) (rfc8949)

IANA Considerations and IETF Protocol and Documentation Usage for IEEE 802 Parameters (rfc9542)

Supported tags:

rfc8949: 0, 1, 2, 3, 4, 5, 24, 32, 33, 34, 55799

rfc9542: 48

Tags unlikely to ever be supported: 21, 22, 23

CBOR cbor() values are represented as follows:

Simple values:

False(20) : false

True(21) : true

Null(22) : null

Undefined(23) : undefined

(Integer) : {simple, integer()}

Numbers:

Integer : integer()

Float : float() | {float16, float()} | {float32, float()} | {float64, float()} | inf | neg_inf | nan.

Bistrings:

String : {string, binary()}

indefinite length String : {strings, [{string, binary()}]}

Text : binary()

indefinite length Texts : {texts, [binary()]}

Collections:

Map : #{cbor() => cbor()}

indefinite length Map : {map, [#{cbor() => cbor()}]}

Array : [cbor()]

Arrays : {arrays, [cbor()]}

Tags:

timestamp(0) : {tag, timestamp, integer() | binary()}

posix(1) : {tag, posix, integer() | float() | null | undefined}

bignum(2) : {tag, bignum, non_neg_integer()}

bignum(3) : {tag, bignum, neg_integer()}

decimal_fraction(4) : {tag, decimal_fraction, {integer(), integer()}}

bigfloat(5) : {tag, bigfloat, {integer(), integer()}

embedded(24) : {tag, embedded, binary()}

uri(32) : {tag, uri, #uri{} | binary()}

base64url(33) : {tag, base64url, binary()}

base64(34) : {tag, based, binary()}

mac(48) : {tag, mac, binary()}

cbor(55799) : {tag, cbor, cbor()}

(Integer) : {tag, integer(), _}

# Summary

## Types

array/0
cbor/0
cfloat/0
cmap/0
cstring/0
ctext/0
opt/0
opts/0
simple/0
tag/0
tag_alias/0
tag_content/0

## Functions

decode(Binary)

Decodes the binary into a tuple of structured Erlang term.

decode(Binary, Opts)

Decodes the binary into a structured Erlang term or a tuple of a structured Erlang term and the rest of the binary if the continue option is given. A {tag_callbacks, [{integer(), nodule()}]} can supplied to specify a module with a cbor_decode_tag callback function to decode tags not supporrted in this module.

encode(CBOR)

Encodes the structured Erlang term as an iolist. Equivalent of encode(Term, []) -> CBOR.

encode(T, Opts)

Encodes the structured Erlang term as an iolist or binary. Encode will give an exception if the erlang term is not well formed. Options are: binary -> a binary is returned iolist -> an iolist is returned (default) {tag_callbacks, [{integer(), nodule()}]} specifies a module with a cbor_encode_tag callback function to decode tags not supporrted in this module.

# Types

# array/0

    -type array() :: [cbor()] | {arrays, [[cbor()]]}.

# cbor/0

    -type cbor() ::
              simple() | integer() | float() | cfloat() | cstring() | ctext() | cmap() | array() | tag().

# cfloat/0

    -type cfloat() :: {float16, float()} | {float32, float()} | {float64, float()} | inf | neg_inf | nan.

# cmap/0

    -type cmap() :: #{cbor() => cbor()} | {maps, [#{cbor() => cbor()}]}.

# cstring/0

    -type cstring() :: {string, binary()} | {strings, [{string, binary()}]}.

# ctext/0

    -type ctext() :: binary() | {texts, [binary()]}.

# opt/0

    -type opt() :: binary | iolist | continue | {tag_callbacks, [{integer(), atom()}]}.

# opts/0

    -type opts() :: [opt()].

# simple/0

    -type simple() :: true | false | null | undefined | {simple, integer()}.

# tag/0

    -type tag() :: {tag, integer(), tag_content()} | {tag, tag_alias(), tag_content()}.

# tag_alias/0

    -type tag_alias() ::
              timestamp | posix | bignum | decimal_fraction | bigfloat | embedded | uri | base64url |
              base64 | mac | cbor.

# tag_content/0

    -type tag_content() :: _.

# Functions

# decode(Binary)

    -spec decode(binary()) -> {cbor(), binary()}.

Decodes the binary into a tuple of structured Erlang term.

# decode(Binary, Opts)

    -spec decode(binary(), opts()) -> {cbor(), binary()}.

Decodes the binary into a structured Erlang term or a tuple of a structured Erlang term and the rest of the binary if the continue option is given. A {tag_callbacks, [{integer(), nodule()}]} can supplied to specify a module with a cbor_decode_tag callback function to decode tags not supporrted in this module.

# encode(CBOR)

    -spec encode(cbor()) -> iodata().

Encodes the structured Erlang term as an iolist. Equivalent of encode(Term, []) -> CBOR.

# encode(T, Opts)

    -spec encode(cbor(), opts()) -> binary().

Encodes the structured Erlang term as an iolist or binary. Encode will give an exception if the erlang term is not well formed. Options are: binary -> a binary is returned iolist -> an iolist is returned (default) {tag_callbacks, [{integer(), nodule()}]} specifies a module with a cbor_encode_tag callback function to decode tags not supporrted in this module.