Class JsonEquals

java.lang.Object
cloud.opencode.base.json.util.JsonEquals

public final class JsonEquals extends Object
JSON Equals - Structural JSON Equality Comparison Utility. JSON 相等性比较 - 结构化 JSON 相等性比较工具。

Provides deep structural equality comparison between JsonNode instances. Objects are compared ignoring key order, while arrays respect element order by default. An optional mode allows ignoring array order for set-like comparisons.

提供 JsonNode 实例之间的深度结构相等性比较。 对象比较忽略键顺序,数组默认按元素顺序比较。 可选模式允许忽略数组顺序,用于集合类比较。

Features | 主要功能:

  • Structural equality: objects ignore key order - 结构相等性:对象忽略键顺序
  • Array order sensitive by default, with option to ignore - 数组默认顺序敏感,可选择忽略
  • Number comparison by numeric value (1 == 1.0) - 数字按数值比较(1 == 1.0)
  • String convenience methods for direct JSON string comparison - 字符串便利方法直接比较 JSON 字符串
  • Depth guard against deeply nested structures - 深度保护防止深度嵌套结构

Usage Examples | 使用示例:

JsonNode a = JsonNode.object().put("x", 1).put("y", 2);
JsonNode b = JsonNode.object().put("y", 2).put("x", 1);
boolean eq = JsonEquals.equals(a, b); // true

// Compare JSON strings directly
boolean eq2 = JsonEquals.equals("{\"a\":1}", "{\"a\":1.0}"); // true

// Ignore array order
JsonNode arr1 = JsonNode.array().add(1).add(2).add(3);
JsonNode arr2 = JsonNode.array().add(3).add(2).add(1);
boolean eq3 = JsonEquals.equalsIgnoreArrayOrder(arr1, arr2); // true

Performance | 性能:

  • O(n) for ordered comparison, O(n^2) worst case for unordered array comparison - 有序比较 O(n),无序数组比较最坏 O(n^2)
  • Stateless and thread-safe - 无状态且线程安全

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Depth-limited to prevent stack overflow attacks - 深度限制以防止栈溢出攻击
Since:
JDK 25, opencode-base-json V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • equals

      public static boolean equals(JsonNode a, JsonNode b)
      Compares two JSON nodes for structural equality. 比较两个 JSON 节点的结构相等性。

      Objects are compared ignoring key order. Arrays respect element order. Numbers are compared by numeric value (e.g. 1 equals 1.0).

      对象比较忽略键顺序。数组按元素顺序比较。数字按数值比较(如 1 等于 1.0)。

      Parameters:
      a - the first JSON node - 第一个 JSON 节点
      b - the second JSON node - 第二个 JSON 节点
      Returns:
      true if structurally equal - 如果结构相等则返回 true
    • equals

      public static boolean equals(String jsonA, String jsonB)
      Compares two JSON strings for structural equality. 比较两个 JSON 字符串的结构相等性。

      Parses both strings using OpenJson.parse(String) and then performs structural comparison.

      使用 OpenJson.parse(String) 解析两个字符串,然后进行结构比较。

      Parameters:
      jsonA - the first JSON string - 第一个 JSON 字符串
      jsonB - the second JSON string - 第二个 JSON 字符串
      Returns:
      true if structurally equal - 如果结构相等则返回 true
      Throws:
      OpenJsonProcessingException - if either string cannot be parsed - 如果任一字符串无法解析
      NullPointerException - if either argument is null - 如果任一参数为 null
    • equalsIgnoreArrayOrder

      public static boolean equalsIgnoreArrayOrder(JsonNode a, JsonNode b)
      Compares two JSON nodes for structural equality, ignoring array element order. 比较两个 JSON 节点的结构相等性,忽略数组元素顺序。

      This is useful for set-like comparisons where the order of array elements does not matter.

      适用于集合类比较,数组元素的顺序无关紧要。

      Parameters:
      a - the first JSON node - 第一个 JSON 节点
      b - the second JSON node - 第二个 JSON 节点
      Returns:
      true if structurally equal ignoring array order - 如果忽略数组顺序后结构相等则返回 true