Sakura Lang

Decode JSON into a refined record

Decode an order once and let its field refinements reject invalid external data.

Decode an order once and let its field refinements reject invalid external data.

Json.decode infers Order from the return annotation and checks both the record shape and the predicates on item and quantity. Its Result keeps malformed JSON, missing fields and invalid quantities recoverable; main handles both branches and reports the decoding path. Use from only for trusted values where a failed predicate should be a defect. No permissions are needed.

Running main prints 2 x tea; the repository's check runs it and both tests. The tests cover a valid record, invalid JSON, an empty name, missing quantity and both out-of-range bounds.

Source project

Run it

From the repository root, with this checkout's sakura on PATH:

cd compiler/examples/json_refined
sakura check --json
sakura run
sakura test --json

Source and tests

src/main.sak includes all required imports and tests.

import std/json.{DecodeError}

type Quantity = Int where it >= 1 && it <= 100
type Order = { item: String where String.len(it) > 0, quantity: Quantity }

fn read_order(text: String) -> Result<Order, DecodeError> { Json.decode(text) }

pub fn main() -> Unit ! {Console} {
  match read_order("\{\"item\":\"tea\",\"quantity\":2\}") {
    Ok(order) => Console.print("{order.quantity} x {order.item}"),
    Err(err) => Console.print("invalid order at {err.path}: {err.message}"),
  }
}

test "JSON decoding validates the record fields" {
  expect_eq(
    read_order("\{\"item\":\"tea\",\"quantity\":2\}"),
    Ok(Order { item: "tea", quantity: 2 }),
  )
}

test "malformed JSON and invalid fields are recoverable" {
  expect(Result.is_err(read_order("not json")))
  expect(Result.is_err(read_order("\{\"item\":\"tea\",\"quantity\":0\}")))
  expect(Result.is_err(read_order("\{\"item\":\"tea\",\"quantity\":101\}")))
  expect(Result.is_err(read_order("\{\"item\":\"\",\"quantity\":1\}")))
  expect(Result.is_err(read_order("\{\"item\":\"tea\"\}")))
}

Open in playground

Expected stdout

2 x tea

Manifest and permissions

sakura.toml names the package and grants what the program may use.

[package]
name = "json_refined"
version = "0.1.0"
edition = "2026"

[permissions]

How this recipe is checked

Every project runs offline in the repository's tests (cargo test -p sakura_cli --test examples), against the compiler in the same checkout, on the JavaScript backend. A recipe that talks to an HTTP API or a model replays a cassette in its tests: a committed file of recorded requests and responses. These cassettes are synthetic, so no network access or API key is needed. Running a project needs Node 26, whose built-in SQLite the SQLite recipe uses.

To adapt a recipe, build the CLI from the same checkout and run sakura --version and sakura prompt first. Sakura Lang is pre-1.0: read STABILITY.md before depending on an API. The initial API baseline is compiler revision 5cae349516c21714d90d36e87f1cfd6debe980c6. An installed skill's frontmatter records its compiler version and build revision. These teaching examples are separate from the agent evaluation tasks.

Each page is generated from its example's recipe.md, sakura.toml, src/main.sak and fixture files. Edit those sources, not the rendered pages, then run cargo run -p agent_docs from compiler/ to regenerate the cookbook and the skill reference.

Edit on GitHub

Last updated

On this page