Debian Codegraph @

js/src/tests/test262/built-ins/JSON/stringify/value-tojson-result.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 Result of toJSON method is stringified.
7 info: |
8 JSON.stringify ( value [ , replacer [ , space ] ] )
9
10 [...]
11 12. Return ? SerializeJSONProperty(the empty String, wrapper).
12
13 SerializeJSONProperty ( key, holder )
14
15 [...]
16 2. If Type(value) is Object, then
17 a. Let toJSON be ? Get(value, "toJSON").
18 b. If IsCallable(toJSON) is true, then
19 i. Set value to ? Call(toJSON, value, « key »).
20 ---*/
21
22 assert.sameValue(
23 JSON.stringify({
24 toJSON: function() { return [false]; },
25 }),
26 '[false]'
27 );
28
29 var arr = [true];
30 arr.toJSON = function() {};
31 assert.sameValue(JSON.stringify(arr), undefined);
32
33 var str = new String('str');
34 str.toJSON = function() { return null; };
35 assert.sameValue(JSON.stringify({key: str}), '{"key":null}');
36
37 var num = new Number(14);
38 num.toJSON = function() { return {key: 7}; };
39 assert.sameValue(JSON.stringify([num]), '[{"key":7}]');
40
41 reportCompare(0, 0);