Sign in

    json_parser

    Fast and compliant JSON parser and serializer for MoonBit supporting the full JSON specification with strict validation, unicode escapes, pretty printing, and detailed error reporting

    json
    parser
    serializer
    stringify
    parse
    rfc8259
    data
    format
    encoder
    decoder
    Download zip
    Author
    Version
    0.1.1
    License
    Apache-2.0
    Last updated
    11 months ago
    Downloads
    131

    No dependencies

    #JSON Parser for MoonBit

    A fast and compliant JSON parser and serializer for MoonBit, supporting the full JSON specification with proper error handling.

    #Features

    • ✅ Complete JSON parsing - null, boolean, number, string, array, object
    • ✅ JSON serialization - Convert JsonValue back to JSON strings
    • ✅ Pretty printing - Configurable indentation for readable output
    • ✅ Unicode support - Full unicode escape sequence handling (\uXXXX)
    • ✅ Error handling - Detailed error messages with position tracking
    • ✅ Strict validation - Rejects trailing commas, duplicate keys, invalid escapes
    • ✅ Type-safe API - Helper methods for safe value extraction

    #Installation

    moon add maria/json_parser

    #Quick Example

    // Parse JSON
    let json_str = "{\"name\": \"MoonBit\", \"version\": 1}"
    let value = parse(json_str)

    // Extract values
    match value {
    Object(obj) => {
    println(obj.get("name")) // Some("MoonBit")
    }
    _ => ()
    }

    // Create and stringify
    let data : JsonValue = Object({
    "name": String("Alice"),
    "age": Number(30.0),
    })
    println(stringify(data))
    // {"name":"Alice","age":30}

    #API

    #Parsing

    • parse(input: String) -> JsonValue raise JsonError

    #Serialization

    • stringify(value: JsonValue) -> String
    • stringify_pretty(value: JsonValue, indent: Int) -> String

    #Helper Methods

    • as_object() -> Map[String, JsonValue]?
    • as_array() -> Array[JsonValue]?
    • as_string() -> String?
    • as_number() -> Double?
    • as_bool() -> Bool?
    • is_null() -> Bool

    #Documentation

    #License

    Apache-2.0

    JsonError

    pub(all) suberror JsonError {
    UnexpectedChar(Position, Char)
    UnexpectedToken(Position, String)
    UnexpectedEof(Position)
    InvalidNumber(Position, String)
    InvalidEscape(Position, String)
    InvalidUnicode(Position, String)
    TrailingComma(Position)
    DuplicateKey(Position, String)
    }

    JSON parse errors
    impl Eq for JsonError
    impl Show for JsonError

    JsonValue

    pub(all) enum JsonValue {
    Null
    Bool(Bool)
    Number(Double)
    String(String)
    Array(Array[JsonValue])
    Object(Map[String, JsonValue])
    }

    JSON value representation
    impl Eq for JsonValue
    impl Show for JsonValue

    JsonValue::as_array

    fn JsonValue::as_array(self : JsonValue) -> Array[JsonValue]?

    Helper to get array from JsonValue

    JsonValue::as_bool

    fn JsonValue::as_bool(self : JsonValue) -> Bool?

    Helper to get boolean from JsonValue

    JsonValue::as_number

    fn JsonValue::as_number(self : JsonValue) -> Double?

    Helper to get number from JsonValue

    JsonValue::as_object

    fn JsonValue::as_object(self : JsonValue) -> Map[String, JsonValue]?

    Helper to get object from JsonValue

    JsonValue::as_string

    fn JsonValue::as_string(self : JsonValue) -> String?

    Helper to get string from JsonValue

    JsonValue::is_null

    fn JsonValue::is_null(self : JsonValue) -> Bool

    Check if value is null

    Lexer

    type Lexer

    Lexer state

    Lexer::next_token

    fn Lexer::next_token(self : Lexer) -> Token raise JsonError

    Get next token

    Position

    pub(all) struct Position {
    line : Int
    column : Int
    }

    Position in source for error reporting
    impl Eq for Position
    impl Show for Position

    Token

    type Token

    JSON tokens
    impl Eq for Token
    impl Show for Token

    fib

    fn fib(n : Int) -> Int64

    parse

    fn parse(input : String) -> JsonValue raise JsonError

    Main parse function

    stringify

    fn stringify(value : JsonValue) -> String

    Convert JsonValue to JSON string

    stringify_pretty

    fn stringify_pretty(value : JsonValue, indent : Int) -> String

    Pretty print with indentation

    sum

    fn sum(data~ : Array[Int], start? : Int, length? : Int) -> Int

    data is a labelled argument without default value having type Array[Int] start is an optional labelled argument with default value 0 having type Int length is an optional labelled argument without default value having type Option[Int]