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