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 ) == '"asd\\u001fasd"' )
25escpad_string = "\"asda\sd"
26print ( JSON . stringify ( escpad_string ) == '"\\"asdasd"' )
27print ( JSON . stringify ( true ) === 'true' )
28print ( JSON . stringify ( "foo" ) === '"foo"' )
29print ( JSON . stringify ( null ) === 'null' )
30print ( JSON . stringify ( RegExp ) === undefined )
31print ( JSON . stringify ( new Number ( 1 ) ) === '1' )
32print ( JSON . stringify ( new Boolean ( true ) ) === 'true' )
33print ( JSON . stringify ( new String ( "foo" ) ) === '"foo"' )
34empty_object = { }
35print ( JSON . stringify ( empty_object ) == '{}' )
36empty_object = { }
37empty_object . $ = undefined
38print ( JSON . stringify ( empty_object ) == '{}' )
39p_object = { "a" : 1 , "b" : true , "c" : "foo" , "d" : null , "e" : undefined }
40print ( JSON . stringify ( p_object ) == '{"a":1,"b":true,"c":"foo","d":null}' )
41o_object = { "a" : new Number ( 1 ) , "b" : new Boolean ( true ) , "c" : new String ( "foo" ) }
42print ( JSON . stringify ( o_object ) == '{"a":1,"b":true,"c":"foo"}' )
43child = { "a" : 1 , "b" : new String ( "\nfoo" ) , $ : undefined }
44parent = { "a" : true , "b" : child , "c" : null }
45print ( JSON . stringify ( parent ) == '{"a":true,"b":{"a":1,"b":"\\nfoo"},"c":null}' )
46recursive_object = { }
47recursive_object . $ = 0
48recursive_object . b = recursive_object
49try { JSON . stringify ( recursive_object )
50$ ( ')' )
51} catch ( e ) { print ( e instanceof TypeError )
52}
53empty_array = [ ]
54print ( JSON . stringify ( empty_array ) == '[]' )
55array = [ undefined ]
56print ( JSON . stringify ( array ) == '[null]' )
57p_array = [ 1 , true , "foo" , null , undefined ]
58print ( JSON . stringify ( p_array ) == '[1,true,"foo",null,null]' )
59o_array = [ new Number ( 1 ) , new Boolean ( true ) , new String ( "foo" ) ]
60print ( JSON . stringify ( o_array ) == '[1,true,"foo"]' )
61child = [ 1 , new String ( "\nfoo" ) , undefined ]
62parent = [ true , child , null ]
63print ( JSON . stringify ( parent ) == '[true,[1,"\\nfoo",null],null]' )
64recursive_array = [ ]
65recursive_array [ 0 ] = 2
66recursive_array [ 1 ] = recursive_array
67try { JSON . stringify ( recursive_array )
68$ ( $ )
69} catch ( e ) { print ( e instanceof TypeError )
70}
71object = { "a" : 1 , "b" : [ 1 , true , { "a" : "foo" } ] }
72print ( JSON . stringify ( object ) == '{"a":1,"b":[1,true,{"a":"foo"}]}' )
73object = { "a" : [ 1 ] , "b" : { } }
74print ( JSON . stringify ( object ) === '{"a":[1],"b":{}}' )
75array = [ 1 , { "a" : 2 , "b" : true , c : [ 3 ] } ]
76print ( JSON . stringify ( array ) == '[1,{"a":2,"b":true,"c":[3]}]' )
77to_json_object = { }
78to_json_object . $ = 0
79to_json_object . $ = function ( ) {
80}
81var v1 = ( new Int8Array ( 149 ) ) . subarray ( 78 )
82function replacer_function ( $ , value ) { if ( typeof ( value ) == "string" ) return "FOO"
83return value
84} object = { "a" : "JSON" , "b" : new String ( "JSON" ) , "c" : 3 }
85print ( JSON . stringify ( object , replacer_function ) == '{"a":"FOO","b":"JSON","c":3}' )
86filter = [ "a" , "b" ]
87print ( JSON . stringify ( object , filter ) == '{"a":"JSON","b":"JSON"}' )
88print ( JSON . stringify ( [ ] , [ "" , 4 ] ) === '[]' )
89number = new Number ( 2.2 )
90number . toString = { }
91new Promise ( isFinite . toString ) . catch ( Promise . prototype . then )
92try { } catch ( $ ) { $ ( $ instanceof $ )
93}
94function replacer_thrower ( ) { throw new ReferenceError ( "foo" )
95} try { JSON . stringify ( object , replacer_thrower )
96} catch ( e ) { print ( e . message === "foo" )
97print ( e instanceof ReferenceError )
98}
99object = { "a" : 2 }
100print ( JSON . stringify ( object , 3 ) == '{"a":2}' )
101print ( JSON . stringify ( object , 0 ) == '{"a":2}' )
102print ( JSON . stringify ( object , 0 ) == '{"a":2}' )
103print ( JSON . stringify ( object , undefined ) == '{"a":2}' )
104print ( JSON . stringify ( object , 0 ) == '{"a":2}' )
105print ( JSON . stringify ( object , new Boolean ( 0 ) ) == '{"a":2}' )
106print ( JSON . stringify ( object , 0 , "asd\u20400123456789" ) == '{\nasd⁀012345"a": 2\n}' )
107print ( JSON . stringify ( object , 0 , 100 ) == '{\n          "a": 2\n}' )
108print ( JSON . stringify ( object , 0 , - 5 ) == '{"a":2}' )
109array = [ 2 ]
110print ( JSON . stringify ( array , 0 , "   " ) == '[\n   2\n]' )
111print ( JSON . stringify ( array , 0 , "asd" ) == '[\nasd2\n]' )
112print ( JSON . stringify ( array , 0 , "asd0123456789" ) == '[\nasd01234562\n]' )
113print ( JSON . stringify ( array , 0 , "asd\u20400123456789" ) == '[\nasd⁀0123452\n]' )
114print ( JSON . stringify ( array , 0 , 100 ) == '[\n          2\n]' )
115print ( JSON . stringify ( array , 0 , - 5 ) == '[2]' )
116nested_object = { do : 2 , let : this }
117print ( JSON . stringify ( nested_object , 0 , 2 ) == '{\n  "a": 2,\n  "b": {\n    "c": 1,\n    "d": true\n  }\n}' )
118$ = [ $ , [ $ ] ]
119$ ( $ . $ ( nested_array , $ , 2 ) == '[\n  2,\n  [\n    1,\n    true\n  ]\n]' )
120$
121$
122$
123$ ( $ . $ ( $ , 0 , $ ) == $ )
124eval ( "{}/a/g" )
125$ ( $ . $ ( $ , $ , [ $ , $ , 0 ] ) == $ )
126$ ( $ . $ ( $ , $ , { $ : 3 } ) == $ )
127