amjson

A complete JSON parser implementation in MoonBit

json
parser
parsing
serialization
data
moon add tonyfettes/amjson@0.1.0
Download zip
Version
0.1.0
License
Apache-2.0
Last updated
9 months ago
Downloads
19
README

#amjson

A complete JSON parser implementation in MoonBit that handles all JSON value types with proper error handling and position tracking.

#Features

  • Complete JSON Support: Parses all JSON value types:
    • null
    • Booleans (true, false)
    • Numbers (integers, floats, scientific notation)
    • Strings (with escape sequences and Unicode escapes)
    • Arrays
    • Objects

  • Error Handling: Provides detailed error messages with position information
  • Standards Compliant: Follows the JSON specification
  • Zero Dependencies: Uses only MoonBit standard library

#Usage

#Basic Example

///|
test "basic usage" {
let json = "{\"name\": \"Alice\", \"age\": 30}"
let result = @tonyfettes/amjson.parse(json)
match result {
Object(map) => {
inspect(map.get("name"), content="Some(String(\"Alice\"))")
inspect(map.get("age"), content="Some(Number(30))")
}
_ => fail("Expected Object")
}
}

#Parsing Different Value Types

///|
test "parse different types" {
// Null
let null_val = @tonyfettes/amjson.parse("null")
inspect(null_val, content="Null")

// Boolean
let bool_val = @tonyfettes/amjson.parse("true")
inspect(bool_val, content="Bool(true)")

// Number
let num_val = @tonyfettes/amjson.parse("42.5")
inspect(num_val, content="Number(42.5)")

// String
let str_val = @tonyfettes/amjson.parse("\"hello\"")
inspect(str_val, content="String(\"hello\")")

// Array
let arr_val = @tonyfettes/amjson.parse("[1, 2, 3]")
inspect(arr_val, content="Array([Number(1), Number(2), Number(3)])")
}

#Error Handling

///|
test "error handling" {
let invalid_json = "{invalid}"
let result = try! @tonyfettes/amjson.parse(invalid_json)
match result {
Err(UnexpectedChar(_pos, ch)) => inspect(ch, content="i")
_ => fail("Expected parse error")
}
}

#Complex Nested Structures

///|
test "nested structures" {
let json =
#|{
#| "users": [
#| {"name": "Bob", "score": 95.5},
#| {"name": "Carol", "score": 87.0}
#| ],
#| "count": 2
#|}
#|
let result = @tonyfettes/amjson.parse(json)
match result {
Object(map) => {
match map.get("users") {
Some(Array(users)) => inspect(users.length(), content="2")
_ => fail("Expected users array")
}
inspect(map.get("count"), content="Some(Number(2))")
}
_ => fail("Expected Object")
}
}

#API

#parse(input: String) -> JsonValue raise ParseError

Parses a JSON string and returns a JsonValue.

Parameters:
  • input: A string containing valid JSON

Returns:
  • A JsonValue representing the parsed JSON

Raises:
  • ParseError with position information if parsing fails

#JsonValue Type

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

#ParseError Type

///|
pub(all) suberror ParseError {
UnexpectedChar(Position, Char)
UnexpectedEof(Position)
InvalidNumber(Position, String)
InvalidEscape(Position)
InvalidUnicodeEscape(Position)
}

#String Escape Sequences

The parser supports all standard JSON escape sequences:

  • \" - Double quote
  • \\ - Backslash
  • \/ - Forward slash
  • \b - Backspace
  • \f - Form feed
  • \n - Newline
  • \r - Carriage return
  • \t - Tab
  • \uXXXX - Unicode escape (4 hex digits)

#Number Format

Supports:
  • Integers: 42, -123
  • Floats: 3.14, -2.5
  • Scientific notation: 1e10, 1.5e-3

#License

This project is licensed under the Apache License 2.0.

#
ParseError

pub(all) suberror ParseError {
UnexpectedChar(Position, Char)
UnexpectedEof(Position)
InvalidNumber(Position, String)
InvalidEscape(Position)
InvalidUnicodeEscape(Position)
}

Parse errors with position information
impl Eq for ParseError
impl Show for ParseError

#
JsonValue

pub 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

#
Lexer

type Lexer

Lexer state

#
Position

type Position

Position in the input for error reporting
impl Eq for Position
impl Show for Position

#
parse

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

Parse a JSON value

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io