| OLD | NEW |
| (Empty) | |
| 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('"abc\x00def"'); |
| 140 TestInvalid('"abc\x10def"'); |
| 141 TestInvalid('"abc\x1fdef"'); |
| 142 |
| 143 TestInvalid("[1, 2"); |
| 144 TestInvalid('{"x": 3'); |
| 145 |
| 146 // Stringify |
| 147 |
| 148 assertEquals("true", JSON.stringify(true)); |
| 149 assertEquals("false", JSON.stringify(false)); |
| 150 assertEquals("null", JSON.stringify(null)); |
| 151 assertEquals("false", JSON.stringify({toJSON: function () { return false; }})); |
| 152 assertEquals("4", JSON.stringify(4)); |
| 153 assertEquals('"foo"', JSON.stringify("foo")); |
| 154 assertEquals("null", JSON.stringify(Infinity)); |
| 155 assertEquals("null", JSON.stringify(-Infinity)); |
| 156 assertEquals("null", JSON.stringify(NaN)); |
| 157 assertEquals("4", JSON.stringify(new Number(4))); |
| 158 assertEquals('"bar"', JSON.stringify(new String("bar"))); |
| 159 |
| 160 assertEquals('"foo\\u0000bar"', JSON.stringify("foo\0bar")); |
| 161 assertEquals('"f\\"o\'o\\\\b\\ba\\fr\\nb\\ra\\tz"', |
| 162 JSON.stringify("f\"o\'o\\b\ba\fr\nb\ra\tz")); |
| 163 |
| 164 assertEquals("[1,2,3]", JSON.stringify([1, 2, 3])); |
| 165 assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 1)); |
| 166 assertEquals("[\n 1,\n 2,\n 3\n]", JSON.stringify([1, 2, 3], null, 2)); |
| 167 assertEquals("[\n 1,\n 2,\n 3\n]", |
| 168 JSON.stringify([1, 2, 3], null, new Number(2))); |
| 169 assertEquals("[\n^1,\n^2,\n^3\n]", JSON.stringify([1, 2, 3], null, "^")); |
| 170 assertEquals("[\n^1,\n^2,\n^3\n]", |
| 171 JSON.stringify([1, 2, 3], null, new String("^"))); |
| 172 assertEquals("[\n 1,\n 2,\n [\n 3,\n [\n 4\n ],\n 5\n ],\n 6,\n 7\n]", |
| 173 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null, 1)); |
| 174 assertEquals("[]", JSON.stringify([], null, 1)); |
| 175 assertEquals("[1,2,[3,[4],5],6,7]", |
| 176 JSON.stringify([1, 2, [3, [4], 5], 6, 7], null)); |
| 177 assertEquals("[2,4,[6,[8],10],12,14]", |
| 178 JSON.stringify([1, 2, [3, [4], 5], 6, 7], DoubleNumbers)); |
| 179 |
| 180 var circular = [1, 2, 3]; |
| 181 circular[2] = circular; |
| 182 assertThrows(function () { JSON.stringify(circular); }, TypeError); |
| 183 |
| 184 var singleton = []; |
| 185 var multiOccurrence = [singleton, singleton, singleton]; |
| 186 assertEquals("[[],[],[]]", JSON.stringify(multiOccurrence)); |
| 187 |
| 188 assertEquals('{"x":5,"y":6}', JSON.stringify({x:5,y:6})); |
| 189 assertEquals('{"x":5}', JSON.stringify({x:5,y:6}, ['x'])); |
| 190 assertEquals('{\n "a": "b",\n "c": "d"\n}', |
| 191 JSON.stringify({a:"b",c:"d"}, null, 1)); |
| 192 assertEquals('{"y":6,"x":5}', JSON.stringify({x:5,y:6}, ['x', 'y'])); |
| 193 |
| 194 assertEquals(undefined, JSON.stringify(undefined)); |
| 195 assertEquals(undefined, JSON.stringify(function () { })); |
| OLD | NEW |