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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « src/json-stringifier.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 (function testBasic() {
6 var stack = [];
7 var object = {a: false};
8 var replaced = {a: false, replaced: true};
9
10 function replacer(key, value) {
11 stack.push({ holder: this, key, value });
12 if (stack.length === 1) return replaced;
13 if (key === "a") return true;
14 return value;
15 }
16
17 assertEquals(`{"a":true,"replaced":true}`, JSON.stringify(object, replacer));
18
19 assertEquals([
20 {
21 holder: { "": { a: false } },
22 key: "",
23 value: { a: false }
24 },
25 {
26 holder: { a: false, replaced: true },
27 key: "a",
28 value: false
29 },
30 {
31 holder: { a: false, replaced: true },
32 key: "replaced",
33 value: true
34 }
35 ], stack);
36
37 assertSame(stack[0].holder[""], object);
38 assertSame(stack[0].value, object);
39 assertSame(stack[1].holder, replaced);
40 assertSame(stack[2].holder, replaced);
41 })();
42
43 (function testToJSON() {
44 var stack = [];
45 var object = {a: false, toJSON };
46 var nested = { toJSON: nestedToJSON };
47 var replaced = {a: false, replaced: true, nested };
48 var toJSONd = {a: false, toJSONd: true }
49 var nestedToJSONd = { nestedToJSONd: true };
50
51 function toJSON(key, value) {
52 return toJSONd;
53 }
54
55 function nestedToJSON(key, value) {
56 return nestedToJSONd;
57 }
58
59 function replacer(key, value) {
60 stack.push({ holder: this, key, value });
61 if (stack.length === 1) return replaced;
62 if (key === "a") return true;
63 return value;
64 }
65
66 assertEquals(`{"a":true,"replaced":true,"nested":{"nestedToJSONd":true}}`,
67 JSON.stringify(object, replacer));
68
69 assertEquals([
70 {
71 holder: { "": { a: false, toJSON: toJSON } },
72 key: "",
73 value: { a: false, toJSONd: true }
74 },
75 {
76 holder: { a: false, replaced: true, nested: { toJSON: nestedToJSON } },
77 key: "a",
78 value: false
79 },
80 {
81 holder: { a: false, replaced: true, nested: { toJSON: nestedToJSON } },
82 key: "replaced",
83 value: true
84 },
85 {
86 holder: { a: false, replaced: true, nested: { toJSON: nestedToJSON } },
87 key: "nested",
88 value: { nestedToJSONd: true }
89 },
90 {
91 holder: { nestedToJSONd: true },
92 key: "nestedToJSONd",
93 value: true
94 }
95 ], stack);
96
97 assertSame(stack[0].holder[""], object);
98 assertSame(stack[0].value, toJSONd);
99 assertSame(stack[1].holder, replaced);
100 assertSame(stack[2].holder, replaced);
101 assertSame(stack[3].holder, replaced);
102 assertSame(stack[3].value, nestedToJSONd);
103 assertSame(stack[4].holder, nestedToJSONd);
104 })();
OLDNEW
« 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