replacer-function-result.js (1192B)
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-serializejsonproperty 5 description: > 6 Result of replacer function 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 3. If ReplacerFunction is not undefined, then 17 a. Set value to ? Call(ReplacerFunction, holder, « key, value »). 18 ---*/ 19 20 var obj = { 21 a1: { 22 b1: [1, 2], 23 b2: { 24 c1: true, 25 c2: false, 26 }, 27 }, 28 a2: 'a2', 29 }; 30 31 var replacer = function(key, value) { 32 assert.sameValue(value, null); 33 34 switch (key) { 35 case '': return {a1: null, a2: null}; 36 case 'a1': return {b1: null, b2: null}; 37 case 'a2': return 'a2'; 38 39 case 'b1': return [null, null]; 40 case 'b2': return {c1: null, c2: null}; 41 42 case '0': return 1; 43 case '1': return 2; 44 case 'c1': return true; 45 case 'c2': return false; 46 } 47 48 throw new Test262Error('unreachable'); 49 }; 50 51 assert.sameValue( 52 JSON.stringify(null, replacer), 53 JSON.stringify(obj) 54 ); 55 56 reportCompare(0, 0);