atd_runtime

    Runtime library of the MoonBit code generated by atdmbt (bobzhang/atd)

    atd
    json
    runtime
    Download zip
    Author
    Version
    0.1.0
    License
    BSD-3-Clause
    Last updated
    3 hours ago
    Downloads
    3

    #bobzhang/atd_runtime

    The runtime library of the MoonBit code generated by atdmbt, the MoonBit code generator of bobzhang/atd (a port of ATD). It has no dependencies beyond the standard library.

    Add it to the module containing the generated code:

    moon add bobzhang/atd_runtime

    and import it with the alias atd_runtime in the moon.pkg of the package containing the generated code:

    import { "bobzhang/atd_runtime" @atd_runtime, }

    It provides:

    • readers ((Json, Path) -> T raise JsonError) and writers ((T) -> Json) for the basic types and combinators for lists, options, tuples, associative arrays and records, used by the generated code;
    • parse, a JSON parser that keeps the textual representation of numbers (64-bit integers and exact floats), and stringify, which prints numbers like Yojson, for compatibility with the JSON produced by the other ATD code generators;
    • JsonError, with the location (Path) of the error in the JSON value.

    #License

    BSD-3-Clause, like ATD; see LICENSE.md.

    JsonError

    pub(all) suberror JsonError {
    JsonError(String)
    } derive(Eq,
    Debug
    )

    Error raised when a JSON value doesn't have the expected shape.
    impl Show for JsonError

    JsonError::message

    fn JsonError::message(self : JsonError) -> String

    The error message.

    Path

    pub(all) enum Path {
    Root
    Key(Path, String)
    Index(Path, Int)
    }

    Location of a JSON value within the root JSON value, used in error messages.
    impl Show for Path

    Path::index

    fn Path::index(self : Path, i : Int) -> Path

    The path of the element i of the array at this path.

    Path::key

    fn Path::key(self : Path, key : String) -> Path

    The path of the field key of the object at this path.

    Path::to_string

    fn Path::to_string(self : Path) -> String

    Format a path in the JSONPath syntax, e.g. $.items[2].id.

    bad_json

    fn[T] bad_json(expected : String, x : Json, path : Path) -> T raise JsonError

    Fail with a message indicating the type that was expected.

    format_float_g

    fn format_float_g(x : Double, precision : Int) -> String

    Format a finite number like C's printf("%.<precision>g", x).

    get_field

    fn get_field(o : Map[String, Json], key : String, keep_nulls? : Bool) -> Json?

    Look up the field of an object, treating null like a missing field unless keep_nulls is set.

    missing_field

    fn[T] missing_field(type_name : String, json_field_name : String, path : Path) -> T raise JsonError

    Fail because a required field is missing.

    parse

    fn parse(s : StringView) -> Json raise JsonError

    Parse a JSON string. Unlike @json.parse, the exact text of every number is kept in Json::Number(_, repr=Some(text)), which lets the readers of integers reject non-integers such as 1.5 or 1.0 and read 64-bit integers without loss of precision.

    read_assoc_array_into_map

    fn[K : Hash + Eq, V] read_assoc_array_into_map(read_key : (Json, Path) -> K raise JsonError, read_value : (Json, Path) -> V raise JsonError) -> ((Json, Path) -> Map[K, V] raise JsonError)

    Reader for (k * v) list <mbt repr="map">, encoded as a JSON array of pairs.

    read_assoc_object_into_array

    fn[T] read_assoc_object_into_array(read_value : (Json, Path) -> T raise JsonError) -> ((Json, Path) -> Array[(String, T)] raise JsonError)

    Reader for (string * t) list <json repr="object"> represented as an array of pairs.

    read_assoc_object_into_map

    fn[T] read_assoc_object_into_map(read_value : (Json, Path) -> T raise JsonError) -> ((Json, Path) -> Map[String, T] raise JsonError)

    Reader for (string * t) list <json repr="object"> <mbt repr="map">.

    read_bool

    fn read_bool(x : Json, path : Path) -> Bool raise JsonError

    Read a boolean.

    read_float

    fn read_float(x : Json, path : Path) -> Double raise JsonError

    Read a number.

    read_int

    fn read_int(x : Json, path : Path) -> Int raise JsonError

    Read an integer that must fit in 32 bits.

    When the text of the number is known (see parse), it must be an integer literal: 1.0 or 1e3 are rejected, like with atdgen. Otherwise, the number must be an integral value within range.

    read_int64

    fn read_int64(x : Json, path : Path) -> Int64 raise JsonError

    Read a 64-bit integer, using the exact text of the number when available (see read_int).

    read_int64_of_string

    fn read_int64_of_string(x : Json, path : Path) -> Int64 raise JsonError

    Read a 64-bit integer represented as a JSON string.

    read_int_of_string

    fn read_int_of_string(x : Json, path : Path) -> Int raise JsonError

    Read an integer represented as a JSON string (<json repr="string">).

    read_json

    fn read_json(x : Json, _path : Path) -> Json raise JsonError

    Read any JSON value (ATD type abstract).

    read_list

    fn[T] read_list(read_elt : (Json, Path) -> T raise JsonError) -> ((Json, Path) -> Array[T] raise JsonError)

    Reader for t list.

    read_nullable

    fn[T] read_nullable(read_elt : (Json, Path) -> T raise JsonError) -> ((Json, Path) -> T? raise JsonError)

    Reader for t nullable, encoded as null or the value itself.

    read_object

    fn read_object(x : Json, path : Path, type_name : String) -> Map[String, Json] raise JsonError

    Check that a JSON value is an object and return its fields.

    read_option

    fn[T] read_option(read_elt : (Json, Path) -> T raise JsonError) -> ((Json, Path) -> T? raise JsonError)

    Reader for t option, encoded as "None" or ["Some", x].

    read_optional_field

    fn[T] read_optional_field(o : Map[String, Json], key : String, read_value : (Json, Path) -> T raise JsonError, path : Path, keep_nulls? : Bool) -> T? raise JsonError

    Read an optional field, missing or null meaning None.

    read_required_field

    fn[T] read_required_field(o : Map[String, Json], type_name : String, key : String, read_value : (Json, Path) -> T raise JsonError, path : Path) -> T raise JsonError

    Read a required field.

    read_single_field

    fn read_single_field(x : Json) -> (String, Json)?

    If the JSON value is an object with a single field, return it. This is used for reading sum types represented as JSON objects (<json repr="object">).

    read_string

    fn read_string(x : Json, path : Path) -> String raise JsonError

    Read a string.

    read_tuple

    fn read_tuple(x : Json, path : Path, n : Int) -> Array[Json] raise JsonError

    Check that a JSON value is an array of length n and return its elements.

    read_unit

    fn read_unit(x : Json, path : Path) -> Unit raise JsonError

    Read null as unit.

    stringify

    fn stringify(x : Json, indent? : Int) -> String

    Print JSON compactly, or with the given indentation.

    write_assoc_array_to_object

    fn[T] write_assoc_array_to_object(write_value : (T) -> Json) -> ((Array[(String, T)]) -> Json)

    Writer for (string * t) list <json repr="object"> represented as an array of pairs.

    write_assoc_map_to_array

    fn[K, V] write_assoc_map_to_array(write_key : (K) -> Json, write_value : (V) -> Json) -> ((Map[K, V]) -> Json)

    Writer for (k * v) list <mbt repr="map">.

    write_assoc_map_to_object

    fn[T] write_assoc_map_to_object(write_value : (T) -> Json) -> ((Map[String, T]) -> Json)

    Writer for (string * t) list <json repr="object"> <mbt repr="map">.

    write_bool

    fn write_bool(x : Bool) -> Json

    Write a boolean.

    write_float

    fn write_float(x : Double) -> Json

    Write a number.

    write_float_as_int

    fn write_float_as_int(x : Double) -> Json

    Write a number rounded to an integer (<json repr="int">), exactly like atdgen: positive numbers are rounded half up; other numbers are formatted with C's %.0f, which rounds half to even (and may print -0). Non-finite numbers can't be represented in JSON; they are written as NaN or Infinity, which is not valid JSON (atdgen raises an error).

    write_float_prec

    fn write_float_prec(precision : Int) -> ((Double) -> Json)

    Build a writer for numbers with at most precision significant digits (<json precision="N">), formatted like atdgen does (C's %.Ng).

    write_int

    fn write_int(x : Int) -> Json

    Write an integer.

    write_int64

    fn write_int64(x : Int64) -> Json

    Write a 64-bit integer, preserving all its digits.

    write_int64_to_string

    fn write_int64_to_string(x : Int64) -> Json

    Write a 64-bit integer as a JSON string.

    write_int_to_string

    fn write_int_to_string(x : Int) -> Json

    Write an integer as a JSON string (<json repr="string">).

    write_json

    fn write_json(x : Json) -> Json

    Write any JSON value (ATD type abstract).

    write_list

    fn[T] write_list(write_elt : (T) -> Json) -> ((Array[T]) -> Json)

    Writer for t list.

    write_nullable

    fn[T] write_nullable(write_elt : (T) -> Json) -> ((T?) -> Json)

    Writer for t nullable.

    write_option

    fn[T] write_option(write_elt : (T) -> Json) -> ((T?) -> Json)

    Writer for t option.

    write_string

    fn write_string(x : String) -> Json

    Write a string.

    write_unit

    fn write_unit(_x : Unit) -> Json

    Write unit as null.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io