Source-preserving JSON/JSONC editing, tolerant syntax trees, formatting and transactional configuration changes in MoonBit
{
// Keep the team convention documented.
"scripts": {"test": "moon test"},
"buildId": 9007199254740993
}moon add YeeHh2004/moonjsonc@0.1.0import {
"YeeHh2004/moonjsonc" @jsonc,
}fn main {
let source = "{/* keep */\"enabled\":false,\"id\":9007199254740993}"
let edits = @jsonc.modify(source, [Key("enabled")], Some("true")).unwrap()
println(@jsonc.apply_edits(source, edits).unwrap())
}{/* keep */"enabled":true,"id":9007199254740993}| Area | APIs | Useful for |
|---|---|---|
| Syntax | scan, parse, node_value | Tokens, JSONC options, tolerant trees, bounded diagnostics |
| Navigation | find_node, node_at_offset, get_location, to_pointer, visit | Editor settings, structured inspection, source events |
| Presentation | format, strip_comments | Document/range formatting, comment removal with offsets intact |
| Source updates | modify, insert, apply_edits | Create, replace, remove and insert without rewriting unrelated source |
| Coherent batches | transact | Snapshot checks, Test preconditions, all-or-error multi-step changes |
git clone https://github.com/YeeHh2004/moonjsonc.git
cd moonjsonc
moon run examples/basic --target js
moon run examples/editor --target wasm-gc
moon run examples/transaction --target jsnpm ci --ignore-scripts
npm run build
node cli/moonjsonc.mjs --help
node cli/moonjsonc.mjs check config.jsonc
node cli/moonjsonc.mjs set config.jsonc '["service","replicas"]' '4'
node cli/moonjsonc.mjs format config.jsonc --check
npm run serve[
{"op":"test","path":["version"],"value":"3"},
{"op":"set","path":["service","replicas"],"value":"4"},
{"op":"insert","path":["service","regions"],"index":1,"value":"\"west\""},
{"op":"set","path":["version"],"value":"4"}
]node cli/moonjsonc.mjs transaction deployment.jsonc operations.jsonpub(all) enum EditError {
InvalidDocument(Array[Diagnostic])
InvalidValue(Array[Diagnostic])
InvalidPath(String)
InvalidEdit(String)
Conflict(String)
InvalidOptions(String)
} derive(Eq, Debug)pub(all) struct Event {
kind : String
offset : Int
length : Int
path : Array[PathSegment]
value : String
} derive(Eq, Debug)pub(all) struct Location {
path : Array[PathSegment]
is_property_key : Bool
offset : Int
line : Int
column : Int
} derive(Eq, Debug)pub(all) enum Operation {
Set(Array[PathSegment], String)
Remove(Array[PathSegment])
Insert(Array[PathSegment], Int, String)
Test(Array[PathSegment], String)
} derive(Eq, Debug)fn insert(text : String, path : Array[PathSegment], index : Int, value : String, options? : FormatOptions) -> Result[Array[TextEdit], EditError]fn modify(text : String, path : Array[PathSegment], value : String?, options? : FormatOptions) -> Result[Array[TextEdit], EditError]fn strip_comments(text : String, replace_with_spaces? : Bool) -> Stringfn transact(text : String, operations : Array[Operation], options? : FormatOptions, expected_source? : String?) -> Result[Transaction, EditError]Install
Download zipSource-preserving JSON/JSONC editing, tolerant syntax trees, formatting and transactional configuration changes in MoonBit