Debian Codegraph @

js/src/tests/test262/built-ins/JSON/stringify/replacer-function-tojson.js

raw
1 // Copyright (C) 2019 Aleksey Shvayka. All rights reserved.
2 // This code is governed by the BSD license found in the LICENSE file.
3 /*---
4 esid: sec-serializejsonproperty
5 description: >
6 Replacer function is called on result of toJSON method.
7 info: |
8 JSON.stringify ( value [ , replacer [ , space ] ] )
9
10 [...]
11 4. If Type(replacer) is Object, then
12 a. If IsCallable(replacer) is true, then
13 i. Let ReplacerFunction be replacer.
14 [...]
15 12. Return ? SerializeJSONProperty(the empty String, wrapper).
16
17 SerializeJSONProperty ( key, holder )
18
19 [...]
20 2. If Type(value) is Object, then
21 a. Let toJSON be ? Get(value, "toJSON").
22 b. If IsCallable(toJSON) is true, then
23 i. Set value to ? Call(toJSON, value, « key »).
24 3. If ReplacerFunction is not undefined, then
25 a. Set value to ? Call(ReplacerFunction, holder, « key, value »).
26 ---*/
27
28 assert.sameValue(
29 JSON.stringify({
30 toJSON: function() {
31 return 'toJSON';
32 },
33 }, function(_key, value) {
34 return value + '|replacer';
35 }),
36 '"toJSON|replacer"'
37 );
38
39 assert.sameValue(
40 JSON.stringify({
41 toJSON: function() {
42 return {calls: 'toJSON'};
43 },
44 }, function(_key, value) {
45 if (value && value.calls) {
46 value.calls += '|replacer';
47 }
48
49 return value;
50 }),
51 '{"calls":"toJSON|replacer"}'
52 );
53
54 reportCompare(0, 0);