clj-cbor.core

Core CBOR library API.

cbor-codec

(cbor-codec & opts)

Construct a new CBOR codec with no configuration. Note that this does not include any read and write handlers. See the default-codec and the default-read-handlers and default-write-handlers vars.

Arguments may be a map or a sequence of key/value pairs. Valid options are:

  • :write-dispatch function which is called to provide a dispatch value based on the data to be rendered. (default: class)
  • :write-handlers lookup function from dispatch values to handlers which take some data to be encoded and return a transformed version of it (typically a tagged value).
  • :read-handlers lookup function from integer tags to handlers which take the embedded item and return the parsed data value.

decode

(decode input)(decode decoder input)(decode decoder input eof-guard)

Decode a single CBOR value from the input.

This uses the given codec or the default-codec if none is provided. If at the end of the input, this returns eof-guard or nil.

The input must be an input stream or something coercible to one like a file or byte array. Note that coercion will produce a BufferedInputStream if the argument is not already a stream, so repeated reads will probably not behave as expected! If you need incremental parsing, make sure you pass in something that is already an InputStream.

decode-seq

(decode-seq input)(decode-seq decoder input)

Decode a sequence of CBOR values from the input.

This uses the given codec or the default-codec if none is provided. The returned sequence is lazy, so take care that the input stream is not closed before the entries are realized.

The input must be an input stream or something coercible to one - see decode for usage notes.

default-codec

Default CBOR codec to use when none is specified.

default-read-handlers

Map of default tag handlers to use, keyed by tag.

The default choice of representation for instants in time is java.time.Instant.

default-write-handlers

Map of default write handlers to use, keyed by class.

The default choice of encoding for instants in time is the numeric epoch representation (tag 1).

encode

(encode value)(encode encoder value)(encode encoder output value)

Encode a single value as CBOR data.

Writes the value bytes to the provided output stream, or returns the value as a byte array if no output is given. The default-codec is used to encode the value if none is provided.

encode-seq

(encode-seq values)(encode-seq encoder values)(encode-seq encoder output values)

Encode a sequence of values as CBOR data. This eagerly consumes the input sequence.

Writes the value bytes to the provided output stream, or returns the value as a byte array if no output is given. The default-codec is used to encode the value if none is provided.

self-describe

(self-describe value)

Wraps a value with a self-describing CBOR tag. This will cause the first few bytes of the data to be D9D9F7, which serves as a distinguishing header for format detection.

slurp

(slurp f & opts)

Opens an input stream from f, reads the first value from it, then closes the stream.

slurp-all

(slurp-all f & opts)

Opens an input stream from f, reads all values from it, then closes the stream.

spit

(spit f value & opts)

Opens an output stream to f, writes value to it, then closes the stream.

Options may include :append to write to the end of the file instead of truncating.

spit-all

(spit-all f values & opts)

Opens an output stream to f, writes each element in values to it, then closes the stream.

Options may include :append to write to the end of the file instead of truncating.