Skip to content

Commit c1be8f3

Browse files
author
Himanshu
authored
Fixes mdn#8743 - Removes json.parse polyfill (mdn#8745)
1 parent 16e08ce commit c1be8f3

File tree

1 file changed

+0
-101
lines changed
  • files/en-us/web/javascript/reference/global_objects/json/parse

1 file changed

+0
-101
lines changed

files/en-us/web/javascript/reference/global_objects/json/parse/index.md

Lines changed: 0 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -43,107 +43,6 @@ corresponding to the given JSON `text`.
4343

4444
Throws a {{jsxref("SyntaxError")}} exception if the string to parse is not valid JSON.
4545

46-
## Polyfill
47-
48-
```js
49-
// From https://github.com/douglascrockford/JSON-js/blob/master/json2.js
50-
if (typeof JSON.parse !== "function") {
51-
    var rx_one = /^[\],:{}\s]*$/;
52-
    var rx_two = /\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g;
53-
    var rx_three = /"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g;
54-
    var rx_four = /(?:^|:|,)(?:\s*\[)+/g;
55-
    var rx_dangerous = /[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g;
56-
    JSON.parse = function(text, reviver) {
57-
58-
        // The parse method takes a text and an optional reviver function, and returns
59-
        // a JavaScript value if the text is a valid JSON text.
60-
61-
        var j;
62-
63-
        function walk(holder, key) {
64-
65-
            // The walk method is used to recursively walk the resulting structure so
66-
            // that modifications can be made.
67-
68-
            var k;
69-
            var v;
70-
            var value = holder[key];
71-
            if (value && typeof value === "object") {
72-
                for (k in value) {
73-
                    if (Object.prototype.hasOwnProperty.call(value, k)) {
74-
                        v = walk(value, k);
75-
                        if (v !== undefined) {
76-
                            value[k] = v;
77-
                        } else {
78-
                            delete value[k];
79-
                        }
80-
                    }
81-
                }
82-
            }
83-
            return reviver.call(holder, key, value);
84-
        }
85-
86-
        // Parsing happens in four stages. In the first stage, we replace certain
87-
        // Unicode characters with escape sequences. JavaScript handles many characters
88-
        // incorrectly, either silently deleting them, or treating them as line endings.
89-
90-
        text = String(text);
91-
        rx_dangerous.lastIndex = 0;
92-
        if (rx_dangerous.test(text)) {
93-
            text = text.replace(rx_dangerous, function(a) {
94-
                return (
95-
                    "\\u" +
96-
                    ("0000" + a.charCodeAt(0).toString(16)).slice(-4)
97-
                );
98-
            });
99-
        }
100-
101-
        // In the second stage, we run the text against regular expressions that look
102-
        // for non-JSON patterns. We are especially concerned with "()" and "new"
103-
        // because they can cause invocation, and "=" because it can cause mutation.
104-
        // But just to be safe, we want to reject all unexpected forms.
105-
106-
        // We split the second stage into 4 regexp operations in order to work around
107-
        // crippling inefficiencies in IE's and Safari's regexp engines. First we
108-
        // replace the JSON backslash pairs with "@" (a non-JSON character). Second, we
109-
        // replace all simple value tokens with "]" characters. Third, we delete all
110-
        // open brackets that follow a colon or comma or that begin the text. Finally,
111-
        // we look to see that the remaining characters are only whitespace or "]" or
112-
        // "," or ":" or "{" or "}". If that is so, then the text is safe for eval.
113-
114-
        if (
115-
            rx_one.test(
116-
                text
117-
                .replace(rx_two, "@")
118-
                .replace(rx_three, "]")
119-
                .replace(rx_four, "")
120-
            )
121-
        ) {
122-
123-
            // In the third stage we use the eval function to compile the text into a
124-
            // JavaScript structure. The "{" operator is subject to a syntactic ambiguity
125-
            // in JavaScript: it can begin a block or an object literal. We wrap the text
126-
            // in parens to eliminate the ambiguity.
127-
128-
            j = eval("(" + text + ")");
129-
130-
            // In the optional fourth stage, we recursively walk the new structure, passing
131-
            // each name/value pair to a reviver function for possible transformation.
132-
133-
            return (typeof reviver === "function") ?
134-
                walk({
135-
                    "": j
136-
                }, "") :
137-
                j;
138-
        }
139-
140-
        // If the text is not JSON parsable, then a SyntaxError is thrown.
141-
142-
        throw new SyntaxError("JSON.parse");
143-
    };
144-
}
145-
```
146-
14746
## Examples
14847

14948
### Using JSON.parse()

0 commit comments

Comments
 (0)