joo

script("json") rev 2

joo
Mar 19th, 2018 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function parse (me, text) {
  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.     // 2018-03-20: Fixed errors with parsing of comments. - J. Crowe
  7.  
  8.     text = text.replace(/\/\/.*($|\n|\r)|\/\*[^]*?\*\/|"(?:\\[^]|[^"])*"/g, function (s) { return s[0]=='"'?s:'' });
  9.     var cx = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
  10.     if (cx.test(text)) text = text.replace(cx, function (a) { return '\\u' + ('0000' + a.charCodeAt(0).toString(16)).slice(-4); });
  11.  
  12.     if (/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, '@').
  13.         replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, ']').
  14.         replace(/(?:^|:|,)(?:\s*\[)+/g, ''))) {
  15.         try { return eval('(' + text + ')'); } catch (error) { return error; }
  16.     }
  17.    
  18.     return null;
  19. }
Add Comment
Please, Sign In to add comment