Skip to content

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 json
let doc = match json.parse(raw) {
Ok(v) => v,
Err(_e) => json.null(),
};
let body = json.stringify({ ok: true, count: 3 });
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.

Read

doc.title // JsonValue (null if missing)
doc["title"] // bracket
json.get_string(doc, "title", "untitled")
json.string_or(doc.title, "")

Write (primitives auto-wrap)

obj.title = "hello";
obj["count"] = 42;
obj.ok = true;
import vws from vow_web_server
import 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.

The hello-app project (playground) is a todo API + static UI using this JSON API end-to-end:

Terminal window
cd hello-app
vpm install
vow run src/main.vow
open http://127.0.0.1:8787