-
Notifications
You must be signed in to change notification settings - Fork 2
/
pretty_json.js
57 lines (51 loc) · 1.69 KB
/
pretty_json.js
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
var JSON_PRETTY = new function () {
var pretty = {
"parse": function (member) {
return this[(member == undefined) ? 'null' : member.constructor.name.toLowerCase()](member);
},
"null": function (value) {
return this['value']('null', 'null');
},
"array": function (value) {
var results = '';
for (var x=0; x < value.length; x++) {
results += '<li>' + this['parse'](value[x]) + '</li>';
}
return '[ ' + ((results.length > 0) ? '<ul class="array">' + results + '</ul>' : '') + ' ]';
},
"object": function (value) {
var results = '';
for (member in value) {
results += '<li>' + this['value']('object', member) + ': ' + this['parse'](value[member]) + '</li>';
}
return '{ ' + ((results.length > 0) ? '<ul class="object">' + results + '</ul>' : '') + ' }';
},
"number": function (value) {
return this['value']('number', value);
},
"string": function (value) {
return this['value']('string', value);
},
"boolean": function (value) {
return this['value']('boolean', value);
},
"value": function (type, value) {
if (/^(http|https):\/\/[^\s]+$/.test(value)) {
return this['value'](type, '<a href="' + value + '" target="_blank">' + value + '</a>');
}
return '<span class="' + type + '">' + value + '</span>';
}
};
var parse = {
"error": function (error) {
return '<h1>Unable to parse JSON.</h1><p><h2>Error Message:</h2><textarea>' + error + '</textarea><br /><br /><h2>Response:</h2><textarea>' + document.body.innerText + '</textarea></p>';
}
}
try {
var output = pretty.parse (eval ('(' + document.body.innerText + ')'));
}
catch (error) {
var output = parse.error (error);
}
document.body.innerHTML = output;
};