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