Add OK and Cancel button for grid editing
[phpmyadmin/arisferyanto.git] / js / jquery / jquery.json-2.2.js
blob87f5d0157c801ea16929f8452cde507e9c5f5bcd
1 /*
2 * jQuery JSON Plugin
3 * version: 2.1 (2009-08-14)
5 * This document is licensed as free software under the terms of the
6 * MIT License: http://www.opensource.org/licenses/mit-license.php
8 * Brantley Harris wrote this plugin. It is based somewhat on the JSON.org
9 * website's http://www.json.org/json2.js, which proclaims:
10 * "NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.", a sentiment that
11 * I uphold.
13 * It is also influenced heavily by MochiKit's serializeJSON, which is
14 * copyrighted 2005 by Bob Ippolito.
17 (function($) {
18 /** jQuery.toJSON( json-serializble )
19 Converts the given argument into a JSON respresentation.
21 If an object has a "toJSON" function, that will be used to get the representation.
22 Non-integer/string keys are skipped in the object, as are keys that point to a function.
24 json-serializble:
25 The *thing* to be converted.
26 **/
27 $.toJSON = function(o)
29 if (typeof(JSON) == 'object' && JSON.stringify)
30 return JSON.stringify(o);
32 var type = typeof(o);
34 if (o === null)
35 return "null";
37 if (type == "undefined")
38 return undefined;
40 if (type == "number" || type == "boolean")
41 return o + "";
43 if (type == "string")
44 return $.quoteString(o);
46 if (type == 'object')
48 if (typeof o.toJSON == "function")
49 return $.toJSON( o.toJSON() );
51 if (o.constructor === Date)
53 var month = o.getUTCMonth() + 1;
54 if (month < 10) month = '0' + month;
56 var day = o.getUTCDate();
57 if (day < 10) day = '0' + day;
59 var year = o.getUTCFullYear();
61 var hours = o.getUTCHours();
62 if (hours < 10) hours = '0' + hours;
64 var minutes = o.getUTCMinutes();
65 if (minutes < 10) minutes = '0' + minutes;
67 var seconds = o.getUTCSeconds();
68 if (seconds < 10) seconds = '0' + seconds;
70 var milli = o.getUTCMilliseconds();
71 if (milli < 100) milli = '0' + milli;
72 if (milli < 10) milli = '0' + milli;
74 return '"' + year + '-' + month + '-' + day + 'T' +
75 hours + ':' + minutes + ':' + seconds +
76 '.' + milli + 'Z"';
79 if (o.constructor === Array)
81 var ret = [];
82 for (var i = 0; i < o.length; i++)
83 ret.push( $.toJSON(o[i]) || "null" );
85 return "[" + ret.join(",") + "]";
88 var pairs = [];
89 for (var k in o) {
90 var name;
91 var type = typeof k;
93 if (type == "number")
94 name = '"' + k + '"';
95 else if (type == "string")
96 name = $.quoteString(k);
97 else
98 continue; //skip non-string or number keys
100 if (typeof o[k] == "function")
101 continue; //skip pairs where the value is a function.
103 var val = $.toJSON(o[k]);
105 pairs.push(name + ":" + val);
108 return "{" + pairs.join(", ") + "}";
112 /** jQuery.evalJSON(src)
113 Evaluates a given piece of json source.
115 $.evalJSON = function(src)
117 if (typeof(JSON) == 'object' && JSON.parse)
118 return JSON.parse(src);
119 return eval("(" + src + ")");
122 /** jQuery.secureEvalJSON(src)
123 Evals JSON in a way that is *more* secure.
125 $.secureEvalJSON = function(src)
127 if (typeof(JSON) == 'object' && JSON.parse)
128 return JSON.parse(src);
130 var filtered = src;
131 filtered = filtered.replace(/\\["\\\/bfnrtu]/g, '@');
132 filtered = filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']');
133 filtered = filtered.replace(/(?:^|:|,)(?:\s*\[)+/g, '');
135 if (/^[\],:{}\s]*$/.test(filtered))
136 return eval("(" + src + ")");
137 else
138 throw new SyntaxError("Error parsing JSON, source is not valid.");
141 /** jQuery.quoteString(string)
142 Returns a string-repr of a string, escaping quotes intelligently.
143 Mostly a support function for toJSON.
145 Examples:
146 >>> jQuery.quoteString("apple")
147 "apple"
149 >>> jQuery.quoteString('"Where are we going?", she asked.')
150 "\"Where are we going?\", she asked."
152 $.quoteString = function(string)
154 if (string.match(_escapeable))
156 return '"' + string.replace(_escapeable, function (a)
158 var c = _meta[a];
159 if (typeof c === 'string') return c;
160 c = a.charCodeAt();
161 return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
162 }) + '"';
164 return '"' + string + '"';
167 var _escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
169 var _meta = {
170 '\b': '\\b',
171 '\t': '\\t',
172 '\n': '\\n',
173 '\f': '\\f',
174 '\r': '\\r',
175 '"' : '\\"',
176 '\\': '\\\\'
178 })(jQuery);