/***jQueryjson-viewer*@author:AlexandreBodelot<alexandre.bodelot@gmail.com>*/(function($){ /***Checkifargiseitheranarraywithatleast1element,oradictwithatleast1key*@returnboolean*/ function isCollapsable(arg) { return arg instanceof Object && Object.keys(arg).length > 0; } /***Checkifastringrepresentsavalidurl*@returnboolean*/ function isUrl(string) { var regexp = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/; return regexp.test(string); } /***Transformajsonobjectintohtmlrepresentation*@returnstring*/ function json2html(json, options) { var html = ''; if (typeof json === 'string') { /*Escapetags*/ json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); if (isUrl(json)) html += '<a href="' + json + '" class="json-string">' + json + '</a>';elsehtml+='<span class="json-string">"'+json+'"</span>';}elseif(typeofjson==='number'){html+='<span class="json-literal">'+json+'</span>';}elseif(typeofjson==='boolean'){html+='<span class="json-literal">'+json+'</span>';}elseif(json===null){html+='<span class="json-literal">null</span>';}elseif(jsoninstanceofArray){if(json.length>0){html+='[<ol class="json-array">';for(vari=0;i<json.length;++i){html+='<li>';/* Add toggle button if item is collapsable */if(isCollapsable(json[i])){html+='<a href class="json-toggle"></a>';}html+=json2html(json[i],options);/* Add comma if item is not last */if(i<json.length-1){html+=',';}html+='</li>';}html+='</ol>]';}else{html+='[]';}}elseif(typeofjson==='object'){varkey_count=Object.keys(json).length;if(key_count>0){html+='{<ul class="json-dict">';for(varkeyinjson){if(json.hasOwnProperty(key)){html+='<li>';varkeyRepr=options.withQuotes?'<span class="json-string">"'+key+'"</span>':key;/* Add toggle button if item is collapsable */if(isCollapsable(json[key])){html+='<a href class="json-toggle">'+keyRepr+'</a>';}else{html+=keyRepr;}html+=': '+json2html(json[key],options);/* Add comma if item is not last */if(--key_count>0)html+=',';html+='</li>';}}html+='</ul>}';}else{html+='{}';}}returnhtml;}/** * jQuery plugin method * @param json: a javascript object * @param options: an optional options hash */$.fn.jsonViewer=function(json,options){options=options||{};/* jQuery chaining */returnthis.each(function(){/* Transform to HTML */varhtml=json2html(json,options);if(isCollapsable(json))html='<a href class="json-toggle"></a>'+html;/* Insert HTML in target DOM element */$(this).html(html);/* Bind click on toggle buttons */$(this).off('click');$(this).on('click','a.json-toggle',function(){vartarget=$(this).toggleClass('collapsed').siblings('ul.json-dict, ol.json-array');target.toggle();if(target.is(':visible')){target.siblings('.json-placeholder').remove();}else{varcount=target.children('li').length;varplaceholder=count+(count>1?' items':' item');target.after('<a href class="json-placeholder">'+placeholder+'</a>');}returnfalse;});/* Simulate click on toggle button when placeholder is clicked */$(this).on('click','a.json-placeholder',function(){$(this).siblings('a.json-toggle').click();returnfalse;});if(options.collapsed==true){/* Trigger click to collapse all nodes */$(this).find('a.json-toggle').click();}});};})(jQuery);