Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Prototype <= 1.6.1 compatibility. Closes #8.

  • Loading branch information...
commit a326fcd8540796c5472df99ebdfc9222b4a73f61 1 parent 99661ca
@kitcambridge kitcambridge authored
View
12 lib/json3.js
@@ -95,6 +95,9 @@
// methods entirely.
JSON3.stringify(value) === "1" &&
JSON3.stringify([value]) == "[1]" &&
+ // Prototype <= 1.6.1 serializes `[undefined]` as `"[]"` instead of
+ // `"[null]"`.
+ JSON3.stringify([undef]) == "[null]" &&
// YUI 3.0.0b1 fails to serialize `null` literals.
JSON3.stringify(null) == "null" &&
// FF 3.1b1, 2 halts serialization if an array contains a function:
@@ -315,7 +318,8 @@
serialize = function (property, object, callback, properties, whitespace, indentation, stack) {
var value = object[property], className, year, month, date, time, hours, minutes, seconds, milliseconds, results, element, index, length, prefix, any;
if (typeof value == "object" && value) {
- if (getClass.call(value) == "[object Date]" && !isProperty.call(value, "toJSON")) {
+ className = getClass.call(value);
+ if (className == "[object Date]" && !isProperty.call(value, "toJSON")) {
if (value > -1 / 0 && value < 1 / 0) {
// Dates are serialized according to the `Date#toJSON` method
// specified in ES 5.1 section 15.9.5.44. See section 15.9.1.15
@@ -359,7 +363,11 @@
} else {
value = null;
}
- } else if (typeof value.toJSON == "function") {
+ } else if (typeof value.toJSON == "function" && ((className != "[object Number]" && className != "[object String]" && className != "[object Array]") || isProperty.call(value, "toJSON"))) {
+ // Prototype <= 1.6.1 adds non-standard `toJSON` methods to the
+ // `Number`, `String`, `Date`, and `Array` prototypes. JSON 3
+ // ignores all `toJSON` methods on these objects unless they are
+ // defined directly on an instance.
value = value.toJSON(property);
}
}
View
3  test/test_browser.html
@@ -23,6 +23,9 @@
this.curl = { "apiName": "require" };
this.document.write('<script src="../vendor/' + (parameters.amd == "require" ? "require" : "curl") + '.js"><\/script>');
}
+ if (Spec.hasKey(parameters, "prototype")) {
+ this.document.write('<script src="../vendor/prototype.js"><\/script>');
+ }
}).call(this);
</script>
<script>
View
3  test/test_json3.js
@@ -225,7 +225,7 @@
});
testSuite.addTest("`stringify`", function () {
- var expected = 27, value, pattern;
+ var expected = 28, value, pattern;
// Special values.
this.serializes("null", null, "`null` is represented literally");
@@ -237,6 +237,7 @@
this.serializes('"\\\\\\"How\\bquickly\\tdaft\\njumping\\fzebras\\rvex\\""', new String('\\"How\bquickly\tdaft\njumping\fzebras\rvex"'), "All control characters in strings are escaped");
this.serializes("[false,1,\"Kit\"]", [new Boolean, new Number(1), new String("Kit")], "Arrays are serialized recursively");
+ this.serializes("[null]", [void 0], "`[undefined]` is serialized as `[null]`");
// Property enumeration is implementation-dependent.
value = {
View
4,874 vendor/prototype.js
4,874 additions, 0 deletions not shown
Please sign in to comment.
Something went wrong with that request. Please try again.