Class JsonPatch
java.lang.Object
cloud.opencode.base.json.patch.JsonPatch
JSON Patch - RFC 6902 Implementation
JSON Patch - RFC 6902 实现
This class implements JSON Patch (RFC 6902), which defines a format for expressing a sequence of operations to apply to a JSON document.
此类实现 JSON Patch(RFC 6902),定义了一种格式, 用于表示应用于 JSON 文档的操作序列。
Operations | 操作:
add- Add a value - 添加值remove- Remove a value - 移除值replace- Replace a value - 替换值move- Move a value - 移动值copy- Copy a value - 复制值test- Test a value - 测试值
Example | 示例:
JsonNode target = OpenJson.parse("{\"name\":\"John\",\"age\":30}");
JsonPatch patch = JsonPatch.builder()
.replace("/name", JsonNode.of("Jane"))
.add("/email", JsonNode.of("jane@example.com"))
.remove("/age")
.build();
JsonNode result = patch.apply(target);
// Result: {"name":"Jane","email":"jane@example.com"}
Features | 主要功能:
- RFC 6902 JSON Patch implementation - RFC 6902 JSON Patch实现
- Six patch operations: add, remove, replace, move, copy, test - 六种补丁操作
- Builder pattern for constructing patch sequences - 构建补丁序列的构建器模式
Security | 安全性:
- Thread-safe: Yes (immutable) - 线程安全: 是(不可变)
- Null-safe: N/A - 空值安全: 不适用
- Since:
- JDK 25, opencode-base-json V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for JsonPatch.static enumPatch operation type 补丁操作类型static final recordSingle patch operation 单个补丁操作 -
Method Summary
Modifier and TypeMethodDescriptionApplies this patch to a JSON document.static JsonPatch.Builderbuilder()Creates a new builder.Returns the operations in this patch.static JsonPatchof(List<JsonPatch.PatchOperation> operations) Creates a patch from a list of operations.intsize()Returns the number of operations.booleanValidates this patch against a target without applying.
-
Method Details
-
builder
Creates a new builder. 创建新的构建器。- Returns:
- a new builder - 新构建器
-
of
Creates a patch from a list of operations. 从操作列表创建补丁。- Parameters:
operations- the operations - 操作- Returns:
- the patch - 补丁
-
apply
Applies this patch to a JSON document. 将此补丁应用于 JSON 文档。- Parameters:
target- the target document - 目标文档- Returns:
- the patched document - 打补丁后的文档
- Throws:
OpenJsonProcessingException- if patch fails - 如果补丁失败
-
validate
Validates this patch against a target without applying. 验证此补丁对目标的有效性但不应用。- Parameters:
target- the target document - 目标文档- Returns:
- true if patch would succeed - 如果补丁会成功则返回 true
-
getOperations
Returns the operations in this patch. 返回此补丁中的操作。- Returns:
- the operations - 操作
-
size
public int size()Returns the number of operations. 返回操作数量。- Returns:
- the operation count - 操作数量
-