Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(63)

Unified Diff: test/mjsunit/json-stringify-holder.js

Issue 2328523002: [JSON] call replacer function with correct holder in JSON.stringify (Closed)
Patch Set: Nits and more tests Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/json-stringifier.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/mjsunit/json-stringify-holder.js
diff --git a/test/mjsunit/json-stringify-holder.js b/test/mjsunit/json-stringify-holder.js
new file mode 100644
index 0000000000000000000000000000000000000000..2f06d77095214c0d00703c6bfe994ae6264648c0
--- /dev/null
+++ b/test/mjsunit/json-stringify-holder.js
@@ -0,0 +1,104 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+(function testBasic() {
+ var stack = [];
+ var object = {a: false};
+ var replaced = {a: false, replaced: true};
+
+ function replacer(key, value) {
+ stack.push({ holder: this, key, value });
+ if (stack.length === 1) return replaced;
+ if (key === "a") return true;
+ return value;
+ }
+
+ assertEquals(`{"a":true,"replaced":true}`, JSON.stringify(object, replacer));
+
+ assertEquals([
+ {
+ holder: { "": { a: false } },
+ key: "",
+ value: { a: false }
+ },
+ {
+ holder: { a: false, replaced: true },
+ key: "a",
+ value: false
+ },
+ {
+ holder: { a: false, replaced: true },
+ key: "replaced",
+ value: true
+ }
+ ], stack);
+
+ assertSame(stack[0].holder[""], object);
+ assertSame(stack[0].value, object);
+ assertSame(stack[1].holder, replaced);
+ assertSame(stack[2].holder, replaced);
+})();
+
+(function testToJSON() {
+ var stack = [];
+ var object = {a: false, toJSON };
+ var nested = { toJSON: nestedToJSON };
+ var replaced = {a: false, replaced: true, nested };
+ var toJSONd = {a: false, toJSONd: true }
+ var nestedToJSONd = { nestedToJSONd: true };
+
+ function toJSON(key, value) {
+ return toJSONd;
+ }
+
+ function nestedToJSON(key, value) {
+ return nestedToJSONd;
+ }
+
+ function replacer(key, value) {
+ stack.push({ holder: this, key, value });
+ if (stack.length === 1) return replaced;
+ if (key === "a") return true;
+ return value;
+ }
+
+ assertEquals(`{"a":true,"replaced":true,"nested":{"nestedToJSONd":true}}`,
+ JSON.stringify(object, replacer));
+
+ assertEquals([
+ {
+ holder: { "": { a: false, toJSON: toJSON } },
+ key: "",
+ value: { a: false, toJSONd: true }
+ },
+ {
+ holder: { a: false, replaced: true, nested: { toJSON: nestedToJSON } },
+ key: "a",
+ value: false
+ },
+ {
+ holder: { a: false, replaced: true, nested: { toJSON: nestedToJSON } },
+ key: "replaced",
+ value: true
+ },
+ {
+ holder: { a: false, replaced: true, nested: { toJSON: nestedToJSON } },
+ key: "nested",
+ value: { nestedToJSONd: true }
+ },
+ {
+ holder: { nestedToJSONd: true },
+ key: "nestedToJSONd",
+ value: true
+ }
+ ], stack);
+
+ assertSame(stack[0].holder[""], object);
+ assertSame(stack[0].value, toJSONd);
+ assertSame(stack[1].holder, replaced);
+ assertSame(stack[2].holder, replaced);
+ assertSame(stack[3].holder, replaced);
+ assertSame(stack[3].value, nestedToJSONd);
+ assertSame(stack[4].holder, nestedToJSONd);
+})();
« no previous file with comments | « src/json-stringifier.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698