Skip to content

Commit 42467d0

Browse files
author
Arian
committed
Backport JSON from 2.0 so it uses JSON.parse and JSON.stringify
1 parent 17cf2d0 commit 42467d0

File tree

1 file changed

+48
-32
lines changed

1 file changed

+48
-32
lines changed

Source/Utilities/JSON.js

+48-32
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,53 @@ JSON = new Hash({
2727

2828
//</1.2compat>
2929

30-
Object.append(JSON, {
31-
32-
$specialChars: {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'},
33-
34-
$replaceChars: function(chr){
35-
return JSON.$specialChars[chr] || '\\u00' + Math.floor(chr.charCodeAt() / 16).toString(16) + (chr.charCodeAt() % 16).toString(16);
36-
},
37-
38-
encode: function(obj){
39-
switch (typeOf(obj)){
40-
case 'string':
41-
return '"' + obj.replace(/[\x00-\x1f\\"]/g, JSON.$replaceChars) + '"';
42-
case 'array':
43-
return '[' + String(obj.map(JSON.encode).clean()) + ']';
44-
case 'object': case 'hash':
45-
var string = [];
46-
Object.each(obj, function(value, key){
47-
var json = JSON.encode(value);
48-
if (json) string.push(JSON.encode(key) + ':' + json);
49-
});
50-
return '{' + string + '}';
51-
case 'number': case 'boolean': return String(obj);
52-
case 'null': return 'null';
53-
}
54-
return null;
55-
},
56-
57-
decode: function(string, secure){
58-
if (typeOf(string) != 'string' || !string.length) return null;
59-
if (secure && !(/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(string.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, ''))) return null;
60-
return eval('(' + string + ')');
30+
(function(){
31+
32+
var special = {'\b': '\\b', '\t': '\\t', '\n': '\\n', '\f': '\\f', '\r': '\\r', '"' : '\\"', '\\': '\\\\'};
33+
34+
var escape = function(chr){
35+
return special[chr] || '\\u' + ('0000' + chr.charCodeAt(0).toString(16)).slice(-4);
36+
};
37+
38+
JSON.validate = function(string){
39+
string = string.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
40+
replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
41+
replace(/(?:^|:|,)(?:\s*\[)+/g, '');
42+
43+
return (/^[\],:{}\s]*$/).test(string);
44+
};
45+
46+
JSON.encode = JSON.stringify || function(obj){
47+
if (obj && obj.toJSON) obj = obj.toJSON();
48+
49+
switch (typeOf(obj)){
50+
case 'string':
51+
return '"' + obj.replace(/[\x00-\x1f\\"]/g, escape) + '"';
52+
case 'array':
53+
return '[' + obj.map(JSON.encode).clean() + ']';
54+
case 'object':
55+
var string = [];
56+
for (var key in obj){
57+
var json = JSON.encode(obj[key]);
58+
if (json) string.push(JSON.encode(key) + ':' + json);
59+
}
60+
return '{' + string + '}';
61+
case 'number': case 'boolean': return '' + obj;
62+
case 'null': return 'null';
6163
}
6264

63-
});
65+
return null;
66+
};
67+
68+
JSON.decode = function(string, secure){
69+
if (!string || typeOf(string) != 'string') return null;
70+
71+
if (secure || JSON.secure){
72+
if (JSON.parse) return JSON.parse(string);
73+
if (!JSON.validate(string)) throw new Error('JSON could not decode the input; security is enabled and the value is not secure.');
74+
}
75+
76+
return eval('(' + string + ')');
77+
};
78+
79+
})();

0 commit comments

Comments
 (0)