space-number-object.js (1140B)
1 // Copyright (C) 2012 Ecma International. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 /*--- 4 esid: sec-json.stringify 5 description: > 6 Number objects are converted to primitives using ToNumber. 7 info: | 8 JSON.stringify ( value [ , replacer [ , space ] ] ) 9 10 [...] 11 5. If Type(space) is Object, then 12 a. If space has a [[NumberData]] internal slot, then 13 i. Set space to ? ToNumber(space). 14 ---*/ 15 16 var obj = { 17 a1: { 18 b1: [1, 2, 3, 4], 19 b2: { 20 c1: 1, 21 c2: 2, 22 }, 23 }, 24 a2: 'a2', 25 }; 26 27 assert.sameValue( 28 JSON.stringify(obj, null, new Number(1)), 29 JSON.stringify(obj, null, 1) 30 ); 31 32 var num = new Number(1); 33 num.toString = function() { throw new Test262Error('should not be called'); }; 34 num.valueOf = function() { return 3; }; 35 36 assert.sameValue( 37 JSON.stringify(obj, null, num), 38 JSON.stringify(obj, null, 3) 39 ); 40 41 var abrupt = new Number(4); 42 abrupt.toString = function() { throw new Test262Error(); }; 43 abrupt.valueOf = function() { throw new Test262Error(); }; 44 45 assert.throws(Test262Error, function() { 46 JSON.stringify(obj, null, abrupt); 47 }); 48 49 reportCompare(0, 0);