aboutsummaryrefslogtreecommitdiffstats
path: root/json_diff.js
blob: 07068cb8d787735fba077e228e6de99d498fdedf (plain) (blame)
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
/*
This is for now just reimplementation of tlrobinson's json-diff (http://tlrobinson.net/projects/javascript-fun/jsondiff/).

However, later I would like to incorporate algorightm from
https://github.com/cmsd2/xdiff (which is an implementation in Ruby of
http://pages.cs.wisc.edu/~yuanwang/xdiff.html)
 */

var jsonBoxA, jsonBoxB;

//var HashStore = {
//  load : function(callback) {
//    if (window.location.hash) {
//      try {
//        var hashObject = JSON.parse(decodeURIComponent(window.location.hash.slice(1)));
//        callback && callback(hashObject.d);
//        return;
//      } catch (e) {
//        console.log()
//      }
//    }
//    callback && callback(null);
//  },
//  sync : function(object) {
//    var hashObject = { d : object };
//    window.location.hash = "#" + encodeURIComponent(JSON.stringify(hashObject));
//  }
//};

//function init() {
//  document.addEventListener("click", clickHandler, false);

//  jsonBoxA = document.getElementById("jsonA");
//  jsonBoxB = document.getElementById("jsonB");

//  HashStore.load(function(data) {
//    if (data) {
//      jsonBoxA.value = data.a;
//      jsonBoxB.value = data.b;
//    }
//  });

//  startCompare();
//}

//function startCompare() {
//  var aValue = jsonBoxA.value;
//  var bValue = jsonBoxB.value;

//  var objA, objB;
//  try {
//    objA = JSON.parse(aValue);
//    jsonBoxA.style.backgroundColor = "";
//  } catch(e) {
//    jsonBoxA.style.backgroundColor = "rgba(255,0,0,0.5)";
//  }
//  try {
//    objB = JSON.parse(bValue);
//    jsonBoxB.style.backgroundColor = "";
//  } catch(e) {
//    jsonBoxB.style.backgroundColor = "rgba(255,0,0,0.5)";
//  }

//  HashStore.sync({
//    a : aValue,
//    b : bValue
//  });

//  results = document.getElementById("results");
//  removeAllChildren(results);

//  compareTree(objA, objB, "root", results);
//}

function compareTree(a, b, name, results) {
  var typeA = typeofReal(a);
  var typeB = typeofReal(b);

  var typeSpanA = document.createElement("span");
  typeSpanA.appendChild(document.createTextNode("("+typeA+")"))
  typeSpanA.setAttribute("class", "typeName");

  var typeSpanB = document.createElement("span");
  typeSpanB.appendChild(document.createTextNode("("+typeB+")"))
  typeSpanB.setAttribute("class", "typeName");

  var aString = (typeA === "object" || typeA === "array") ? "" : String(a) + " ";
  var bString = (typeB === "object" || typeB === "array") ? "" : String(b) + " ";

  var leafNode = document.createElement("span");
  leafNode.appendChild(document.createTextNode(name));
  if (a === undefined) {
    leafNode.setAttribute("class", "added");
    leafNode.appendChild(document.createTextNode(": " + bString));
    leafNode.appendChild(typeSpanB);
  }
  else if (b === undefined) {
    leafNode.setAttribute("class", "removed");
    leafNode.appendChild(document.createTextNode(": " + aString));
    leafNode.appendChild(typeSpanA);
  }
  else if (typeA !== typeB || (typeA !== "object" && typeA !== "array" && a !== b)) {
    leafNode.setAttribute("class", "changed");
    leafNode.appendChild(document.createTextNode(": " + aString));
    leafNode.appendChild(typeSpanA);
    leafNode.appendChild(document.createTextNode(" => "+ bString));
    leafNode.appendChild(typeSpanB);
  }
  else {
    leafNode.appendChild(document.createTextNode(": " + aString));
    leafNode.appendChild(typeSpanA);
  }

  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();

    var listNode = document.createElement("ul");
    listNode.appendChild(leafNode);

    for (var i = 0; i < keys.length; i++) {
      if (keys[i] === keys[i-1]) {
        continue;
      }
      var li = document.createElement("li");
      listNode.appendChild(li);

      compareTree(a && a[keys[i]], b && b[keys[i]], keys[i], li);
    }
    results.appendChild(listNode);
  }
  else {
    results.appendChild(leafNode);
  }
}

function removeAllChildren(node) {
  var child;
  while (child = node.lastChild) {
    node.removeChild(child);
  }
}

function typeofReal(value) {
  return Array.isArray(value) ? "array" : typeof value;
}

function clickHandler(e) {
  var e = e || window.event;
  if (e.target.nodeName.toUpperCase() === "UL") {
    if (e.target.getAttribute("closed") === "yes")
      e.target.setAttribute("closed", "no");
    else
      e.target.setAttribute("closed", "yes");
  }
}

/* vim: set ts=2 et sw=2 tw=80: */