Sign in

    json5

    JSON5 parser and stringifier for MoonBit

    json
    json5
    Download zip
    Author
    Version
    0.0.3
    License
    Apache-2.0
    Last updated
    5 months ago
    Downloads
    74

    No dependencies

    #tiye/json5

    This document was generated with AI assistance and then reviewed/edited for this project. Contributions are welcome via PRs, and AI-assisted review may be used during the review process.

    A lightweight JSON5 parser/serializer for MoonBit.

    This package lets you parse JSON5 input (comments, trailing commas, single-quoted strings, unquoted keys, hex numbers, Infinity, NaN) and work with either:

    • Json5Value (package-native value tree), or
    • core Json (via helper APIs and traits).

    #Features

    • Parse JSON5 object/array/value syntax
    • Support line comments // ... and block comments /* ... */
    • Support trailing commas in objects and arrays
    • Support unquoted object keys (identifier style)
    • Support single-quoted strings
    • Support number forms: decimal, exponent, hex (0x10), Infinity, NaN
    • Convert between Json5Value and core Json using ToJson / FromJson

    #Installation

    Add this package to your MoonBit module dependencies (according to your workspace setup), then import and use:

    ///|
    let value = @json5.parse("{foo: 'bar'}")

    #Quick Start

    #Parse to Json5Value

    ///|
    test "readme parse json5 value" {
    let value = @json5.parse("{foo: 'bar', nums: [1, 2,],}")
    guard value is @json5.Object(obj)
    assert_true(obj.get("foo") is Some(_))
    }

    #Stringify from Json5Value

    ///|
    test "readme stringify json5 value" {
    let value = @json5.parse("{ok: true}")
    let text = @json5.stringify(value)
    // valid identifiers are written unquoted in JSON5
    assert_true(text.contains("ok:true"))
    }

    #Parse directly to core Json

    ///|
    test "readme parse json5 to json" {
    let json = @json5.parse_json("{foo: [1, true, 'x']}")
    @json.json_inspect(json, content={ "foo": [1, true, "x"] })
    }

    #Stringify from core Json

    ///|
    test "readme stringify json" {
    let text = @json5.stringify_json({ "name": "moonbit", "v": 1 })
    // valid identifiers are written unquoted in JSON5
    assert_true(text.contains("name:\"moonbit\""))
    }

    #Public API

    • parse(input : String) -> Json5Value raise Error
    • parse_json(input : String) -> Json raise Error
    • stringify(val : Json5Value) -> String
    • stringify_json(json : Json) -> String raise @json.JsonDecodeError

    Types:

    • pub enum Json5Value { Null | True | False | Number(Double) | String(String) | Array(Array[Json5Value]) | Object(Map[String, Json5Value]) }

    Traits:

    • impl ToJson for Json5Value
    • impl @json.FromJson for Json5Value

    #Error Handling

    • parse / parse_json raise Error for malformed JSON5 text.
    • stringify_json raises @json.JsonDecodeError if conversion from core Json to Json5Value fails.

    Use try/catch when you need resilient decoding.

    #Current Limitations

    • Unicode escape in strings (\uXXXX) is not implemented yet.
    • Serializer currently outputs compact text only (no pretty formatting mode).

    #Tests

    Run:

    moon test

    Json5Value

    pub enum Json5Value {
    Null
    True
    False
    Number(Double)
    String(String)
    Array(Array[Json5Value])
    Object(Map[String, Json5Value])
    } derive(Eq,
    Debug
    )

    Represents a JSON5 value tree.

    This enum is the primary data model for parsed JSON5 content in this package. It mirrors regular JSON shapes while preserving JSON5-friendly numeric forms during parsing (for example NaN and Infinity).

    Variants:
    • Null
    • True
    • False
    • Number(Double)
    • String(String)
    • Array(Array[Json5Value])
    • Object(Map[String, Json5Value])

    Example:

    test {
    let value = @json5.parse("{foo: [1, true, 'x']}")
    guard value is Object(obj)
    assert_true(obj.get("foo") is Some(Array(_)))
    }
    impl Show for Json5Value

    parse

    fn parse(input : String) -> Json5Value raise

    Parses a JSON5 text into Json5Value.

    This is the primary entry point for JSON5 parsing in this package.

    Raises Error for malformed input.

    Example:

    test {
    let value = @json5.parse("{foo: 'bar', nums: [1, 2,],}")
    guard value is Object(obj)
    assert_true(obj.get("foo") is Some(_))
    }

    parse_json

    fn parse_json(input : String) -> Json raise

    Parses JSON5 text directly into core Json.

    This helper is convenient when your downstream code already uses @json APIs and you do not need to manually inspect Json5Value.

    Equivalent to:

    parse(input).to_json()

    stringify

    fn stringify(val : Json5Value) -> String

    Serializes Json5Value to compact text.

    Object keys that are valid JSON5 identifiers are written unquoted; all other keys are quoted.

    Number behavior:
    • finite numbers: normal decimal formatting
    • NaN: "NaN"
    • positive infinity: "Infinity"
    • negative infinity: "-Infinity"

    Example:

    test {
    let value = @json5.parse("{foo: 'bar'}")
    let text = @json5.stringify(value)
    assert_true(text.contains("foo:\"bar\""))
    }

    stringify_json

    fn stringify_json(json : Json) -> String

    Serializes core Json to compact JSON5 text.

    Object keys that are valid JSON5 identifiers are written unquoted.

    stringify_json_pretty

    fn stringify_json_pretty(json : Json, indent_size? : Int) -> String

    Serializes core Json to a pretty-printed JSON5 string.

    Convenience wrapper around stringify_pretty that converts Json first. The indent_size labeled argument defaults to 2.

    stringify_pretty

    fn stringify_pretty(val : Json5Value, indent_size? : Int) -> String

    Serializes Json5Value to a pretty-printed JSON5 string.

    Object keys that are valid JSON5 identifiers are written unquoted. The indent_size labeled argument controls indentation spaces per level and defaults to 2.

    Example:

    test {
    let value = @json5.parse("{foo: [1, true]}")
    let text = @json5.stringify_pretty(value)
    assert_true(text.contains("foo: [\n"))
    }

    Source Files

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io