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