magus v0.3.0 is out. See what's new
¶ View markdown source · ✎ Suggest an edit
1 min read

json

JSON encode/decode.

Naming convention: import the module under its bare name (import "json"), reach members with a backslash, and call methods in camelCase: json\someMethod.

Methods

parse

Decode a JSON string into a value (map, list, string, number, or boolean).

Signature: json\parse(s) → any1 · source

Parameter Type Optional Description
s string

Returns: any

Example:

import "std";
import "json";

final v = json\parse("\{\"name\": \"api\", \"port\": 8080\}");
std\print(v["name"]);
std\print(v["port"]);
// -> api
// -> 8080

stringify

Encode a value as a JSON string. With no indent (or "") the output is compact; pass an indent string (e.g. " " or "\t") for pretty, multi-line output.

Signature: json\stringify(value, [indent]) → string2 · source

Parameter Type Optional Description
value any
indent string yes

Returns: string

Example:

import "std";
import "json";

final config = { "target": "build", "parallel": true };
std\print(json\stringify(config));
// -> {"parallel":true,"target":"build"}

// Pretty-printed with two-space indent:
std\print(json\stringify(config, "  "));

  1. json\parse is also in Buzz's standard library (serialize.jsonDecode); the magus form is sandbox-aware. ↩︎

  2. json\stringify is also in Buzz's standard library (serialize.jsonEncode); the magus form is sandbox-aware. ↩︎

auto-generatedjsonmodulestdlibmagusfile
Last updated (e0463131)
Earlier changes on this page (1)

Full history ↗ · Blame source ↗

Glossary

Magusfile

The magusfile.buzz that declares a project's targets (as export funs) and binds its spells. See targets.

Target

A named operation (build, test, ...) you invoke with magus run <target>; it may compose a spell's tool-native operations and depend on other targets. See targets.

Module

A magus stdlib namespace a magusfile imports for host capabilities: filesystem, exec, vcs, and more. See the module reference.

Buzz

The language magusfiles are written in (the .buzz engine). See engines.

Sandbox

The restricted filesystem and environment a target runs in, so builds stay reproducible and side-effect-free. See sandbox.

Conventions

This page uses none of the site's convention markers. The full set is on the conventions page.