RFC 6902 JSON Patch (apply + diff) and RFC 7386 JSON Merge Patch for MoonBit, with RFC 6901 JSON Pointer support and precise error reporting.
moon add Xu107-hhh/moonbit-jsonpatch| 指标 | 结果 |
|---|---|
| spec_tests.json(RFC 6902 正文附录用例) | 16 / 16 通过(1 例上游标记 disabled 未纳入) |
| tests.json(社区补充用例) | 92 / 92 通过(3 例上游标记 disabled 未纳入) |
| 合计 | 108 / 108(100%) |
let doc = @json.parse("{\"a\":1,\"list\":[1,2]}")
let patch = @json.parse("[{\"op\":\"add\",\"path\":\"/list/-\",\"value\":3}]")
match @jsonpatch.apply_patch(doc, patch) {
Ok(result) => // {"a":1,"list":[1,2,3]}
Err(e) => // e.index / e.op / e.path / e.kind / e.message
}Err(@jsonpatch.PatchError::{ index: 1, op: "add", path: "/x/9", ... })
// message: "array index is malformed or beyond the end of the array"@jsonpatch.diff(old, new) // -> RFC 6902 patch (Json)
@jsonpatch.merge_patch(target, p) // RFC 7386,null 删除成员、对象递归合并match @jsonpatch.apply(doc_text, patch_text) { ... }$ moon run cmd/patch -- apply '{"a":1,"list":[1,2]}' \
'[{"op":"add","path":"/list/-","value":3},{"op":"replace","path":"/a","value":9}]'
{"a":9,"list":[1,2,3]}
$ moon run cmd/patch -- diff '{"a":1,"b":2}' '{"a":2,"c":3}'
[{"op":"remove","path":"/b"},{"op":"replace","path":"/a","value":2},{"op":"add","path":"/c","value":3}]
$ moon run cmd/patch -- merge '{"a":{"x":1,"y":2}}' '{"a":{"y":null,"z":3}}'
{"a":{"x":1,"z":3}}moon check # static checks
moon fmt # format
moon test # unit tests + official conformance suite
moon run cmd/patch -- # run the CLI
python tools/gen_suite.py # regenerate suite tests from fixturespub enum JsonPatchError {
BadDocumentJson(String)
BadPatchJson(String)
PatchFailed(PatchError)
} derive(Eq, Debug)pub(all) struct PatchError {
index : Int
op : String
path : String
kind : PatchErrorKind
message : String
} derive(Eq, Debug)Install
Download zipRFC 6902 JSON Patch (apply + diff) and RFC 7386 JSON Merge Patch for MoonBit, with RFC 6901 JSON Pointer support and precise error reporting.