/* jsonEditor 1.02 copyright 2007-2009 Thomas Frank Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ JSONeditor={ start:function(treeDivName,formDivName,json,showExamples){ if(this.examples.length<6){ var e=this.treeBuilder.JSONstring.make(this) eval("this.examples[5]={JSONeditor:"+e+"}") } this.treeDivName=treeDivName var t=this.treeBuilder, $=t.$ treeBuilder=t var s=$(treeDivName).style var f=$(formDivName) var fs=f.style f.innerHTML=this.formHTML if(!showExamples){$('jExamples').style.display="none"} fs.fontSize=s.fontSize="11px" fs.fontFamily=s.fontFamily="Verdana,Arial,Helvetica,sans-serif" var e=f.getElementsByTagName("*") for(var i=0;i
Load an example: 

\nLabel:


\nValue:


\nData type:     \n\n \n

\n\n\n

\n \n \n \n \n \n

\nAdd children first/siblings before\n
\nPaste as child on objects & arrays\n

\n", examples:[{}, {employee:{gid:102, companyID:121, defaultActionID:444,names:{firstName:"Stive", middleInitial:"Jr",lastName:"Martin"},address:{city:"Albany",state:"NY",zipCode:"14410-585",addreess:"41 State Street"},job:{departmentID:102,jobTitleID:100,hireDate:"1/02/2000",terminationDate:"1/12/2007"},contact:{phoneHome:"12-123-2133", beeper:"5656",email1:"info@soft-amis.com",fax:"21-321-23223",phoneMobile:"32-434-3433",phoneOffice:"82-900-8993"},login:{employeeID:"eID102",password:"password",superUser:true,lastLoginDate:"1/12/2007",text:"text", regexp:/^mmm/, date: new Date() },comment:{PCDATA:"comment"},roles:[{role:102},{role:103}]}}, {"widget": {"debug": true,"window": {"title": "Sample Konfabulator Widget","name": "main_window","width": 500,"height": 500},"Pairs": [ {"src": "Images/Sun.png","name": "sun1"},{"hOffset": 250,"vOffset": 200},null,{"alignment": "center"}],"text": {"a very long item label here": "Click Here","size": 36,"style": "null","name": "text1","hOffset": 250,"vOffset": 100,"alignment": "center","onmouseover": function(){alert("Hello World");},"onMouseUp": "sun1.opacity = (sun1.opacity / 100) * 90;"}}}, {"members": [{"href": "1","entity": {"category": [{"term": "weblog", "label": "Weblog stuff"}],"updated": "2007-05-02T23:32:03Z","title": "This is the second post","author": {"uri": "http://dealmeida.net/","email": "roberto@dealmeida.net","name": "Rob de Almeida"},"summary": "Testing search","content": {"content": "This is my second post, to test the search.","type": "text"},"id": "1"}},{"href": "0","entity": {"category": [{"term": "weblog", "label": "Weblog stuff"},{"term": "json", "label": "JSON"}],"updated": "2007-05-02T23:25:59Z","title": "This is the second version of the first post","author": {"uri": "http://dealmeida.net/","email": "roberto@dealmeida.net","name": "Rob de Almeida"},"summary": "This is my first post here, after some modifications","content": {"content": "This is my first post, testing the jsonstore WSGI microapp PUT.","type": "html"},"id": "0"}}],"next": null}, {"menu": {"header": "SVG Viewer","items": [{"id": "Open"},{"id": "OpenNew", "label": "Open New", "thing": "thing"},{"id": "ZoomIn", "label": "Zoom In"},{"id": "ZoomOut", "label": "Zoom Out"},{"id": "OriginalView", "label": "Original View"},null,{"id": "Quality"},{"id": "Pause"},{"id": "Mute"},null,{"id": "Find", "label": "Find..."},{"id": "FindAgain", "label": "Find Again"},{"id": "Copy"},{"id": "CopyAgain", "label": "Copy Again"},{"id": "CopySVG", "label": "Copy SVG"},{"id": "ViewSVG", "label": "View SVG"}]}} ] } /* treeBuilder v 1.00 + a lot of json stuff added... copyright 2007 Thomas Frank */ JSONeditor.treeBuilder={ stateMem:{}, images:{ folderNode:'', folderNodeOpen:'', folderNodeLast:'', folderNodeOpenLast:'', docNode:'', docNodeLast:'', folder:'', folderOpen:'', doc:'', vertLine:'', folderNodeFirst:'', folderNodeOpenFirst:'', folderNodeLastFirst:'', folderNodeOpenLastFirst:'', path:'../../images/treeBuilderImages/', nodeWidth:16 }, $:function(x){return document.getElementById(x)}, preParse:function(x){ var x=x.innerHTML.split("\n"); var d=[]; for(var i=0;i0;i--){o[i]=o[i-1]} o[0]={} n=l+"[0]" } } this.JSONbuild(this.baseDiv,this.json) for(var i in this.stateMem){this.openAndClose(i,true)} this.jsonResponder(n) }, jsonAddSibling:function(f,label){ var before=f.jbefore.checked var l=f.orgjlabel.value var r=Math.random() eval('var temp=this.'+l) eval('this.'+l+"=r") var s=this.JSONstring.make(this.json) s=s.split(r+",") if(s.length<2){s=s[0].split(r)} var lp=this.jsonParent(l) eval('var o=this.'+lp) var t=(o && this.isArray(o))?"array":typeof o if(t=="object"){ var nl=label||prompt("Label (without path):","") if(!nl){return} if(nl/1==nl){nl="$"+nl} nl=this.jsonAlreadyExists(o,nl) var n=nl.replace(/\w/g,'')===""?"."+nl:'["'+nl+'"]' s=s.join('null,"'+nl+'":{},') lp+=n } if(t=="array"){ s=s.join('null,{},') var k=l.split("[") k[k.length-1]=(k[k.length-1].split("]").join("")/1+1)+"]" lp=k.join("[") } s=s.split("},}").join("}}") // replace with something better soon eval('this.json='+s) eval('this.'+l+'=temp') if(before){lp=this.jsonSwitchPlace(this.jsonParent(l),l,lp)} this.JSONbuild(this.baseDiv,this.json) for(var i in this.stateMem){this.openAndClose(i,true)} this.jsonResponder(lp) }, jSaveFirst:function(f,a){ var l=f.orgjlabel.value eval("var orgj=this."+l) orgj=this.JSONstring.make(orgj) var v=f.jvalue.value v=f.jtype.value=="string"?this.JSONstring.make(v):v v=v.split("\r").join("") if(orgj!=v || f.orgjlabel.value!=f.jlabel.value || this.jTypeChanged){ var k=confirm("Save before "+a+"?") if(k){this.jsonChange(f)} } }, jsonRename:function(f){ this.jSaveFirst(f,"renaming") var orgl=l=f.orgjlabel.value l=this.jsonChild(l) var nl=prompt("Label (without path):",l) if(!nl){return} this.jsonResponder(orgl) var nl=nl.replace(/\w/g,'')===""?"."+nl:'["'+nl+'"]' f.jlabel.value=this.jsonParent(orgl)+nl this.jsonChange(f,false,true) }, jsonSwitchPlace:function(p,el1,el2){ var orgel1=el1, orgel2=el2 eval("var o=this."+p) if(this.isArray(o)){ eval("var t=this."+el1) eval("this."+el1+"=this."+el2) eval("this."+el2+"=t") return orgel1 } el1=this.jsonChild(el1) el2=this.jsonChild(el2) var o2={} for(var i in o){ if(i==el1){o2[el2]=o[el2];o2[el1]=o[el1];continue} if(i==el2){continue} o2[i]=o[i] } eval("this."+p+"=o2") return orgel2 }, jsonCut:function(f){ this.jSaveFirst(f,"cutting") this.jsonCopy(f,true) this.jsonChange(f,true) this.setJsonMessage('Cut to clipboard!') }, jsonCopy:function(f,r){ if(!r){this.jSaveFirst(f,"copying")} var l=f.orgjlabel.value eval("var v=this."+l) v=this.JSONstring.make(v) var l=this.jsonChild(l) this.jClipboard={label:l,jvalue:v} this.jsonResponder(f.jlabel.value) if(!r){this.setJsonMessage('Copied to clipboard!')} }, jsonPaste:function(f,r){ var t=f.jtype.value var sibling=t!="object" && t!="array" if(!f.jPasteAsChild.checked){sibling=true} if(f.orgjlabel.value=="json"){sibling=false} if(sibling){this.jsonAddSibling(f,this.jClipboard.label)} else {this.jsonAddChild(f,this.jClipboard.label)} var l=f.orgjlabel.value eval("this."+l+"="+this.jClipboard.jvalue) this.jsonResponder(l) this.jsonChange(f) if(!r){this.setJsonMessage('Pasted!')} }, setJsonMessage:function(x){ this.$('jformMessage').innerHTML=x setTimeout("treeBuilder.$('jformMessage').innerHTML=''",1500) }, changeJsonDataType:function(x,f){ this.jTypeChanged=true var v=f.jvalue.value var orgv=v; v=x=='object'?'{"label":"'+v+'"}':v v=x=='array'?'["'+v+'"]':v if(!orgv){ v=x=='object'?'{}':v v=x=='array'?'[]':v } v=x=='function'?'function(){'+v+'}':v v=x=='string'?v:v v=x=='number'?v/1:v v=x=='boolean'?!!v:v v=x=='null'?'null':v v=x=='undefined'?'undefined':v f.jvalue.value=v }, jsonChange:function(f,remove,rename){ try { var l=f.jlabel.value var orgl=f.orgjlabel.value||"json.not1r2e3a4l" eval("var cur=this."+l) if(l!=orgl && cur!==undefined){ var c=confirm(l+"\n\nalready contains other data. Overwrite?") if(!c){return false} } var v=f.jvalue.value.split("\r").join("") if(f.jtype.value=="string"){ v=this.JSONstring.make(v) } if(l=="json"){ eval("v="+v) this.JSONbuild(this.baseDiv,v) for(var i in this.stateMem){this.openAndClose(i,true)} this.setJsonMessage('Saved!') return false } eval("var json="+this.JSONstring.make(this.json)) var randi=Math.random() eval(orgl+'='+randi) var paname=this.jsonParent(orgl) var samepa=this.jsonParent(orgl)==this.jsonParent(l) eval("var pa="+paname) if(this.isArray(pa)){ eval(paname+'=[];var newpa='+paname) for(var i=0;ilastlevel){levelmem.push(n);n=d[i-1].id}; if(d[i].level && d[i].level>lastlevel+1){return 'Trying to jump levels!'}; if(d[i].level && d[i].level=0)?j+'First':j; img.src=im.path+(im[k]?im[k]:k+'.gif'); img.style.display="none"; img.style.verticalAlign="middle"; img.id=m.id+"_"+j; if(j.indexOf('folderNode')==0){ img.onclick=function(e){ treeBuilder.openAndClose(this); if (!e){e=window.event}; e.cancelBubble = true; if(e.stopPropagation){e.stopPropagation()}; } }; if(m.firstChild){m.insertBefore(img,m.childNodes[co]); co++} else {m.appendChild(img)}; } }; m.insertBefore(m.t,m.childNodes[co]); m.inited=true }; var lastChild=m.childNodes[m.childNodes.length-1]; var isParent=(lastChild.tagName+"").toLowerCase()=="div"; var isLast=!m.nextSibling; var isOpen=isParent && lastChild.style.display!='none'; $(m.id+"_folder").style.display=!isOpen && isParent?'':'none'; $(m.id+"_folderOpen").style.display=isOpen && isParent?'':'none'; $(m.id+"_doc").style.display=isParent?'none':''; $(m.id+"_docNode").style.display=isParent || isLast?'none':''; $(m.id+"_docNodeLast").style.display=isParent || !isLast?'none':''; $(m.id+"_folderNode").style.display=isOpen || !isParent || isLast?'none':''; $(m.id+"_folderNodeLast").style.display=isOpen || !isParent || !isLast?'none':''; $(m.id+"_folderNodeOpen").style.display=!isOpen || !isParent || isLast?'none':''; $(m.id+"_folderNodeOpenLast").style.display=!isOpen || !isParent || !isLast?'none':''; var p=m.parentNode.nextSibling; if(p && p.id){ var sp=p;insideBase=false; while(sp){if(sp==$(this.treeBaseDiv)){insideBase=true};sp=sp.parentNode} if(!insideBase){return} var bg=im.path+(im.vertLine?im.vertLine:'vertLine.gif'); m.style.backgroundImage='url('+bg+')'; m.style.backgroundRepeat='repeat-y' }; }, openAndClose:function(x,remem){ var o, div=remem?this.$(x):x.parentNode; if(!div){return} if(remem){o=this.stateMem[div.id]} else {o=x.id.indexOf('Open')<0} if(remem=="open"){o=true} this.stateMem[div.id]=o var c=div.childNodes; for(var i=0;i 0) out.push(',\n'); this.toJsonStringArray(arg[i], out); this.path.pop(); } out.push(']'); return out; } else if (typeof arg.toString != 'undefined') { out.push('{'); var first = true; for (var i in arg) { if(!this.includeProtos && arg[i]===arg.constructor.prototype[i]){continue}; this.path.push(i); var curr = out.length; if (!first) out.push(this.compactOutput?',':',\n'); this.toJsonStringArray(i, out); out.push(':'); this.toJsonStringArray(arg[i], out); if (out[out.length - 1] == u) out.splice(curr, out.length - curr); else first = false; this.path.pop(); } out.push('}'); return out; } return out; } out.push('null'); return out; case 'unknown': case 'undefined': case 'function': try {eval('var a='+arg)} catch(e){arg='function(){alert("Could not convert the real function to JSON, due to a browser bug only found in Safari. Let us hope it will get fixed in future versions of Safari!")}'} out.push(this.includeFunctions?arg:u); return out; case 'string': if(this.restore && arg.indexOf("JSONcircRef:")==0){ this.restoreCode.push('this.myObj.'+this.path.join(".")+"="+arg.split("JSONcircRef:").join("this.myObj.")); }; out.push('"'); var a=['\n','\\n','\r','\\r','"','\\"']; arg+=""; for(var i=0;i<6;i+=2){arg=arg.split(a[i]).join(a[i+1])}; out.push(arg); out.push('"'); return out; default: out.push(String(arg)); return out; } } } /* http://www.JSON.org/json2.js 2009-06-29 Public Domain. NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK. See http://www.JSON.org/js.html This file creates a global JSON object containing two methods: stringify and parse. JSON.stringify(value, replacer, space) value any JavaScript value, usually an object or array. replacer an optional parameter that determines how object values are stringified for objects. It can be a function or an array of strings. space an optional parameter that specifies the indentation of nested structures. If it is omitted, the text will be packed without extra whitespace. If it is a number, it will specify the number of spaces to indent at each level. If it is a string (such as '\t' or ' '), it contains the characters used to indent at each level. This method produces a JSON text from a JavaScript value. When an object value is found, if the object contains a toJSON method, its toJSON method will be called and the result will be stringified. A toJSON method does not serialize: it returns the value represented by the name/value pair that should be serialized, or undefined if nothing should be serialized. The toJSON method will be passed the key associated with the value, and this will be bound to the object holding the key. For example, this would serialize Dates as ISO strings. Date.prototype.toJSON = function (key) { function f(n) { // Format integers to have at least two digits. return n < 10 ? '0' + n : n; } return this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z'; }; You can provide an optional replacer method. It will be passed the key and value of each member, with this bound to the containing object. The value that is returned from your method will be serialized. If your method returns undefined, then the member will be excluded from the serialization. If the replacer parameter is an array of strings, then it will be used to select the members to be serialized. It filters the results such that only members with keys listed in the replacer array are stringified. Values that do not have JSON representations, such as undefined or functions, will not be serialized. Such values in objects will be dropped; in arrays they will be replaced with null. You can use a replacer function to replace those with JSON values. JSON.stringify(undefined) returns undefined. The optional space parameter produces a stringification of the value that is filled with line breaks and indentation to make it easier to read. If the space parameter is a non-empty string, then that string will be used for indentation. If the space parameter is a number, then the indentation will be that many spaces. Example: text = JSON.stringify(['e', {pluribus: 'unum'}]); // text is '["e",{"pluribus":"unum"}]' text = JSON.stringify(['e', {pluribus: 'unum'}], null, '\t'); // text is '[\n\t"e",\n\t{\n\t\t"pluribus": "unum"\n\t}\n]' text = JSON.stringify([new Date()], function (key, value) { return this[key] instanceof Date ? 'Date(' + this[key] + ')' : value; }); // text is '["Date(---current time---)"]' JSON.parse(text, reviver) This method parses a JSON text to produce an object or array. It can throw a SyntaxError exception. The optional reviver parameter is a function that can filter and transform the results. It receives each of the keys and values, and its return value is used instead of the original value. If it returns what it received, then the structure is not modified. If it returns undefined then the member is deleted. Example: // Parse the text. Values that look like ISO date strings will // be converted to Date objects. myData = JSON.parse(text, function (key, value) { var a; if (typeof value === 'string') { a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); if (a) { return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])); } } return value; }); myData = JSON.parse('["Date(09/09/2001)"]', function (key, value) { var d; if (typeof value === 'string' && value.slice(0, 5) === 'Date(' && value.slice(-1) === ')') { d = new Date(value.slice(5, -1)); if (d) { return d; } } return value; }); This is a reference implementation. You are free to copy, modify, or redistribute. This code should be minified before deployment. See http://javascript.crockford.com/jsmin.html USE YOUR OWN COPY. IT IS EXTREMELY UNWISE TO LOAD CODE FROM SERVERS YOU DO NOT CONTROL. */ /*jslint evil: true */ /*members "", "\b", "\t", "\n", "\f", "\r", "\"", JSON, "\\", apply, call, charCodeAt, getUTCDate, getUTCFullYear, getUTCHours, getUTCMinutes, getUTCMonth, getUTCSeconds, hasOwnProperty, join, lastIndex, length, parse, prototype, push, replace, slice, stringify, test, toJSON, toString, valueOf */ // Create a JSON object only if one does not already exist. We create the // methods in a closure to avoid creating global variables. var JSON = JSON || {}; (function () { function f(n) { // Format integers to have at least two digits. return n < 10 ? '0' + n : n; } if (typeof Date.prototype.toJSON !== 'function') { Date.prototype.toJSON = function (key) { return isFinite(this.valueOf()) ? this.getUTCFullYear() + '-' + f(this.getUTCMonth() + 1) + '-' + f(this.getUTCDate()) + 'T' + f(this.getUTCHours()) + ':' + f(this.getUTCMinutes()) + ':' + f(this.getUTCSeconds()) + 'Z' : null; }; String.prototype.toJSON = Number.prototype.toJSON = Boolean.prototype.toJSON = function (key) { return this.valueOf(); }; } var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, escapable = /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g, gap, indent, meta = { // table of character substitutions '\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\' }, rep; function quote(string) { // If the string contains no control characters, no quote characters, and no // backslash characters, then we can safely slap some quotes around it. // Otherwise we must also replace the offending characters with safe escape // sequences. escapable.lastIndex = 0; return escapable.test(string) ? '"' + string.replace(escapable, function (a) { var c = meta[a]; return typeof c === 'string' ? c : '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); }) + '"' : '"' + string + '"'; } function str(key, holder) { // Produce a string from holder[key]. var i, // The loop counter. k, // The member key. v, // The member value. length, mind = gap, partial, value = holder[key]; // If the value has a toJSON method, call it to obtain a replacement value. if (value && typeof value === 'object' && typeof value.toJSON === 'function') { value = value.toJSON(key); } // If we were called with a replacer function, then call the replacer to // obtain a replacement value. if (typeof rep === 'function') { value = rep.call(holder, key, value); } // What happens next depends on the value's type. switch (typeof value) { case 'string': return quote(value); case 'number': // JSON numbers must be finite. Encode non-finite numbers as null. return isFinite(value) ? String(value) : 'null'; case 'boolean': case 'null': // If the value is a boolean or null, convert it to a string. Note: // typeof null does not produce 'null'. The case is included here in // the remote chance that this gets fixed someday. return String(value); // If the type is 'object', we might be dealing with an object or an array or // null. case 'object': // Due to a specification blunder in ECMAScript, typeof null is 'object', // so watch out for that case. if (!value) { return 'null'; } // Make an array to hold the partial results of stringifying this object value. gap += indent; partial = []; // Is the value an array? if (Object.prototype.toString.apply(value) === '[object Array]') { // The value is an array. Stringify every element. Use null as a placeholder // for non-JSON values. length = value.length; for (i = 0; i < length; i += 1) { partial[i] = str(i, value) || 'null'; } // Join all of the elements together, separated with commas, and wrap them in // brackets. v = partial.length === 0 ? '[]' : gap ? '[\n' + gap + partial.join(',\n' + gap) + '\n' + mind + ']' : '[' + partial.join(',') + ']'; gap = mind; return v; } // If the replacer is an array, use it to select the members to be stringified. if (rep && typeof rep === 'object') { length = rep.length; for (i = 0; i < length; i += 1) { k = rep[i]; if (typeof k === 'string') { v = str(k, value); if (v) { partial.push(quote(k) + (gap ? ': ' : ':') + v); } } } } else { // Otherwise, iterate through all of the keys in the object. var z = new Array(); for (k in value) z.push(k); z.sort(); for (var l=0;l