Sign in

    json-pretty

    JSON pretty-printer: JsonValue tree with JSON.stringify-style indentation. Warm-up package for the moonbit-sqlfmt toolchain.

    json
    pretty-print
    formatter
    Download zip
    Version
    0.1.0
    License
    Apache-2.0
    Last updated
    2 hours ago
    Downloads
    3

    No dependencies

    #json-pretty

    JSON pretty-printer for MoonBit: build a JsonValue tree, render it with JSON.stringify(value, null, indent)-style indentation.

    Warm-up package for the moonbit-sqlfmt project (M0 milestone): it exercises the full declare → test → implement → publish loop.

    #Usage

    let json : @json-pretty.JsonValue = Obj([
    ("name", Str("Alice")),
    ("age", Number(30.0)),
    ("tags", Arr([Str("sql"), Str("moonbit")])),
    ])
    println(json.pretty())

    Output:

    { "name": "Alice", "age": 30, "tags": [ "sql", "moonbit" ] }

    • pretty(indent~ = 2) — spaces per nesting level
    • empty containers stay inline: {}, []
    • strings are escaped exactly like JSON.stringify

    #API

    MemberDescription
    JsonValueNull \| Bool(Bool) \| Number(Double) \| Str(String) \| Arr(Array[JsonValue]) \| Obj(Array[(String, JsonValue)])
    JsonValue::pretty(self, indent~ : Int = 2) -> String

    Object members keep insertion order (stored as an array of pairs, not a map).

    JsonValue

    pub(all) enum JsonValue {
    Null
    Bool(Bool)
    Number(Double)
    Str(String)
    Arr(Array[JsonValue])
    Obj(Array[(String, JsonValue)])
    } derive(Eq)

    JSON value tree used by the warm-up pretty-printer. Object members keep insertion order, so Obj stores an array of pairs instead of a hash map.

    JsonValue::equal

    fn JsonValue::equal(JsonValue, JsonValue) -> Bool

    JsonValue::not_equal

    fn JsonValue::not_equal(x : JsonValue, y : JsonValue) -> Bool

    JsonValue::pretty

    fn JsonValue::pretty(self : JsonValue, indent? : Int) -> String

    Render a JSON value as indented, human-readable text.

    Behavior mirrors JSON.stringify(value, null, indent):
    • empty objects/arrays render as {} / []
    • every other container spans multiple lines, one member per line
    • indent is the number of spaces per nesting level (default 2)

    Source Files