joo

script("json") rev 1

joo
Mar 16th, 2018 (edited)
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function parse (me, text, cx) {
  2.     // Parses a JSON string using eval(), after performing security checks.
  3.     // Returns the parsed object if successful, the resulting Error if one is thrown, or null.
  4.     // Extracted from http://json.org/json2.js
  5.     // 2018-03-16: Modified to allow C-style single-line and multi-line comments. - J. Crowe
  6.  
  7.     var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  8.     if (cx.test(text)) text = text.replace(cx, function (a) { return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); });
  9.  
  10.     if (/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
  11.         replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
  12.         replace(/(?:^|:|,)(?:\s*\[)+/g, '').
  13.         replace(/\/\/.*$|\/\*.*?\*\//gm, ''))) {
  14.         try { return eval('(' + text + ')'); } catch (error) { return error; }
  15.     }
  16.    
  17.     return null;
  18. }
Add Comment
Please, Sign In to add comment