std/data/cbor

Standard Library documentation

CBOR encoding and decoding for ZuzuScript.

Module

Name
std/data/cbor
Area
Standard Library
Source
modules/std/data/cbor.zzm

NAME

std/data/cbor - CBOR encoding and decoding for ZuzuScript.

SYNOPSIS

  from std/data/cbor import CBOR, TaggedValue;

  let codec := new CBOR();
  let bytes := codec.encode({ answer: 42 });
  let value := codec.decode(bytes);

  // Decode CBOR maps to Zuzu PairLists instead of Dicts.
  // This preserves key order and allows duplicate keys.
  codec := new CBOR( pairlists: true );

IMPLEMENTATION SUPPORT

This module is supported by zuzu.pl, zuzu-rust, and zuzu-js on Node and Electron. It is partially supported by zuzu-js in the browser: in-memory CBOR encode/decode coverage passes, but file-backed load/dump coverage is unsupported because browser filesystem capability is unavailable.

DESCRIPTION

Pure-Zuzu implementation of CBOR (RFC 8949).

EXPORTS

Classes

  • TaggedValue({ tag: Number, value: value })

    Constructs a tagged CBOR value. Returns: TaggedValue. Stores the CBOR tag number and associated value.

  • CBOR({ pairlists?: Boolean })

    Constructs a CBOR codec. Returns: CBOR. The pairlists option makes decoded maps return as PairList values instead of Dict values.

    • codec.encode(value)

      Parameters: value is any CBOR-encodable ZuzuScript value. Returns: BinaryString. Encodes value as CBOR bytes.

    • codec.encode_binarystring(value)

      Alias for codec.encode(value).

    • codec.decode(BinaryString raw)

      Parameters: raw is CBOR data. Returns: value. Decodes CBOR bytes into the equivalent ZuzuScript value.

    • codec.decode_binarystring(BinaryString raw)

      Alias for codec.decode(raw).

    • codec.load(Path path)

      Parameters: path is a std/io Path. Returns: value. Reads CBOR bytes from path and decodes them.

    • codec.dump(Path path, value)

      Parameters: path is a std/io Path and value is any CBOR-encodable value. Returns: null. Encodes value and writes CBOR bytes to path.

COPYRIGHT AND LICENCE

std/data/cbor is copyright Toby Inkster.

It is free software; you may redistribute it and/or modify it under the terms of either the Artistic License 1.0 or the GNU General Public License version 2.