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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
#!/usr/bin/rhino -debug
var jsonBoxA, jsonBoxB;
// compareTree(objA, objB, "root", results);
function JSONDiff(fn1, fn2) {
this.obj1 = JSON.parse(readFile(fn1));
this.obj2 = JSON.parse(readFile(fn2));
}
/**
* Compare two objects recursively
*
*
* For example, comparing object A
*
* { "a": 1,
* "b": 2,
* "son" : {
* "name": "Janošek"
* }
* }
*
* and
*
* { "a": 2,
* "c": 3
* "daughter" : {
* "name": "Maruška"
* }
* }
*
* we get
*
* {
* "insert": [
* { "c": 3 },
* {
* "daughter" : {
* "name": "Maruška"
* }
* }
* ],
* "delete": [
* { "b": 2 },
* {
* "son" : {
* "name": "Janošek"
* }
* }
* ],
* "update": { "a": 2 }
* ]
* }
*/
JSONDiff.prototype.compareTree = function compareTree(a, b, name) {
function typeofReal(value) {
return Array.isArray(value) ? "array" : typeof value;
}
function isScalar(value) {
var typeStr = typeofReal(value);
return !((typeStr == "array") || (typeStr == "object"));
}
var equal = false;
var elements = {};
for (var key in a) {
if a.hasOwnProperty(key) {
elements[key] = null;
}
}
for (var key in b) {
if b.hasOwnProperty(key) {
elements[key] = null;
}
}
// print("compareTree: name = " + name);
var typeA = typeofReal(a);
var typeB = typeofReal(b);
if (typeA !== typeB) {
// There is not much to be done when the objects are not of
// the same type
return {
'deleted': a,
'inserted': b
}
}
// Now we have both objects of the same type, so
// we can evaluate just type of one
// If it is array ...
if (typeA === "array") {
var results = {
'updated': {}
};
var maxLen = a.length > b.length ? a.length : b.length;
for (var i = 0; i < maxLen; i++) {
if (isScalar(a[i]) && isScalar(b[i])) {
if (a[i] !== b[i]) {
results['updated'][i] = b[i];
}
}
}
}
if (typeA === "object") {
}
/*
two trees are equal when:
- they have same keys,
- properties of the same keys have same values
====
if keys are not same, then whole subobject ==> ADDED/DELETED
if property values are not same && value is scalar, ==> UPDATED
if trees are not same, go one level down and compare two siblings
*/
if (a === undefined) {
this.results['inserted'].push(b);
}
else if (b === undefined) {
this.results['deleted'].push(a);
}
else if (typeA !== typeB || (typeA !== "object" && typeA !== "array" && a !== b)) {
this.results['updated'].push(b);
}
if (typeA === "object" || typeA === "array" || typeB === "object" || typeB === "array") {
var keys = [];
for (var i in a) {
if (a.hasOwnProperty(i)) {
keys.push(i);
}
}
for (var i in b) {
if (b.hasOwnProperty(i)) {
keys.push(i);
}
}
keys.sort();
for (var i = 0; i < keys.length; i++) {
if (keys[i] === keys[i-1]) {
continue;
}
this.compareTree(a && a[keys[i]], b && b[keys[i]], keys[i]);
}
}
};
JSONDiff.prototype.diff = function diff() {
this.compareTree(this.obj1, this.obj2, "root");
return this.results;
};
if (arguments.length == 2) {
var diffObj = new JSONDiff(arguments[0], arguments[1]);
// print(diffObj);
var diff = diffObj.diff();
print (JSON.stringify(diff));
}
/* vim: set ts=2 et sw=2 tw=80: */
|