Created
August 12, 2016 18:09
-
-
Save Hummer12007/a87f280ed003ad8e84308deefff17627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jseval !function(){"use strict";var n=function(n){var l=document.createElement("style");return l.innerHTML=n,l}("#json{font-family:monospace;font-size:1.1em}#json ul{list-style:none;margin:0 0 0 2em;padding:0}#json ul li{position:relative}#json ul li span.null{color:red}#json ul li span.boolean{color:blue}#json ul li span.number{color:blue}#json ul li span.string{color:green}#json ul li span.prop{font-weight:bold}#json ul li:after{content:','}#json ul li:last-child:after{content:''}"),l=function(n){var l=function(n){return null!==n?n.toString().replace(/&/g,"&").replace(/"/g,""").replace(/</g,"<").replace(/>/g,">"):""},e=function(n,e){return"string"===e&&/^(http|https):\/\/[^\s]+$/.test(n)?'<a href="'+n+'">'+l(n)+"</a>":'<span class="'+e+'">'+l(n)+"</span>"},t=function(n,l){var t=typeof l,s="";if(null===l)s+=e("null","null");else switch(t){case"string":case"number":case"boolean":s+=e(l,t);break;case"object":s+=l instanceof Array?o(l,t):r(l,t)}return s},o=function(n){var l='[<ul class="array">',e=!1;for(var o in n)e=!0,l+="<li>",l+=t(o,n[o]),l+="</li>";return l+="</ul>]",e||(l="[ ]"),l},r=function(n){var e='{<ul class="obj">',o=!1;for(var r in n)o=!0,e+="<li>",e+='<span class="prop">'+l(r)+"</span>: ",e+=t(r,n[r]),e+="</li>";return e+="</ul>}",o||(e="{ }"),e};return'<div id="json">'+t(null,n)+"</div>"},e=l(JSON.parse(document.body.firstChild.textContent));document.body.innerHTML=e,document.body.appendChild(n)}(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
var style = (function (style) { | |
var node = document.createElement("style"); | |
node.innerHTML = style; | |
return node; | |
}("#json{font-family:monospace;font-size:1.1em}#json ul{list-style:none;margin:0 0 0 2em;padding:0}#json ul li{position:relative}#json ul li span.null{color:red}#json ul li span.boolean{color:blue}#json ul li span.number{color:blue}#json ul li span.string{color:green}#json ul li span.prop{font-weight:bold}#json ul li:after{content:','}#json ul li:last-child:after{content:''}")); | |
var toHtml = function (json) { | |
var htmlEncode = function (t) { | |
return t !== null ? t.toString().replace(/&/g, "&").replace(/"/g, """).replace(/</g, "<").replace(/>/g, ">") : ''; | |
}; | |
var decorateWithSpan = function (value, dataType) { | |
if (dataType === 'string' && /^(http|https):\/\/[^\s]+$/.test(value)) { | |
return '<a href="' + value + '">' + htmlEncode(value) + '</a>'; | |
} | |
return '<span class="' + dataType + '">' + htmlEncode(value) + '</span>'; | |
}; | |
var valueToHTML = function (key, value) { | |
var valueType = typeof value, | |
output = ''; | |
if (value === null) { | |
output += decorateWithSpan('null', 'null'); | |
} else { | |
switch (valueType) { | |
case 'string': | |
case 'number': | |
case 'boolean': | |
output += decorateWithSpan(value, valueType); | |
break; | |
//Recursive Methods | |
case 'object': | |
if (value instanceof Array) { | |
output += arrayToHTML(value, valueType); | |
} else { | |
output += objectToHTML(value, valueType); | |
} | |
break; | |
} | |
} | |
return output; | |
}; | |
var arrayToHTML = function (json) { | |
var output = '[<ul class="array">', | |
hasContents = false; | |
for (var prop in json) { | |
hasContents = true; | |
output += '<li>' + valueToHTML(prop, json[prop]) + '</li>'; | |
} | |
output += '</ul>]'; | |
if (!hasContents) output = "[ ]"; | |
return output; | |
}; | |
var objectToHTML = function (json) { | |
var output = '{<ul class="obj">', | |
hasContents = false; | |
for (var prop in json) { | |
hasContents = true; | |
output += '<li>' + '<span class="prop">' + htmlEncode(prop) + '</span>: ' + valueToHTML(prop, json[prop]) + '</li>'; | |
} | |
output += '</ul>}'; | |
if (!hasContents) output = "{ }"; | |
return output; | |
}; | |
return '<div id="json">' + valueToHTML(null, json) + '</div>'; | |
}; | |
var node = toHtml(JSON.parse(document.body.firstChild.textContent)); | |
document.body.innerHTML = node; | |
document.body.appendChild(style); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment