2019-01-22 03:42:43 +08:00
|
|
|
<!DOCTYPE HTML>
|
2020-09-23 16:23:38 +08:00
|
|
|
<html lang="en">
|
2019-01-22 03:42:43 +08:00
|
|
|
<head>
|
2020-09-23 16:23:38 +08:00
|
|
|
<meta charset="utf-8">
|
2019-01-22 03:42:43 +08:00
|
|
|
|
|
|
|
<link href="../dist/jsoneditor.css" rel="stylesheet" type="text/css">
|
|
|
|
<script src="../dist/jsoneditor.js"></script>
|
|
|
|
|
|
|
|
<style type="text/css">
|
|
|
|
body {
|
|
|
|
font: 10.5pt arial;
|
|
|
|
color: #4d4d4d;
|
|
|
|
line-height: 150%;
|
2019-01-22 03:53:02 +08:00
|
|
|
width: 100%;
|
|
|
|
padding-left: 10px;
|
2019-01-22 03:42:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
code {
|
|
|
|
background-color: #f5f5f5;
|
|
|
|
}
|
|
|
|
|
|
|
|
#containerLeft {
|
2019-01-22 03:53:02 +08:00
|
|
|
display: inline-block;
|
2019-01-22 03:42:43 +08:00
|
|
|
width: 500px;
|
2019-01-22 03:53:02 +08:00
|
|
|
height: 500px;
|
|
|
|
margin-right: 10px;
|
2019-01-22 03:42:43 +08:00
|
|
|
}
|
2019-01-22 03:53:02 +08:00
|
|
|
|
2019-01-22 03:42:43 +08:00
|
|
|
#containerRight {
|
2019-01-22 03:53:02 +08:00
|
|
|
display: inline-block;
|
2019-01-22 03:42:43 +08:00
|
|
|
width: 500px;
|
|
|
|
height: 500px;
|
|
|
|
}
|
|
|
|
#containerRight .different_element {
|
2020-05-23 20:26:29 +08:00
|
|
|
background-color: #acee61;
|
2019-01-22 03:42:43 +08:00
|
|
|
}
|
2020-05-23 20:26:29 +08:00
|
|
|
#containerRight .different_element div.jsoneditor-field,
|
|
|
|
#containerRight .different_element div.jsoneditor-value {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
|
2019-01-22 03:42:43 +08:00
|
|
|
#containerLeft .different_element {
|
2020-05-23 20:26:29 +08:00
|
|
|
background-color: pink;
|
|
|
|
}
|
|
|
|
#containerLeft .different_element div.jsoneditor-field,
|
|
|
|
#containerLeft .different_element div.jsoneditor-value {
|
|
|
|
color: red;
|
2019-01-22 03:42:43 +08:00
|
|
|
}
|
2020-05-23 20:26:29 +08:00
|
|
|
</style>
|
2019-01-22 03:42:43 +08:00
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
|
2019-01-22 03:53:02 +08:00
|
|
|
<h3>JSON Diff</h3>
|
2019-01-22 03:42:43 +08:00
|
|
|
<p>
|
2019-01-22 03:53:02 +08:00
|
|
|
This example highlights the differences between two JSON objects using the option <code>onClassName</code>.
|
|
|
|
Make a change in the left or right editor to see the changes update accordingly.
|
2019-01-22 03:42:43 +08:00
|
|
|
</p>
|
|
|
|
<div id="wrapper">
|
|
|
|
<div id="containerLeft"></div>
|
|
|
|
<div id="containerRight"></div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script>
|
2019-08-29 21:45:32 +08:00
|
|
|
const containerLeft = document.getElementById('containerLeft')
|
|
|
|
const containerRight = document.getElementById('containerRight')
|
2019-01-22 03:42:43 +08:00
|
|
|
|
|
|
|
function findNodeInJson(json, path){
|
|
|
|
if(!json || path.length ===0) {
|
|
|
|
return {field: undefined, value: undefined}
|
2019-08-29 21:45:32 +08:00
|
|
|
}
|
|
|
|
const first = path[0]
|
|
|
|
const remainingPath = path.slice(1)
|
2019-01-22 03:42:43 +08:00
|
|
|
|
|
|
|
if(remainingPath.length === 0) {
|
2019-08-29 21:45:32 +08:00
|
|
|
return {field: (typeof json[first] !== 'undefined' ? first : undefined), value: json[first]}
|
2019-01-22 03:42:43 +08:00
|
|
|
} else {
|
|
|
|
return findNodeInJson(json[first], remainingPath)
|
|
|
|
}
|
2019-01-22 04:04:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function onClassName({ path, field, value }) {
|
2019-08-29 21:45:32 +08:00
|
|
|
const thisNode = findNodeInJson(jsonRight, path)
|
|
|
|
const oppositeNode = findNodeInJson(jsonLeft, path)
|
|
|
|
let isValueEqual = JSON.stringify(thisNode.value) === JSON.stringify(oppositeNode.value)
|
2019-01-22 04:04:32 +08:00
|
|
|
|
|
|
|
if(Array.isArray(thisNode.value) && Array.isArray(oppositeNode.value)) {
|
|
|
|
isValueEqual = thisNode.value.every(function (e) {
|
2019-08-29 21:45:32 +08:00
|
|
|
return oppositeNode.value.includes(e)
|
2019-01-22 04:04:32 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thisNode.field === oppositeNode.field && isValueEqual) {
|
2019-08-29 21:45:32 +08:00
|
|
|
return 'the_same_element'
|
2019-01-22 04:04:32 +08:00
|
|
|
} else {
|
|
|
|
return 'different_element'
|
|
|
|
}
|
2019-01-22 03:42:43 +08:00
|
|
|
}
|
|
|
|
|
2019-08-29 21:45:32 +08:00
|
|
|
const optionsLeft = {
|
2019-01-22 03:42:43 +08:00
|
|
|
mode: 'tree',
|
|
|
|
onError: function (err) {
|
2019-08-29 21:45:32 +08:00
|
|
|
alert(err.toString())
|
2019-01-22 03:42:43 +08:00
|
|
|
},
|
2019-01-22 04:04:32 +08:00
|
|
|
onClassName: onClassName,
|
2019-01-22 03:42:43 +08:00
|
|
|
onChangeJSON: function (j) {
|
2019-08-29 21:45:32 +08:00
|
|
|
jsonLeft = j
|
2019-01-22 03:42:43 +08:00
|
|
|
window.editorRight.refresh()
|
|
|
|
}
|
2019-08-29 21:45:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const optionsRight = {
|
2019-01-22 03:42:43 +08:00
|
|
|
mode: 'tree',
|
|
|
|
onError: function (err) {
|
2019-08-29 21:45:32 +08:00
|
|
|
alert(err.toString())
|
2019-01-22 03:42:43 +08:00
|
|
|
},
|
2019-01-22 04:04:32 +08:00
|
|
|
onClassName: onClassName,
|
2019-01-22 03:42:43 +08:00
|
|
|
onChangeJSON: function (j) {
|
2019-08-29 21:45:32 +08:00
|
|
|
jsonRight = j
|
|
|
|
window.editorLeft.refresh()
|
2019-01-22 03:42:43 +08:00
|
|
|
}
|
2019-08-29 21:45:32 +08:00
|
|
|
}
|
2019-01-22 03:42:43 +08:00
|
|
|
|
2019-08-29 21:45:32 +08:00
|
|
|
let jsonLeft = {
|
2019-01-22 03:42:43 +08:00
|
|
|
"arrayOfArrays": [1, 2, 999, [3,4,5]],
|
|
|
|
"someField": true,
|
|
|
|
"boolean": true,
|
|
|
|
"htmlcode": '"',
|
|
|
|
"escaped_unicode": '\\u20b9',
|
|
|
|
"unicode": '\u20b9,\uD83D\uDCA9',
|
|
|
|
"return": '\n',
|
|
|
|
"null": null,
|
|
|
|
"thisObjectDoesntExistOnTheRight" : {key: "value"},
|
|
|
|
"number": 123,
|
|
|
|
"object": {"a": "b","new":4, "c": "d", "e": [1, 2, 3]},
|
|
|
|
"string": "Hello World",
|
|
|
|
"url": "http://jsoneditoronline.org",
|
|
|
|
"[0]": "zero"
|
2019-08-29 21:45:32 +08:00
|
|
|
}
|
2019-01-22 03:42:43 +08:00
|
|
|
|
2019-08-29 21:45:32 +08:00
|
|
|
let jsonRight = {
|
2019-01-22 03:42:43 +08:00
|
|
|
"arrayOfArrays": [1, 2, [3,4,5]],
|
|
|
|
"boolean": true,
|
|
|
|
"htmlcode": '"',
|
|
|
|
"escaped_unicode": '\\u20b9',
|
|
|
|
"thisFieldDoesntExistOnTheLeft": 'foobar',
|
|
|
|
"unicode": '\u20b9,\uD83D\uDCA9',
|
|
|
|
"return": '\n',
|
|
|
|
"null": null,
|
|
|
|
"number": 123,
|
|
|
|
"object": {"a": "b", "c": "d", "e": [1, 2, 3]},
|
|
|
|
"string": "Hello World",
|
|
|
|
"url": "http://jsoneditoronline.org",
|
|
|
|
"[0]": "zero"
|
2019-08-29 21:45:32 +08:00
|
|
|
}
|
2019-01-22 03:42:43 +08:00
|
|
|
|
2019-08-29 21:45:32 +08:00
|
|
|
window.editorLeft = new JSONEditor(containerLeft, optionsLeft, jsonLeft)
|
|
|
|
window.editorRight = new JSONEditor(containerRight, optionsRight, jsonRight)
|
2019-01-22 03:42:43 +08:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|