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:
utf8Accepted for compatibility with older runtime implementations. Use
encode_binarystringwhen 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:
valueis any JSON-encodable ZuzuScript value. Returns:String. Encodesvalueas JSON text.codec.encode_binarystring(value)Parameters:
valueis any JSON-encodable ZuzuScript value. Returns:BinaryString. Encodesvalueas UTF-8 JSON bytes.codec.decode(String json)Parameters:
jsonis JSON text. Returns: value. Decodes JSON into the equivalent ZuzuScript value.codec.decode_binarystring(BinaryString json)Parameters:
jsonis UTF-8 JSON bytes. Returns: value. Decodes JSON into the equivalent ZuzuScript value.codec.load(Path path)Parameters:
pathis astd/ioPath. Returns: value. Reads JSON text frompathand decodes it.codec.dump(Path path, value)Parameters:
pathis astd/ioPathandvalueis any JSON-encodable value. Returns:null. Encodesvalueand writes JSON text topath.
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.