calog/examples/scripts/libraries/jsonInBerry.be

26 lines
949 B
Text

# Showcase the calog json library from Berry: jsonParse a small JSON document,
# read fields out of the resulting map, then build a Berry map and jsonStringify
# it back to text.
# Run: bin/calog examples/scripts/libraries/jsonInBerry.be
# Parse a small JSON string into a Berry map.
var text = '{ "name": "calog", "version": 2, "stable": true, "engines": ["lua", "berry", "scheme"] }'
var doc = jsonParse(text)
# Read fields out of the parsed map.
calogPrint("name:", doc["name"])
calogPrint("version:", doc["version"])
calogPrint("stable:", doc["stable"])
calogPrint("first engine:", doc["engines"][0])
calogPrint("engine count:", doc["engines"].size())
# Build a fresh map and serialize it to JSON text.
var report = {
"tool": doc["name"],
"ok": doc["stable"],
"engines": doc["engines"].size()
}
calogPrint("stringified:", jsonStringify(report))
# Tear down and exit cleanly (required -- calog does not auto-exit).
calogExit(0)