Compare parsed value with builtin parser
diff --git a/transcode-to-json.py b/transcode-to-json.py
index bb674b4..2515578 100755
--- a/transcode-to-json.py
+++ b/transcode-to-json.py
@@ -1,12 +1,51 @@
#!/usr/bin/env python
from argparse import ArgumentParser
+from collections.abc import Mapping, Sequence
+from itertools import zip_longest
+from json import loads
from logging import basicConfig, DEBUG, getLogger
+from math import isnan
from pathlib import Path
from pyjson5 import decode, encode
+def eq_with_nans(left, right):
+ if left == right:
+ return True
+ elif isnan(left):
+ return isnan(right)
+ elif isnan(right):
+ return False
+
+ if not isinstance(left, Sequence) or not isinstance(right, Sequence):
+ return False
+ elif len(left) != len(right):
+ return False
+
+ left_mapping = isinstance(left, Mapping)
+ right_mapping = isinstance(right, Mapping)
+ if left_mapping != right_mapping:
+ return False
+
+ sentinel = object()
+ if left_mapping:
+ for k, left_value in left.items():
+ right_value = right.pop(k, sentinel)
+ if not eq_with_nans(left_value, right_value):
+ return False
+ if right:
+ # extraneous keys
+ return False
+ else:
+ for l, r in zip_longest(left, right, fillvalue=sentinel):
+ if not eq_with_nans(l, r):
+ return False
+
+ return True
+
+
argparser = ArgumentParser(description='Run JSON5 parser tests')
argparser.add_argument('input', type=Path)
argparser.add_argument('output', nargs='?', type=Path)
@@ -31,6 +70,15 @@
raise SystemExit(1)
try:
+ json_obj = loads(data)
+ except Exception:
+ pass
+ else:
+ if not eq_with_nans(obj, json_obj):
+ logger.error('JSON and PyJSON5 did not read the same data: %s, %r != %r', args.input, obj, json_obj)
+ raise SystemExit(2)
+
+ try:
data = encode(obj)
except Exception:
logger.error('Could open stringify content: %s', args.input, exc_info=True)