Class JsonDiff

java.lang.Object
cloud.opencode.base.json.diff.JsonDiff

public final class JsonDiff extends Object
JSON Diff - JSON Document Comparison Tool JSON Diff - JSON 文档比较工具

This class compares two JSON documents and generates a detailed difference report. It can also produce JSON Patch (RFC 6902) output.

此类比较两个 JSON 文档并生成详细的差异报告。 它还可以生成 JSON Patch(RFC 6902)输出。

Example | 示例:

JsonNode source = OpenJson.parse("{\"name\":\"John\",\"age\":30}");
JsonNode target = OpenJson.parse("{\"name\":\"Jane\",\"email\":\"jane@example.com\"}");

DiffResult result = JsonDiff.diff(source, target);

// Iterate differences
for (Difference diff : result.getDifferences()) {
    System.out.println(diff.getType() + " at " + diff.getPath());
}

// Generate JSON Patch
JsonPatch patch = result.toPatch();

Features | 主要功能:

  • Deep comparison of JSON documents - JSON文档的深度比较
  • Categorized differences (added, removed, changed, type changed) - 分类差异
  • Automatic JSON Patch (RFC 6902) generation from diff - 从差异自动生成JSON Patch

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

    • diff

      public static JsonDiff.DiffResult diff(JsonNode source, JsonNode target)
    • equals

      public static boolean equals(JsonNode source, JsonNode target)
      Checks if two documents are equal. 检查两个文档是否相等。
      Parameters:
      source - the source document - 源文档
      target - the target document - 目标文档
      Returns:
      true if equal - 如果相等则返回 true
    • diffAsJson

      public static JsonNode diffAsJson(JsonNode source, JsonNode target)
      Computes the diff and serializes it directly as an RFC 6902 JSON Patch document (a JSON array of operation objects). 计算差异并直接序列化为 RFC 6902 JSON Patch 文档(操作对象的 JSON 数组)。

      Convenience for diff(source, target).toPatch().toJson(). Use when you need the standard JSON-Patch wire format directly without first inspecting the rich JsonDiff.DiffResult record (HTTP transport, persistence, logging, etc.).

      diff(source, target).toPatch().toJson() 的便捷方法。当你需要直接得到 标准 JSON-Patch 线上格式而无需先检查丰富的 JsonDiff.DiffResult 记录时使用 (HTTP 传输、持久化、日志等)。

      Parameters:
      source - the source document - 源文档
      target - the target document - 目标文档
      Returns:
      the RFC 6902 JSON Patch document - RFC 6902 JSON Patch 文档
      Since:
      opencode-base-json V1.0.4