RFC 6902 JSON Patch (apply + diff) and RFC 7386 JSON Merge Patch for MoonBit, with RFC 6901 JSON Pointer support and precise error reporting.
Dependencies
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 删除成员、对象递归合并// 删除中间项并在头部插入新项:
@jsonpatch.diff_keyed(old, new, "id")
// -> [{"op":"remove","path":"/list/1"},{"op":"add","path":"/list/0","value":{"id":0}}]
// 位置 diff 则会输出连锁 replace(把 id:1 改成 id:0、id:2 改成 id:1)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 -- diff '{"list":[{"id":1},{"id":2},{"id":3}]}' \
'{"list":[{"id":0},{"id":1},{"id":3}]}' --key id
[{"op":"remove","path":"/list/1"},{"op":"add","path":"/list/0","value":{"id":0}}]
$ moon run cmd/patch -- merge '{"a":{"x":1,"y":2}}' '{"a":{"y":null,"z":3}}'
{"a":{"x":1,"z":3}}$ moon run cmd/patch -- apply @doc.json @patch.json
$ cat patch.json | moon run --target js cmd/patch -- apply @doc.json -| 场景 | n=1,000 | n=10,000 | n=100,000 |
|---|---|---|---|
| apply 对象(100 操作) | 4.2 ms | 216 ms | 2.9 s |
| apply 数组(100 操作) | 14 µs | 1.7 ms | 137 ms |
| diff 对象(~15% 变更) | 6.7 µs | 0.9 ms | 129 ms |
| diff 数组(~20% 变更) | 1.0 µs | 124 µs | 11.8 ms |
| merge 嵌套对象 | 0.1 µs | 13 µs | 2.0 ms |
moon check # static checks
moon fmt # format
moon test # unit tests + official conformance suite
moon run examples/basic # runnable example (apply / diff / merge / errors)
moon run cmd/patch -- # run the CLI
moon run --release benches # benchmarks (apply / diff / merge)
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)fn diff_text_keyed(original_text : String, modified_text : String, key : String) -> Result[String, TextJsonError]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.
Dependencies