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// Checking quoting strings 16assert (JSON.stringify ("") === '""'); 17 18normal_string = "asdasd"; 19assert (JSON.stringify (normal_string) == '"asdasd"'); 20 21format_characters = "\ba\fs\nd\ra\tsd"; 22assert (JSON.stringify (format_characters) == '"\\ba\\fs\\nd\\ra\\tsd"'); 23 24ctl_string = "asdasd"; 25assert (JSON.stringify (ctl_string) == '"asd\\u001fasd"'); 26 27escpad_string = "\"asda\sd"; 28assert (JSON.stringify (escpad_string) == '"\\"asdasd"'); 29 30assert (JSON.stringify('\u2040') == '"⁀"'); 31assert (JSON.stringify('abc\u2040\u2030cba') == '"abc⁀‰cba"'); 32 33// Checking primitive types 34assert (JSON.stringify (1) === '1'); 35assert (JSON.stringify (true) === 'true'); 36assert (JSON.stringify ("foo") === '"foo"'); 37assert (JSON.stringify (null) === 'null'); 38assert (JSON.stringify (undefined) === undefined); 39 40assert (JSON.stringify (new Number(1)) === '1'); 41assert (JSON.stringify (new Boolean(true)) === 'true'); 42assert (JSON.stringify (new String("foo")) === '"foo"'); 43 44// Checking objects 45empty_object = {} 46assert (JSON.stringify (empty_object) == '{}'); 47 48empty_object = {}; 49empty_object.a = undefined; 50 51assert (JSON.stringify (empty_object) == '{}'); 52 53p_object = { "a": 1, "b": true, "c": "foo", "d": null, "e": undefined }; 54assert (JSON.stringify (p_object) == '{"a":1,"b":true,"c":"foo","d":null}'); 55 56o_object = { "a": new Number(1), "b": new Boolean(true), "c": new String("foo") }; 57assert (JSON.stringify (o_object) == '{"a":1,"b":true,"c":"foo"}'); 58 59child = { "a": 1, "b": new String("\nfoo"), "c": undefined }; 60parent = { "a": true, "b": child, "c": null}; 61 62assert (JSON.stringify (parent) == '{"a":true,"b":{"a":1,"b":"\\nfoo"},"c":null}'); 63 64recursive_object = {}; 65recursive_object.a = 2; 66recursive_object.b = recursive_object; 67 68try { 69 JSON.stringify (recursive_object) 70 // Should not be reached. 71 assert (false); 72} catch (e) { 73 assert (e instanceof TypeError); 74} 75 76// Checking arrays 77empty_array = []; 78assert (JSON.stringify (empty_array) == '[]'); 79 80array = [undefined]; 81assert (JSON.stringify (array) == '[null]'); 82 83p_array = [1, true, "foo", null, undefined]; 84assert (JSON.stringify (p_array) == '[1,true,"foo",null,null]'); 85 86o_array = [new Number(1), new Boolean(true), new String("foo")]; 87assert (JSON.stringify (o_array) == '[1,true,"foo"]'); 88 89child = [ 1, new String("\nfoo"), undefined ]; 90parent = [ true, child, null ]; 91 92assert (JSON.stringify (parent) == '[true,[1,"\\nfoo",null],null]'); 93 94recursive_array = []; 95recursive_array[0] = 2; 96recursive_array[1] = recursive_array; 97 98try { 99 JSON.stringify (recursive_array) 100 // Should not be reached. 101 assert (false); 102} catch (e) { 103 assert (e instanceof TypeError); 104} 105 106object = {"a": 1, "b": [1, true, {"a": "foo"}]}; 107assert (JSON.stringify (object) == '{"a":1,"b":[1,true,{"a":"foo"}]}'); 108 109object = {"a": [1], "b": {}}; 110assert (JSON.stringify(object) === '{"a":[1],"b":{}}'); 111 112array = [1, {"a": 2, "b": true, c: [3]}]; 113assert (JSON.stringify (array) == '[1,{"a":2,"b":true,"c":[3]}]'); 114 115// Filtering / replacing 116to_json_object = {}; 117to_json_object.a = 2; 118to_json_object.toJSON = function (key) { return 3; }; 119 120assert (JSON.stringify (to_json_object) === "3"); 121 122function replacer_function (key, value) 123{ 124 if (typeof(value) == "string") 125 return "FOO"; 126 return value; 127} 128 129object = { "a": "JSON", "b": new String("JSON"), "c": 3 }; 130assert (JSON.stringify (object, replacer_function) == '{"a":"FOO","b":"JSON","c":3}'); 131 132filter = ["a", "b"]; 133assert (JSON.stringify (object, filter) == '{"a":"JSON","b":"JSON"}'); 134 135assert (JSON.stringify ([], [ 'foo', 'foo' ]) === '[]'); 136 137number = new Number(2.2); 138number.toString = {}; 139number.valueOf = []; 140 141try 142{ 143 JSON.stringify([], [number]); 144 // Should not be reached. 145 assert (false); 146} 147catch (e) 148{ 149 assert (e instanceof TypeError); 150} 151 152// Throw error in the replacer function 153function replacer_thrower (key, value) 154{ 155 throw new ReferenceError("foo"); 156 return value; 157} 158 159try { 160 JSON.stringify (object, replacer_thrower) 161 // Should not be reached. 162 assert (false); 163} catch (e) { 164 assert (e.message === "foo"); 165 assert (e instanceof ReferenceError); 166} 167 168// Checking replacer with different primitive types 169object = { "a": 2 }; 170assert (JSON.stringify (object, 3) == '{"a":2}'); 171assert (JSON.stringify (object, true) == '{"a":2}'); 172assert (JSON.stringify (object, null) == '{"a":2}'); 173assert (JSON.stringify (object, undefined) == '{"a":2}'); 174assert (JSON.stringify (object, "foo") == '{"a":2}'); 175 176// Checking replacer with different primitive types 177assert (JSON.stringify (object, new Boolean (true)) == '{"a":2}'); 178assert (JSON.stringify (object, new Number (3)) == '{"a":2}'); 179assert (JSON.stringify (object, new String ("foo")) == '{"a":2}'); 180assert (JSON.stringify (object, { "a": 3 }) == '{"a":2}'); 181 182// Checking JSON formatting 183object = {"a": 2}; 184assert (JSON.stringify (object, null, " ") == '{\n "a": 2\n}'); 185assert (JSON.stringify (object, null, "asd") == '{\nasd"a": 2\n}'); 186assert (JSON.stringify (object, null, "asd0123456789") == '{\nasd0123456"a": 2\n}'); 187assert (JSON.stringify (object, null, "asd\u20400123456789") == '{\nasd⁀012345"a": 2\n}'); 188assert (JSON.stringify (object, null, 100) == '{\n "a": 2\n}'); 189assert (JSON.stringify (object, null, -5) == '{"a":2}'); 190 191array = [2]; 192assert (JSON.stringify (array, null, " ") == '[\n 2\n]'); 193assert (JSON.stringify (array, null, "asd") == '[\nasd2\n]'); 194assert (JSON.stringify (array, null, "asd0123456789") == '[\nasd01234562\n]'); 195assert (JSON.stringify (array, null, "asd\u20400123456789") == '[\nasd⁀0123452\n]'); 196assert (JSON.stringify (array, null, 100) == '[\n 2\n]'); 197assert (JSON.stringify (array, null, -5) == '[2]'); 198 199nested_object = {"a": 2, "b": {"c": 1, "d": true}}; 200assert (JSON.stringify (nested_object, null, 2) == '{\n "a": 2,\n "b": {\n "c": 1,\n "d": true\n }\n}'); 201 202nested_array = [2, [1,true]]; 203assert (JSON.stringify (nested_array, null, 2) == '[\n 2,\n [\n 1,\n true\n ]\n]'); 204 205// Checking space (formatting parameter) with different primititve types 206object = { "a": 2 }; 207assert (JSON.stringify (object, null, true) == '{"a":2}'); 208assert (JSON.stringify (object, null, null) == '{"a":2}'); 209assert (JSON.stringify (object, null, undefined) == '{"a":2}'); 210 211// Checking space (formatting parameter) with different object types 212assert (JSON.stringify (object, null, new Boolean (true)) == '{"a":2}'); 213assert (JSON.stringify (object, null, [1, 2, 3] ) == '{"a":2}'); 214assert (JSON.stringify (object, null, { "a": 3 }) == '{"a":2}'); 215