| 1023 | // Public: Initializes JSON 3 using the given `context` object, attaching the |
| 1024 | // `stringify` and `parse` functions to the specified `exports` object. |
| 1025 | function runInContext(context, exports) { |
| 1026 | context || (context = root["Object"]()); |
| 1027 | exports || (exports = root["Object"]()); |
| 1028 | |
| 1029 | // Native constructor aliases. |
| 1030 | var Number = context["Number"] || root["Number"], |
| 1031 | String = context["String"] || root["String"], |
| 1032 | Object = context["Object"] || root["Object"], |
| 1033 | Date = context["Date"] || root["Date"], |
| 1034 | SyntaxError = context["SyntaxError"] || root["SyntaxError"], |
| 1035 | TypeError = context["TypeError"] || root["TypeError"], |
| 1036 | Math = context["Math"] || root["Math"], |
| 1037 | nativeJSON = context["JSON"] || root["JSON"]; |
| 1038 | |
| 1039 | // Delegate to the native `stringify` and `parse` implementations. |
| 1040 | if (typeof nativeJSON == "object" && nativeJSON) { |
| 1041 | exports.stringify = nativeJSON.stringify; |
| 1042 | exports.parse = nativeJSON.parse; |
| 1043 | } |
| 1044 | |
| 1045 | // Convenience aliases. |
| 1046 | var objectProto = Object.prototype, |
| 1047 | getClass = objectProto.toString, |
| 1048 | isProperty, forEach, undef; |
| 1049 | |
| 1050 | // Test the `Date#getUTC*` methods. Based on work by @Yaffle. |
| 1051 | var isExtended = new Date(-3509827334573292); |
| 1052 | try { |
| 1053 | // The `getUTCFullYear`, `Month`, and `Date` methods return nonsensical |
| 1054 | // results for certain dates in Opera >= 10.53. |
| 1055 | isExtended = isExtended.getUTCFullYear() == -109252 && isExtended.getUTCMonth() === 0 && isExtended.getUTCDate() === 1 && |
| 1056 | // Safari < 2.0.2 stores the internal millisecond time value correctly, |
| 1057 | // but clips the values returned by the date methods to the range of |
| 1058 | // signed 32-bit integers ([-2 ** 31, 2 ** 31 - 1]). |
| 1059 | isExtended.getUTCHours() == 10 && isExtended.getUTCMinutes() == 37 && isExtended.getUTCSeconds() == 6 && isExtended.getUTCMilliseconds() == 708; |
| 1060 | } catch (exception) {} |
| 1061 | |
| 1062 | // Internal: Determines whether the native `JSON.stringify` and `parse` |
| 1063 | // implementations are spec-compliant. Based on work by Ken Snyder. |
| 1064 | function has(name) { |
| 1065 | if (has[name] !== undef) { |
| 1066 | // Return cached feature test result. |
| 1067 | return has[name]; |
| 1068 | } |
| 1069 | var isSupported; |
| 1070 | if (name == "bug-string-char-index") { |
| 1071 | // IE <= 7 doesn't support accessing string characters using square |
| 1072 | // bracket notation. IE 8 only supports this for primitives. |
| 1073 | isSupported = "a"[0] != "a"; |
| 1074 | } else if (name == "json") { |
| 1075 | // Indicates whether both `JSON.stringify` and `JSON.parse` are |
| 1076 | // supported. |
| 1077 | isSupported = has("json-stringify") && has("json-parse"); |
| 1078 | } else { |
| 1079 | var value, serialized = '{"a":[1,true,false,null,"\\u0000\\b\\n\\f\\r\\t"]}'; |
| 1080 | // Test `JSON.stringify`. |
| 1081 | if (name == "json-stringify") { |
| 1082 | var stringify = exports.stringify, stringifySupported = typeof stringify == "function" && isExtended; |