std::json
std::json reads and writes JSON for Sakura, strictly, as RFC 8259 writes it:
any value may stand alone, text must be UTF-8, and comments, trailing commas,
NaN, Infinity, and single quotes are errors. json::parse reads text into a
json::document, which holds every value of it, json::reader gives one token
at a time, and json::writer writes JSON as it is given it. A value whose type
marks its fields with @serial is written as JSON and read back by those marks,
over std::serial.
Using it
Require the standard library in your blossomfile, and name the module among a
target's deps, with the other modules of std it reads:
requires:
- https://git.malunal.studio/sakura/std@v1.0.0
targets:
- name: app
type: program
deps: [std.json, std.io]
import std::io;
import std::json;
def main(): void {
val read := json::parse("{\"name\": \"sakura\", \"stars\": 5}") else (_) {
io::println("That isn't JSON.");
return;
};
if read.root.get("name") is some(name) {
if name.as_text() is value(text) {
io::println(text);
}
}
}
Testing
From the standard library's root, where the conformance test finds its suite,
blossom test --suite std.json runs them, or:
sakurac run --lib std=. tests/main.sakura tests/json/*.sakura
The conformance test reads each file of JSONTestSuite's test_parsing, in
tests/json/parsing, whose license is beside it.