4 * Copyright(c) 2006-2007, Ext JS, LLC.
6 * Originally Released Under LGPL - original licence link has changed is not relivant.
9 * <script type="text/javascript">
12 * @class Roo.util.JSON
13 * Modified version of Douglas Crockford"s json.js that doesn"t
14 * mess with the Object prototype
15 * http://www.json.org/js.html
18 Roo.util.JSON = new (function(){
19 var useHasOwn = {}.hasOwnProperty ? true : false;
21 // crashes Safari in some instances
22 //var validRE = /^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/;
24 var pad = function(n) {
25 return n < 10 ? "0" + n : n;
38 var encodeString = function(s){
39 if (/["\\\x00-\x1f]/.test(s)) {
40 return '"' + s.replace(/([\x00-\x1f\\"])/g, function(a, b) {
47 Math.floor(c / 16).toString(16) +
48 (c % 16).toString(16);
54 var encodeArray = function(o){
55 var a = ["["], b, i, l = o.length, v;
56 for (i = 0; i < l; i += 1) {
67 a.push(v === null ? "null" : Roo.util.JSON.encode(v));
75 var encodeDate = function(o){
76 return '"' + o.getFullYear() + "-" +
77 pad(o.getMonth() + 1) + "-" +
78 pad(o.getDate()) + "T" +
79 pad(o.getHours()) + ":" +
80 pad(o.getMinutes()) + ":" +
81 pad(o.getSeconds()) + '"';
85 * Encodes an Object, Array or other value
86 * @param {Mixed} o The variable to encode
87 * @return {String} The JSON string
89 this.encode = function(o)
91 // should this be extended to fully wrap stringify..
93 if(typeof o == "undefined" || o === null){
95 }else if(o instanceof Array){
96 return encodeArray(o);
97 }else if(o instanceof Date){
99 }else if(typeof o == "string"){
100 return encodeString(o);
101 }else if(typeof o == "number"){
102 return isFinite(o) ? String(o) : "null";
103 }else if(typeof o == "boolean"){
106 var a = ["{"], b, i, v;
108 if(!useHasOwn || o.hasOwnProperty(i)) {
119 a.push(this.encode(i), ":",
120 v === null ? "null" : this.encode(v));
131 * Decodes (parses) a JSON string to an object. If the JSON is invalid, this function throws a SyntaxError.
132 * @param {String} json The JSON string
133 * @return {Object} The resulting object
135 this.decode = function(json){
137 return /** eval:var:json */ eval("(" + json + ')');
141 * Shorthand for {@link Roo.util.JSON#encode}
144 Roo.encode = typeof(JSON) != 'undefined' && JSON.stringify ? JSON.stringify : Roo.util.JSON.encode;
146 * Shorthand for {@link Roo.util.JSON#decode}
149 Roo.decode = typeof(JSON) != 'undefined' && JSON.parse ? JSON.parse : Roo.util.JSON.decode;