ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 1 | // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | function GenericToJSONChecks(Constructor, value, alternative) { |
| 29 | var n1 = new Constructor(value); |
| 30 | n1.valueOf = function () { return alternative; }; |
| 31 | assertEquals(alternative, n1.toJSON()); |
| 32 | var n2 = new Constructor(value); |
| 33 | n2.valueOf = null; |
| 34 | assertThrows(function () { n2.toJSON(); }, TypeError); |
| 35 | var n3 = new Constructor(value); |
| 36 | n3.valueOf = function () { return {}; }; |
| 37 | assertThrows(function () { n3.toJSON(); }, TypeError, 'result_not_primitive'); |
| 38 | var n4 = new Constructor(value); |
| 39 | n4.valueOf = function () { |
| 40 | assertEquals(0, arguments.length); |
| 41 | assertEquals(this, n4); |
| 42 | return null; |
| 43 | }; |
| 44 | assertEquals(null, n4.toJSON()); |
| 45 | } |
| 46 | |
| 47 | // Number toJSON |
| 48 | assertEquals(3, (3).toJSON()); |
| 49 | assertEquals(3, (3).toJSON(true)); |
| 50 | assertEquals(4, (new Number(4)).toJSON()); |
| 51 | GenericToJSONChecks(Number, 5, 6); |
| 52 | |
| 53 | // Boolean toJSON |
| 54 | assertEquals(true, (true).toJSON()); |
| 55 | assertEquals(true, (true).toJSON(false)); |
| 56 | assertEquals(false, (false).toJSON()); |
| 57 | assertEquals(true, (new Boolean(true)).toJSON()); |
| 58 | GenericToJSONChecks(Boolean, true, false); |
| 59 | GenericToJSONChecks(Boolean, false, true); |
| 60 | |
| 61 | // String toJSON |
| 62 | assertEquals("flot", "flot".toJSON()); |
| 63 | assertEquals("flot", "flot".toJSON(3)); |
| 64 | assertEquals("tolf", (new String("tolf")).toJSON()); |
| 65 | GenericToJSONChecks(String, "x", "y"); |
| 66 | |
| 67 | // Date toJSON |
| 68 | assertEquals("1970-01-01T00:00:00Z", new Date(0).toJSON()); |
| 69 | assertEquals("1979-01-11T08:00:00Z", new Date("1979-01-11 08:00 GMT").toJSON()); |
| 70 | assertEquals("2005-05-05T05:05:05Z", new Date("2005-05-05 05:05:05 GMT").toJSON()); |
| 71 | var n1 = new Date(10000); |
| 72 | n1.toISOString = function () { return "foo"; }; |
| 73 | assertEquals("foo", n1.toJSON()); |
| 74 | var n2 = new Date(10001); |
| 75 | n2.toISOString = null; |
| 76 | assertThrows(function () { n2.toJSON(); }, TypeError); |
| 77 | var n3 = new Date(10002); |
| 78 | n3.toISOString = function () { return {}; }; |
| 79 | assertThrows(function () { n3.toJSON(); }, TypeError, "result_not_primitive"); |
| 80 | var n4 = new Date(10003); |
| 81 | n4.toISOString = function () { |
| 82 | assertEquals(0, arguments.length); |
| 83 | assertEquals(this, n4); |
| 84 | return null; |
| 85 | }; |
| 86 | assertEquals(null, n4.toJSON()); |
| 87 | |
| 88 | assertEquals(Object.prototype, JSON.__proto__); |
| 89 | assertEquals("[object JSON]", Object.prototype.toString.call(JSON)); |
| 90 | |
| 91 | // DontEnum |
| 92 | for (var p in this) |
| 93 | assertFalse(p == "JSON"); |
| 94 | |
| 95 | // Parse |
| 96 | |
| 97 | assertEquals({}, JSON.parse("{}")); |
| 98 | assertEquals(null, JSON.parse("null")); |
| 99 | assertEquals(true, JSON.parse("true")); |
| 100 | assertEquals(false, JSON.parse("false")); |
| 101 | assertEquals("foo", JSON.parse('"foo"')); |
| 102 | assertEquals("f\no", JSON.parse('"f\\no"')); |
| 103 | assertEquals(1.1, JSON.parse("1.1")); |
| 104 | assertEquals(1, JSON.parse("1.0")); |
| 105 | assertEquals(0.0000000003, JSON.parse("3e-10")); |
| 106 | assertEquals([], JSON.parse("[]")); |
| 107 | assertEquals([1], JSON.parse("[1]")); |
| 108 | assertEquals([1, "2", true, null], JSON.parse('[1, "2", true, null]')); |
| 109 | |
| 110 | function GetFilter(name) { |
| 111 | function Filter(key, value) { |
| 112 | return (key == name) ? undefined : value; |
| 113 | } |
| 114 | return Filter; |
| 115 | } |
| 116 | |
| 117 | var pointJson = '{"x": 1, "y": 2}'; |
| 118 | assertEquals({'x': 1, 'y': 2}, JSON.parse(pointJson)); |
| 119 | assertEquals({'x': 1}, JSON.parse(pointJson, GetFilter('y'))); |
| 120 | assertEquals({'y': 2}, JSON.parse(pointJson, GetFilter('x'))); |
| 121 | assertEquals([1, 2, 3], JSON.parse("[1, 2, 3]")); |
| 122 | assertEquals([1, undefined, 3], JSON.parse("[1, 2, 3]", GetFilter(1))); |
| 123 | assertEquals([1, 2, undefined], JSON.parse("[1, 2, 3]", GetFilter(2))); |
| 124 | |
| 125 | function DoubleNumbers(key, value) { |
| 126 | return (typeof value == 'number') ? 2 * value : value; |
| 127 | } |
| 128 | |
| 129 | var deepObject = '{"a": {"b": 1, "c": 2}, "d": {"e": {"f": 3}}}'; |
| 130 | assertEquals({"a": {"b": 1, "c": 2}, "d": {"e": {"f": 3}}}, |
| 131 | JSON.parse(deepObject)); |
| 132 | assertEquals({"a": {"b": 2, "c": 4}, "d": {"e": {"f": 6}}}, |
| 133 | JSON.parse(deepObject, DoubleNumbers)); |
| 134 | |
| 135 | function TestInvalid(str) { |
| 136 | assertThrows(function () { JSON.parse(str); }, SyntaxError); |
| 137 | } |
| 138 | |
| 139 | TestInvalid('abcdef'); |
| 140 | TestInvalid('isNaN()'); |
| 141 | TestInvalid('{"x": [1, 2, deepObject]}'); |
| 142 | TestInvalid('[1, [2, [deepObject], 3], 4]'); |
| 143 | TestInvalid('function () { return 0; }'); |
| 144 | |
| 145 | TestInvalid("[1, 2"); |
| 146 | TestInvalid('{"x": 3'); |
| 147 | |
| 148 | // Stringify |
| 149 | |
| 150 | assertEquals("true", JSON.stringify(true)); |
| 151 | assertEquals("false", JSON.stringify(false)); |
| 152 | assertEquals("null", JSON.stringify(null)); |
| 153 | assertEquals("false", JSON.stringify({toJSON: function () { return false; }})); |
| 154 | assertEquals("4", JSON.stringify(4)); |
| 155 | assertEquals('"foo"', JSON.stringify("foo")); |
| 156 | assertEquals("null", JSON.stringify(Infinity)); |
| 157 | assertEquals("null", JSON.stringify(-Infinity)); |
| 158 | assertEquals("null", JSON.stringify(NaN)); |
| 159 | assertEquals("4", JSON.stringify(new Number(4))); |
| 160 | assertEquals('"bar"', JSON.stringify(new String("bar"))); |
| 161 | |
| 162 | assertEquals('"foo\\u0000bar"', JSON.stringify("foo\0bar")); |
| 163 | assertEquals('"f\\"o\'o\\\\b\\ba\\fr\\nb\\ra\\tz"', |
| 164 | JSON.stringify("f\"o\'o\\b\ba\fr\nb\ra\tz")); |
| 165 | |
| 166 | assertEquals("[1,2,3]", JSON.stringify([1, 2, 3])); |
| 167 | assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 1)); |
| 168 | assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 2)); |
| 169 | assertEquals("[\n 1,\n 2,\n 3\n]", |
| 170 | JSON.stringify([1, 2, 3], null, new Number(2))); |
| 171 | assertEquals("[\n^1,\n^2,\n^3\n]", JSON.stringify([1, 2, 3], null, "^")); |
| 172 | assertEquals("[\n^1,\n^2,\n^3\n]", |
| 173 | JSON.stringify([1, 2, 3], null, new String("^"))); |
| 174 | assertEquals("[\n 1,\n 2,\n [\n 3,\n [\n 4\n ],\n 5\n ],\n 6,\n 7\n]", |
| 175 | JSON.stringify([1, 2, [3, [4], 5], 6, 7], null, 1)); |
| 176 | assertEquals("[]", JSON.stringify([], null, 1)); |
| 177 | assertEquals("[1,2,[3,[4],5],6,7]", |
| 178 | JSON.stringify([1, 2, [3, [4], 5], 6, 7], null)); |
| 179 | assertEquals("[2,4,[6,[8],10],12,14]", |
| 180 | JSON.stringify([1, 2, [3, [4], 5], 6, 7], DoubleNumbers)); |
| 181 | |
| 182 | var circular = [1, 2, 3]; |
| 183 | circular[2] = circular; |
| 184 | assertThrows(function () { JSON.stringify(circular); }, TypeError); |
| 185 | |
| 186 | var singleton = []; |
| 187 | var multiOccurrence = [singleton, singleton, singleton]; |
| 188 | assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence)); |
| 189 | |
| 190 | assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6})); |
| 191 | assertEquals('{"x":5}', JSON.stringify({x:5,y:6}, ['x'])); |
| 192 | assertEquals('{\n "a": "b",\n "c": "d"\n}', |
| 193 | JSON.stringify({a:"b",c:"d"}, null, 1)); |
| 194 | assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['y', 'x'])); |
| 195 | |
| 196 | assertEquals(undefined, JSON.stringify(undefined)); |
| 197 | assertEquals(undefined, JSON.stringify(function () { })); |
ager@chromium.org | add848f | 2009-08-13 12:44:13 +0000 | [diff] [blame^] | 198 | |
| 199 | function checkIllegal(str) { |
| 200 | assertThrows(function () { JSON.parse(str); }, SyntaxError); |
| 201 | } |
| 202 | |
| 203 | checkIllegal('1); throw "foo"; (1'); |
| 204 | |
| 205 | var x = 0; |
| 206 | eval("(1); x++; (1)"); |
| 207 | checkIllegal('1); x++; (1'); |