Path: blob/master/webroot/rsrc/externals/javelin/lib/JSON.js
12242 views
/**1* Simple JSON serializer.2*3* @requires javelin-install4* @provides javelin-json5* @javelin6*/78/**9* JSON serializer and parser. This class uses the native JSON parser if it is10* available; if not, it provides an eval-based parser and a simple serializer.11*12* NOTE: This class uses eval() on some systems, without sanitizing input. It is13* not safe to use with untrusted data. Javelin does not provide a library14* suitable for parsing untrusted JSON.15*16* Usage is straightforward:17*18* JX.JSON.stringify({"bees":"knees"}); // Returns string: '{"bees":"knees"}'19* JX.JSON.parse('{"bees":"knees"}'); // Returns object: {"bees":"knees"}20*21* @task json JSON Manipulation22* @task internal Internal23*/24JX.install('JSON', {25statics : {262728/* -( JSON Manipulation )-------------------------------------------------- */293031/**32* Parse a **trusted** JSON string into an object. Accepts a valid JSON33* string and returns the object it encodes.34*35* NOTE: This method does not sanitize input and uses an eval-based parser36* on some systems. It is **NOT SAFE** to use with untrusted inputs.37*38* @param string A valid, trusted JSON string.39* @return object The object encoded by the JSON string.40* @task json41*/42parse : function(data) {43if (typeof data != 'string') {44return null;45}4647if (window.JSON && JSON.parse) {48var obj;49try {50obj = JSON.parse(data);51} catch (e) {}52return obj || null;53}5455return eval('(' + data + ')');56},5758/**59* Serialize an object into a JSON string. Accepts an object comprised of60* maps, lists and scalars and transforms it into a JSON representation.61* This method has undefined behavior if you pass in other complicated62* things, e.g. object graphs containing cycles, document.body, or Date63* objects.64*65* @param object An object comprised of maps, lists and scalars.66* @return string JSON representation of the object.67* @task json68*/69stringify : function(val) {70if (window.JSON && JSON.stringify) {71return JSON.stringify(val);72}7374var out = [];75if (76val === null || val === true || val === false || typeof val == 'number'77) {78return '' + val;79}8081if (val.push && val.pop) {82var v;83for (var ii = 0; ii < val.length; ii++) {8485// For consistency with JSON.stringify(), encode undefined array86// indices as null.87v = (typeof val[ii] == 'undefined') ? null : val[ii];8889out.push(JX.JSON.stringify(v));90}91return '[' + out.join(',') + ']';92}9394if (typeof val == 'string') {95return JX.JSON._esc(val);96}9798for (var k in val) {99out.push(JX.JSON._esc(k) + ':' + JX.JSON.stringify(val[k]));100}101return '{' + out.join(',') + '}';102},103104105/* -( Internal )----------------------------------------------------------- */106107108// Lifted more or less directly from Crockford's JSON2.109_escexp : /[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,110111// List of control character escape codes.112_meta : {113'\b' : '\\b',114'\t' : '\\t',115'\n' : '\\n',116'\f' : '\\f',117'\r' : '\\r',118'"' : '\\"',119'\\' : '\\\\'120},121122/**123* Quote and escape a string for inclusion in serialized JSON. Finds124* characters in the string which need to be escaped and uses125* @{method:_replace} to escape them.126*127* @param string Unescaped string.128* @return string Escaped string.129* @task internal130*/131_esc : function(str) {132JX.JSON._escexp.lastIndex = 0;133return JX.JSON._escexp.test(str) ?134'"' + str.replace(JX.JSON._escexp, JX.JSON._replace) + '"' :135'"' + str + '"';136},137138/**139* Helper callback for @{method:_esc}, escapes characters which can't be140* represented normally in serialized JSON.141*142* @param string Unescaped character.143* @return string Escaped character.144* @task internal145*/146_replace : function(m) {147if (m in JX.JSON._meta) {148return JX.JSON._meta[m];149}150return '\\u' + (('0000' + m.charCodeAt(0).toString(16)).slice(-4));151}152}153});154155156