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

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: 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
« src/json-stringifier.cc ('K') | « 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[1].holder, replaced);
39 assertSame(stack[2].holder, replaced);
40 })();
41
42 (function testToJSON() {
43 var stack = [];
44 var object = {a: false, toJSON };
45 var replaced = {a: false, replaced: true};
46 var toJSONd = {a: false, toJSONd: true }
47
48 function toJSON(key, value) {
49 return toJSONd;
50 }
51
52 function replacer(key, value) {
53 stack.push({ holder: this, key, value });
54 if (stack.length === 1) return replaced;
55 if (key === "a") return true;
56 return value;
57 }
58
59 assertEquals(`{"a":true,"replaced":true}`, JSON.stringify(object, replacer));
60
61 assertEquals([
62 {
63 holder: { "": { a: false, toJSON: toJSON } },
64 key: "",
65 value: { a: false, toJSONd: true }
66 },
67 {
68 holder: { a: false, replaced: true },
69 key: "a",
70 value: false
71 },
72 {
73 holder: { a: false, replaced: true },
74 key: "replaced",
75 value: true
76 }
77 ], stack);
78
79 assertSame(stack[0].holder[""], object);
80 assertSame(stack[0].value, toJSONd);
81 assertSame(stack[1].holder, replaced);
82 assertSame(stack[2].holder, replaced);
83 })();
OLDNEW
« src/json-stringifier.cc ('K') | « src/json-stringifier.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698