Skip to content

std/json

Import with import std/json.

enum Json {
    String String
    Int Int
    Float Float
    Bool Bool
    Arr List<Json>
    Obj Map<String, Json>
    Null
}

Tagged enum representing a parsed JSON value.

Variant names align with the Nomi-side type names rather than the JSON spec’s vocabulary — String String, Int Int, Float Float, Bool Bool keep the payload type visible at the pattern site so case jv { Int(n) -> ... } reads as the obvious shape match.

Number representation note: JSON has a single number type. Json distinguishes Int(Int) from Float(Float) at parse time based on whether the source has a decimal point, exponent, or would overflow Int. This makes integer fields easy to pattern-match while still preserving fractional values. Text round-trips are not representation-preserving for this split: Json.encode(Json.Float(36.0)) emits 36.

fn decode(source: String): Result<Json, JsonDecodeError>

type function on Json

Parse a JSON document into a Json tree. Returns Err with a structured JsonDecodeError on syntax errors, type errors, or trailing content past the first value.

Numbers are decoded as Int when they fit in int64 with no fractional part / exponent; otherwise as Float. See the Json doc-comment for the rationale.

fn encode(jv: Json): String

type function on Json

Serialize a Json tree as compact JSON text (no whitespace between tokens).

Number-rendering note: Float(36.0) renders as "36", not "36.0". The Int/Float distinction captured by Json.decode therefore does not necessarily survive a text round-trip.

fn to_dynamic(jv: Json): Dynamic

type function on Json

Converts a Json tree to Dynamic, preserving the same shape: strings, numbers, booleans, arrays, objects, and null.

impl Display

fn to_string(jv: Json): String

impl Display.to_string

impl Debug

fn inspect(jv: Json): String

impl Debug.inspect

struct JsonDecodeError {
    message: String
    line: Int
    col: Int
    offset: Int
}

Structured decode failure from Json.decode. message is the parser’s human-readable reason; line and col are 1-based source coordinates for human display; offset is the byte position for programmatic tooling.

Named with the Json prefix so it is distinct from decode errors returned by other modules.

impl Display

fn to_string(e: JsonDecodeError): String

impl Display.to_string

impl Debug

fn inspect(value: JsonDecodeError): String

impl Debug.inspect