Apply JSON Patch (RFC 6902) 操作
JSON Patch defines 序列的 操作 (添加, 移除, 替换, move, 副本, 测试) 到修改 JSON document atomically.
- 1
JSON Patch (RFC 6902) applies 数组的 操作到 JSON document. 操作: 添加 (插入或 替换), 移除 (delete), 替换 (overwrite), move (relocate), 副本 (重复), 测试 (assert 值之前 proceeding). 每个操作我们 JSON Pointer 路径.
- 2 JSON 格式化器 →
格式化 both the original document and the patch array to verify they are valid JSON. The patch is an array of {op, path, value?} objects. Format them to catch syntax errors before applying.
- 3 JSON 验证器 →
验证 the patch structure: each operation must have "op" (one of add/remove/replace/move/copy/test) and "path" (a JSON Pointer). Add and replace require "value"; move and copy require "from"; test requires "value" for comparison.
- 4
"测试" 操作是 powerful 为 conditional patches: 它 verifies 值之前 applying subsequent 操作. 如果任何测试 fails, 整个修补是 已拒绝 (atomicity). Use 测试到 implement optimistic 并发: 测试 version 字段, 然后应用 changes.
常见问题
- 是 JSON Patch atomic?
- 是. 如果任何操作 fails (e.g., 测试断言 fails, 路径不 存在为 移除), 整个修补是 已拒绝和 document 是 unchanged. 此 makes 它安全为 concurrent modifications 具有 proper version testing.
- JSON Patch vs JSON Merge Patch?
- JSON Patch (RFC 6902) 我们 explicit 操作 (添加/移除/替换) — 更多 expressive. JSON Merge Patch (RFC 7396) 是 simpler: 单个 JSON object where 空 means delete 和其他 values mean 替换. Use Merge Patch 为简单 updates, JSON Patch 为复杂操作.