Slide 23 of 37

JSON

Encoding, and decoding into a declared type
type Greeting { name: Str }

proc main(): void {
	body := hive.json.encode(Greeting("ada"))
	echo body

	parsed := hive.json.parse(body) with Greeting
	if parsed is Result.Ok(g) {
		echo "hello, {g.name}!"
	}
}
Decoding into a Table
type Greeting { name: Str }

proc main(): void {
	body := hive.json.encode(Greeting("ada"))

	asTable := hive.json.parse(body) with Table
	if asTable is Result.Ok(table) {
		if hive.json.get(table, "name") is Result.Ok(name) {
			echo name
		}
	}
}
hive.json treats a Hive type declaration as the JSON schema itself. parse(text) with T derives a decoder for T at compile time and returns a Result whose Error carries the exact path that failed to decode; parse(text) with Table instead flattens the whole document into [path, value] rows, which hive.json.get looks up by a dotted path. encode(value) derives the encoder from the static type, so it can never fail — there is no Result to check on the way out. table(text) reads a JSON array of flat objects as a headered Table.