Class JsonPatch

java.lang.Object
cloud.opencode.base.json.patch.JsonPatch

public final class JsonPatch extends Object
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:
  • Method Details

    • builder

      public static JsonPatch.Builder builder()
      Creates a new builder. 创建新的构建器。
      Returns:
      a new builder - 新构建器
    • of

      public static JsonPatch of(List<JsonPatch.PatchOperation> operations)
      Creates a patch from a list of operations. 从操作列表创建补丁。
      Parameters:
      operations - the operations - 操作
      Returns:
      the patch - 补丁
    • apply

      public JsonNode apply(JsonNode target)
      Applies this patch to a JSON document. 将此补丁应用于 JSON 文档。
      Parameters:
      target - the target document - 目标文档
      Returns:
      the patched document - 打补丁后的文档
      Throws:
      OpenJsonProcessingException - if patch fails - 如果补丁失败
    • validate

      public boolean validate(JsonNode target)
      Validates this patch against a target without applying. 验证此补丁对目标的有效性但不应用。
      Parameters:
      target - the target document - 目标文档
      Returns:
      true if patch would succeed - 如果补丁会成功则返回 true
    • getOperations

      public List<JsonPatch.PatchOperation> getOperations()
      Returns the operations in this patch. 返回此补丁中的操作。
      Returns:
      the operations - 操作
    • size

      public int size()
      Returns the number of operations. 返回操作数量。
      Returns:
      the operation count - 操作数量
    • toJson

      public JsonNode toJson()
      Serializes this patch as an RFC 6902 JSON document (an array of operation objects, each with op / path / optional from / optional value fields). 将此补丁序列化为 RFC 6902 JSON 文档(操作对象数组,每个含 op / path / 可选 from / 可选 value 字段)。

      Inverse of constructing a JsonPatch from a parsed RFC 6902 document. Use this when you need to transmit / persist / log a patch as a standard JSON document, e.g. as an HTTP request body (Content-Type: application/json-patch+json).

      构造 JsonPatch(从解析后的 RFC 6902 文档)的反向操作。 当需要将补丁作为标准 JSON 文档传输 / 持久化 / 记录日志时使用, 例如作为 HTTP 请求体(Content-Type: application/json-patch+json)。

      Returns:
      the RFC 6902 JSON Patch document (an JsonNode.ArrayNode) - RFC 6902 JSON Patch 文档(JsonNode.ArrayNode
      Since:
      opencode-base-json V1.0.4