JSON
Vow treats JSON like JavaScript: dynamic objects and arrays from json.parse, built with { key: value } literals, read with . and [], and serialized with json.stringify.
import jsonParse and stringify
Section titled “Parse and stringify”let doc = match json.parse(raw) { Ok(v) => v, Err(_e) => json.null(),};let body = json.stringify({ ok: true, count: 3 });Literals
Section titled “Literals”let health = { ok: true, service: "my-app" };
var todos: JsonValue = [ { id: 1, title: "Learn Vow", done: false }, { id: 2, title: "Ship API", done: false },];Annotate JsonValue when an array holds objects or mixed JSON values.
Access
Section titled “Access”Read
doc.title // JsonValue (null if missing)doc["title"] // bracketjson.get_string(doc, "title", "untitled")json.string_or(doc.title, "")Write (primitives auto-wrap)
obj.title = "hello";obj["count"] = 42;obj.ok = true;In HTTP handlers
Section titled “In HTTP handlers”import vws from vow_web_serverimport json
fn health(_req: request.Request) -> response.Response { return vws.json(json.stringify({ ok: true }));}
fn create(body: String) -> response.Response { let title = match json.parse(body) { Ok(doc) => json.get_string(doc, "title", "untitled"), Err(_e) => "untitled", }; return vws.json(201, json.stringify({ ok: true, title: title }));}Use vws.json_mw() middleware to validate incoming JSON bodies before handlers run.
Full demo
Section titled “Full demo”The hello-app project (playground) is a todo API + static UI using this JSON API end-to-end:
cd hello-appvpm installvow run src/main.vowopen http://127.0.0.1:8787- HTTP API
- Built-in functions — JSON
- hello-app README (monorepo)