space-string-object.js (1186B)
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 String exotic objects are converted to primitives using ToString. 7 info: | 8 JSON.stringify ( value [ , replacer [ , space ] ] ) 9 10 [...] 11 5. If Type(space) is Object, then 12 [...] 13 b. Else if space has a [[StringData]] internal slot, then 14 i. Set space to ? ToString(space). 15 ---*/ 16 17 var obj = { 18 a1: { 19 b1: [1, 2, 3, 4], 20 b2: { 21 c1: 1, 22 c2: 2, 23 }, 24 }, 25 a2: 'a2', 26 }; 27 28 assert.sameValue( 29 JSON.stringify(obj, null, new String('xxx')), 30 JSON.stringify(obj, null, 'xxx') 31 ); 32 33 var str = new String('xxx'); 34 str.toString = function() { return '---'; }; 35 str.valueOf = function() { throw new Test262Error('should not be called'); }; 36 37 assert.sameValue( 38 JSON.stringify(obj, null, str), 39 JSON.stringify(obj, null, '---') 40 ); 41 42 var abrupt = new String('xxx'); 43 abrupt.toString = function() { throw new Test262Error(); }; 44 abrupt.valueOf = function() { throw new Test262Error(); }; 45 46 assert.throws(Test262Error, function() { 47 JSON.stringify(obj, null, abrupt); 48 }); 49 50 reportCompare(0, 0);