jsonEquals static method

bool jsonEquals(
  1. Object? left,
  2. Object? right,
  3. DiagnosticResourceLimits resourceLimits
)

Structural equality over JSON-shaped values, bounded by DiagnosticResourceLimits.maxTraversalNodes.

Implementation

static bool jsonEquals(
  Object? left,
  Object? right,
  DiagnosticResourceLimits resourceLimits,
) {
  final pending = <(Object?, Object?)>[(left, right)];
  var inspected = 0;
  while (pending.isNotEmpty && inspected < resourceLimits.maxTraversalNodes) {
    final (currentLeft, currentRight) = pending.removeLast();
    inspected++;
    if (identical(currentLeft, currentRight)) continue;
    if (currentLeft == null ||
        currentRight == null ||
        currentLeft.runtimeType != currentRight.runtimeType) {
      return false;
    }
    if (currentLeft is String || currentLeft is num || currentLeft is bool) {
      if (currentLeft != currentRight) return false;
    } else if (currentLeft is Map<String, Object?> &&
        currentRight is Map<String, Object?>) {
      if (currentLeft.length != currentRight.length) return false;
      for (final entry in currentLeft.entries) {
        if (!currentRight.containsKey(entry.key)) return false;
        pending.add((entry.value, currentRight[entry.key]));
      }
    } else if (currentLeft is List<Object?> &&
        currentRight is List<Object?>) {
      if (currentLeft.length != currentRight.length) return false;
      for (var index = 0; index < currentLeft.length; index++) {
        pending.add((currentLeft[index], currentRight[index]));
      }
    } else {
      return false;
    }
  }
  return pending.isEmpty;
}