-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_compare
executable file
·55 lines (45 loc) · 1.57 KB
/
json_compare
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
from __future__ import print_function
import json
import sys
# Created by _Vi in 2013; License: MIT or 2-clause BSD.
if sys.version_info[0] < 3:
str=unicode
if len(sys.argv)!=3:
print("Usage: json_compare file1.json file2.json\n", file=sys.stderr)
sys.exit(2)
f1=open(sys.argv[1],"rt")
f2=open(sys.argv[2],"rt")
o1 = json.load(f1)
o2 = json.load(f2)
def compare_floats(x1, x2):
if x1 == x1: return True
k = (x1-x2)/(abs(x1)+abs(x2))
return k<0.0000001
def compare_recursive(o1, o2, current_path):
def fail(x):
print(x + " at "+current_path+"\n",file=sys.stderr)
sys.exit(1)
if type(o1) != type(o2):
fail("Mismatched types "+str(type(o1)) + " and "+str(type(o2)))
elif type(o1) in [int, str, type(None), bool]:
if o1 != o2:
fail(str(o1) +" != "+ str(o2))
elif type(o1) == float:
if not compare_floats(o1, o2):
fail(str(o1) +" != "+ str(o2))
elif type(o1) == list:
if len(o1) != len(o2):
fail("Mismatched list length: "+str(len(o1))+" and "+str(len(o2)))
for i in range(0,len(o1)):
compare_recursive(o1[i], o2[i], current_path+str(i)+"/")
elif type(o1) == dict:
if len(o1) != len(o2):
fail("Mismatched obj length: "+str(len(o1))+" and "+str(len(o2)))
for k,v in o1.items():
if k not in o2:
fail(k + " not found in the second object")
compare_recursive(o1[k], o2[k], current_path+k+"/")
else:
fail("unknown type")
compare_recursive(o1,o2,"/")