| 6926 | // Internal: Determines whether the native `JSON.stringify` and `parse` |
| 6927 | // implementations are spec-compliant. Based on work by Ken Snyder. |
| 6928 | function has(name) { |
| 6929 | if (has[name] !== undef) { |
| 6930 | // Return cached feature test result. |
| 6931 | return has[name]; |
| 6932 | } |
| 6933 | var isSupported; |
| 6934 | if (name == "bug-string-char-index") { |
| 6935 | // IE <= 7 doesn't support accessing string characters using square |
| 6936 | // bracket notation. IE 8 only supports this for primitives. |
| 6937 | isSupported = "a" [0] != "a"; |
| 6938 | } else if (name == "json") { |
| 6939 | // Indicates whether both `JSON.stringify` and `JSON.parse` are |
| 6940 | // supported. |
| 6941 | isSupported = has("json-stringify") && has("json-parse"); |
| 6942 | } else { |
| 6943 | var value, |
| 6944 | serialized = '{"a":[1,true,false,null,"\\u0000\\b\\n\\f\\r\\t"]}'; |
| 6945 | // Test `JSON.stringify`. |
| 6946 | if (name == "json-stringify") { |
| 6947 | var stringify = exports.stringify, |
| 6948 | stringifySupported = typeof stringify == "function" && isExtended; |
| 6949 | if (stringifySupported) { |
| 6950 | // A test function object with a custom `toJSON` method. |
| 6951 | (value = function() { |
| 6952 | return 1; |
| 6953 | }).toJSON = value; |
| 6954 | try { |
| 6955 | stringifySupported = |
| 6956 | // Firefox 3.1b1 and b2 serialize string, number, and boolean |
| 6957 | // primitives as object literals. |
| 6958 | stringify(0) === "0" && |
| 6959 | // FF 3.1b1, b2, and JSON 2 serialize wrapped primitives as object |
| 6960 | // literals. |
| 6961 | stringify(new Number()) === "0" && stringify(new String()) == '""' && |
| 6962 | // FF 3.1b1, 2 throw an error if the value is `null`, `undefined`, or |
| 6963 | // does not define a canonical JSON representation (this applies to |
| 6964 | // objects with `toJSON` properties as well, *unless* they are nested |
| 6965 | // within an object or array). |
| 6966 | stringify(getClass) === undef && |
| 6967 | // IE 8 serializes `undefined` as `"undefined"`. Safari <= 5.1.7 and |
| 6968 | // FF 3.1b3 pass this test. |
| 6969 | stringify(undef) === undef && |
| 6970 | // Safari <= 5.1.7 and FF 3.1b3 throw `Error`s and `TypeError`s, |
| 6971 | // respectively, if the value is omitted entirely. |
| 6972 | stringify() === undef && |
| 6973 | // FF 3.1b1, 2 throw an error if the given value is not a number, |
| 6974 | // string, array, object, Boolean, or `null` literal. This applies to |
| 6975 | // objects with custom `toJSON` methods as well, unless they are nested |
| 6976 | // inside object or array literals. YUI 3.0.0b1 ignores custom `toJSON` |
| 6977 | // methods entirely. |
| 6978 | stringify(value) === "1" && stringify([value]) == "[1]" && |
| 6979 | // Prototype <= 1.6.1 serializes `[undefined]` as `"[]"` instead of |
| 6980 | // `"[null]"`. |
| 6981 | stringify([undef]) == "[null]" && |
| 6982 | // YUI 3.0.0b1 fails to serialize `null` literals. |
| 6983 | stringify(null) == "null" && |
| 6984 | // FF 3.1b1, 2 halts serialization if an array contains a function: |
| 6985 | // `[1, true, getClass, 1]` serializes as "[1,true,],". FF 3.1b3 |