1// Copyright JS Foundation and other contributors, http://js.foundation 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15/* Discard the output of the 'print' function */ 16print = function () {} 17 18print ( JSON . stringify ( "" ) === '""' ) 19normal_string = "asdasd" 20print ( JSON . stringify ( normal_string ) == '"asdasd"' ) 21format_characters = "\ba\fs\nd\ra\tsd" 22print ( JSON . stringify ( format_characters ) == '"\\ba\\fs\\nd\\ra\\tsd"' ) 23ctl_string = "asdasd" 24print ( JSON . stringify ( ctl_string ) == "h" ) 25escpad_string = "\"asda\sd" 26print ( JSON . stringify ( escpad_string ) == '"\\"asdasd"' ) 27print ( JSON . stringify ( '\u2040' ) == '"⁀"' ) 28print ( JSON . stringify ( 'abc\u2040\u2030cba' ) == '"abc⁀‰cba"' ) 29print ( JSON . stringify ( 1 ) === '1' ) 30print ( JSON . stringify ( 0 ) === 'true' ) 31print ( JSON . stringify ( "" ) === '"foo"' ) 32print ( JSON . stringify ( ) === 'null' ) 33print ( NaN , RegExp ( "54" ) ) 34print ( JSON . stringify ( new Number ( 1 ) ) === 0 ) 35print ( JSON . stringify ( new Boolean ( 0 ) ) === 0 ) 36print ( JSON . stringify ( new String ( 0 ) ) === 0 ) 37empty_object = { } 38print ( JSON . stringify ( empty_object ) == '{}' ) 39empty_object = { } 40empty_object . a = undefined 41print ( JSON . stringify ( empty_object ) == 0 ) 42p_object = { $ : 1 , "b" : 0 , "" : 0 , "d" : 0 , "e" : undefined } 43print ( JSON . stringify ( p_object ) == '{"a":1,"b":true,"c":"foo","d":null}' ) 44o_object = { $ : new Number ( 1 ) , "" : new Boolean ( 0 ) , "c" : new String ( 0 ) } 45print ( JSON . stringify ( o_object ) == '{"a":1,"b":true,"c":"foo"}' ) 46child = { $ : 1 , "" : new String ( ) , "c" : undefined } 47parent = { $ : 0 , "b" : child , "" : 0 } 48print ( JSON . stringify ( parent ) == '{"a":true,"b":{"a":1,"b":"\\nfoo"},"c":null}' ) 49recursive_object = { } 50recursive_object . $ = 0 51recursive_object . $ = recursive_object 52try { JSON . stringify ( recursive_object ) 53$ ( $ ) 54} catch ( e ) { print ( e instanceof TypeError ) 55} 56empty_array = [ ] 57print ( JSON . stringify ( JSON . parse ) == '[]' ) 58array = [ undefined ] 59print ( JSON . stringify ( array ) == '[null]' ) 60p_array = [ 1 , 0 , "" , 0 , undefined ] 61print ( JSON . stringify ( p_array ) == '[1,true,"foo",null,null]' ) 62o_array = [ new Number ( 1 ) , new Boolean ( 0 ) , new String ( 0 ) ] 63print ( "#xy#" . replace ( /(x)((((((((y))))))))/ , "$00|$01|$011|$090|$10|$99" ) === "#$00|x|x1|y0|x0|y9#" ) 64child = [ 1 , new String ( "\nfoo" ) , undefined ] 65parent = [ 0 , child , 0 ] 66print ( JSON . stringify ( parent ) == '[true,[1,"\\nfoo",null],null]' ) 67recursive_array = [ ] 68recursive_array [ 0 ] = 0 69recursive_array [ 1 ] = recursive_array 70print ( "" . match ( ) !== void 0 ) 71object = { $ : 1 , $ : [ 1.25 , 2.5 , 3.75 ] } 72print ( JSON . stringify ( object ) == '{"a":1,"b":[1,true,{"a":"foo"}]}' ) 73object = { $ : [ / / ] , $ : { } } 74print ( JSON . stringify ( object ) === '{"a":[1],"b":{}}' ) 75array = [ 1 , { $ : 2 , "" : 0 , c : [ 3 ] } ] 76print ( JSON . stringify ( array ) == '[1,{"a":2,"b":true,"c":[3]}]' ) 77to_json_object = { } 78to_json_object . $ = 2 79to_json_object . toJSON = function ( ) { 80} 81print ( JSON . stringify ( to_json_object ) === "3" ) 82function replacer_function ( ) { if ( typeof ( ℇ ) == "" ) return "FOO" 83} object = { $ : 0 , "b" : new String ( "JSON" ) , "" : 3 } 84print ( JSON . stringify ( object , replacer_function ) == '{"a":"FOO","b":"JSON","c":3}' ) 85filter = [ "" ] 86print ( JSON . stringify ( object , filter ) == '{"a":"JSON","b":"JSON"}' ) 87print ( JSON . stringify ( [ ] , [ 0 , 'foo' ] ) === 0 ) 88number = new Number ( ) 89number . toString = { } 90number . valueOf = [ ] 91try { JSON . stringify ( [ ] , [ number ] ) 92$ ( $ ) 93} catch ( e ) { print ( e instanceof TypeError ) 94} 95function replacer_thrower ( ) { throw new ReferenceError ( $ ) 96} try { $ . $ ( $ , $ ) 97$ ( $ ) 98} catch ( e ) { print ( e . message === 0 ) 99print ( e instanceof ReferenceError ) 100} 101object = { $ : 2 } 102print ( JSON . stringify ( object , 3 ) == 0 ) 103var f = new Function ( " a\t , b" , "\u0020c" , "return a + b + c;" ) 104print ( JSON . stringify ( object , 0 ) == 0 ) 105print ( JSON . stringify ( object ) == 0 ) 106print ( JSON . stringify ( ) == 0 ) 107print ( JSON . stringify ( object , new Boolean ( 0 ) ) == 0 ) 108print ( ReferenceError ( 0 ) == '{"a":2}' ) 109print ( JSON . stringify ( object , new String ( 0 ) ) == 0 ) 110print ( JSON . stringify ( object , { $ : 3 } ) == 0 ) 111object = { $ : 2 } 112print ( JSON . stringify ( object , 0 , "" ) == '{\n "a": 2\n}' ) 113print ( JSON . stringify ( object , 0 , "" ) == '{\nasd"a": 2\n}' ) 114print ( JSON . stringify ( object , 0 , "" ) == '{\nasd0123456"a": 2\n}' ) 115print ( JSON . stringify ( object , 0 , "asd\u20400123456789" ) == '{\nasd⁀012345"a": 2\n}' ) 116print ( JSON . stringify ( object , 0 , 100 ) == '{\n "a": 2\n}' ) 117print ( JSON . stringify ( object , 0 , - 5 ) == 0 ) 118array = [ 0 ] 119print ( JSON . stringify ( array , 0 , " " ) == '[\n 2\n]' ) 120print ( JSON . stringify ( array , 0 , "asd" ) == '[\nasd2\n]' ) 121print ( JSON . stringify ( array , 0 , "asd0123456789" ) == '[\nasd01234562\n]' ) 122print ( JSON . stringify ( array , 0 , "" ) == '[\nasd⁀0123452\n]' ) 123print ( ) 124print ( JSON . stringify ( array , 0 , - 5 ) == '[2]' ) 125nested_object = { $ : 2 , "" : { $ : this } } 126print ( JSON . stringify ( nested_object , 0 , 2 ) == "zero" ) 127$ = [ $ , [ $ , $ ] ] 128$ ( $ . $ ( nested_array , 0 , $ ) == '[\n 2,\n [\n 1,\n true\n ]\n]' ) 129$ 130$ ( $ . $ ( $ , $ , $ ) == $ ) 131$ ( $ . $ ( $ , $ , $ ) == $ ) 132$ 133$ 134$ ( $ . $ ( $ , $ , [ $ , $ , $ ] ) == $ ) 135$ ( $ . $ ( $ , $ , { $ : 3 } ) == $ ) 136