std/data/json

Standard Library documentation

JSON encoding and decoding for ZuzuScript.

Module

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

NAME

std/data/json - JSON encoding and decoding for ZuzuScript.

SYNOPSIS

  from std/data/json import JSON;

  let codec := new JSON( pretty: true );
  let text  := codec.encode({ answer: 42 });
  let data  := codec.decode(text);

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 JSON encode/decode coverage passes, but file-backed load/dump coverage is unsupported because browser filesystem capability is unavailable.

DESCRIPTION

This module provides a JSON class for turning ZuzuScript values into JSON text and parsing JSON text back into ZuzuScript values.

EXPORTS

Classes

  • JSON({ utf8?: Bool, pretty?: Bool, canonical?: Bool })

    Constructs a JSON codec. Returns: JSON.

    The constructor accepts named options:

    • utf8

      Accepted for compatibility with older runtime implementations. Use encode_binarystring when UTF-8 bytes are required.

    • pretty (default false)

      Pretty-print the JSON output.

    • canonical (default false)

      Sort keys in objects while encoding.

    • pairlists (default false)

      Decode JSON objects as PairLists instead of Dicts.

      (PairLists will always be encoded into JSON properly; this only affects decoding.)

  • codec.encode(value)

    Parameters: value is any JSON-encodable ZuzuScript value. Returns: String. Encodes value as JSON text.

  • codec.encode_binarystring(value)

    Parameters: value is any JSON-encodable ZuzuScript value. Returns: BinaryString. Encodes value as UTF-8 JSON bytes.

  • codec.decode(String json)

    Parameters: json is JSON text. Returns: value. Decodes JSON into the equivalent ZuzuScript value.

  • codec.decode_binarystring(BinaryString json)

    Parameters: json is UTF-8 JSON bytes. Returns: value. Decodes JSON into the equivalent ZuzuScript value.

  • codec.load(Path path)

    Parameters: path is a std/io Path. Returns: value. Reads JSON text from path and decodes it.

  • codec.dump(Path path, value)

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

COPYRIGHT AND LICENCE

std/data/json 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.