| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library json_tests; | 5 library json_tests; |
| 6 import 'dart:json' as json; | 6 import 'dart:json' as json; |
| 7 import 'dart:html'; | 7 import 'dart:html'; |
| 8 import '../../pkg/unittest/lib/unittest.dart'; | 8 import '../../pkg/unittest/lib/unittest.dart'; |
| 9 import '../../pkg/unittest/lib/html_config.dart'; | 9 import '../../pkg/unittest/lib/html_config.dart'; |
| 10 | 10 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi",' | 64 expect(json.parse(' {"x":3, "y": -4.5, "z" : "hi",' |
| 65 '"w":null, "u" : true, "v": false } '), | 65 '"w":null, "u" : true, "v": false } '), |
| 66 equals({"x":3, "y": -4.5, "z" : "hi", | 66 equals({"x":3, "y": -4.5, "z" : "hi", |
| 67 "w":null, "u" : true, "v": false })); | 67 "w":null, "u" : true, "v": false })); |
| 68 | 68 |
| 69 expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' | 69 expect(json.parse('{"x": {"a":3, "b": -4.5}, "y":[{}], ' |
| 70 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), | 70 '"z":"hi","w":{"c":null,"d":true}, "v":null}'), |
| 71 equals({"x": {"a":3, "b": -4.5}, "y":[{}], | 71 equals({"x": {"a":3, "b": -4.5}, "y":[{}], |
| 72 "z":"hi","w":{"c":null,"d":true}, "v":null})); | 72 "z":"hi","w":{"c":null,"d":true}, "v":null})); |
| 73 | 73 |
| 74 // Errors. |
| 75 expect(() => json.parse('True'), throwsFormatException); |
| 76 // Unfinished maps. |
| 77 expect(() => json.parse('{"a":10)'), throwsFormatException); |
| 78 expect(() => json.parse('{"a":}'), throwsFormatException); |
| 79 // Insist on double quotes. |
| 80 expect(() => json.parse("{'a':1}"), throwsFormatException); |
| 81 // Insist on quoted keys. |
| 82 expect(() => json.parse('{a:1}'), throwsFormatException); |
| 83 }); |
| 84 |
| 85 test('Parse-V8-bugs', () { |
| 86 expect(json.parse('{"__proto__": {"x":42}, "y": 37}'), |
| 87 equals({"__proto__": {"x":42}, "y": 37})); |
| 88 |
| 89 expect(json.parse('{"__proto__": []}'), |
| 90 equals({"__proto__": []})); |
| 91 }); |
| 92 |
| 93 test('Reviver', () { |
| 94 reviver(k, v) { |
| 95 if (k == '') return [v]; // wrap top level in a list. |
| 96 if (v is num || v is String || v is bool || v == null) return '$k$v'; |
| 97 return v; |
| 98 } |
| 99 |
| 100 go(input, expected) { |
| 101 expect(json.parse(input, reviver), equals(expected)); |
| 102 } |
| 103 |
| 104 go('null', [null]); |
| 105 go('["a", "b", true]', [['0a', '1b', '2true']]); |
| 106 go('{"a":100, "b":200}', [{"a":'a100', "b":'b200'}]); |
| 107 go('{"a":100, "b":{"c":200}}', [{"a":'a100', "b":{"c":'c200'}}]); |
| 108 }); |
| 109 |
| 74 test('stringify', () { | 110 test('stringify', () { |
| 75 // Scalars. | 111 // Scalars. |
| 76 expect(json.stringify(5), equals('5')); | 112 expect(json.stringify(5), equals('5')); |
| 77 expect(json.stringify(-42), equals('-42')); | 113 expect(json.stringify(-42), equals('-42')); |
| 78 // Dart does not guarantee a formatting for doubles, | 114 // Dart does not guarantee a formatting for doubles, |
| 79 // so reparse and compare to the original. | 115 // so reparse and compare to the original. |
| 80 validateRoundTrip(3.14); | 116 validateRoundTrip(3.14); |
| 81 expect(json.stringify(true), equals('true')); | 117 expect(json.stringify(true), equals('true')); |
| 82 expect(json.stringify(false), equals('false')); | 118 expect(json.stringify(false), equals('false')); |
| 83 expect(json.stringify(null), equals('null')); | 119 expect(json.stringify(null), equals('null')); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 }); | 162 }); |
| 127 | 163 |
| 128 }); | 164 }); |
| 129 | 165 |
| 130 test('stringify throws if argument cannot be converted', () { | 166 test('stringify throws if argument cannot be converted', () { |
| 131 /** | 167 /** |
| 132 * Checks that we get an exception (rather than silently returning null) if | 168 * Checks that we get an exception (rather than silently returning null) if |
| 133 * we try to stringify something that cannot be converted to json. | 169 * we try to stringify something that cannot be converted to json. |
| 134 */ | 170 */ |
| 135 expect(() => json.stringify(new TestClass()), throws); | 171 expect(() => json.stringify(new TestClass()), throws); |
| 136 }); | |
| 137 }); | 172 }); |
| 138 } | 173 } |
| 139 | 174 |
| 140 class TestClass { | 175 class TestClass { |
| 141 int x; | 176 int x; |
| 142 String y; | 177 String y; |
| 143 | 178 |
| 144 TestClass() : x = 3, y = 'joe' { } | 179 TestClass() : x = 3, y = 'joe' { } |
| 145 } | 180 } |
| 146 | 181 |
| 147 class ToJson { | 182 class ToJson { |
| 148 final object; | 183 final object; |
| 149 const ToJson(this.object); | 184 const ToJson(this.object); |
| 150 toJson() => object; | 185 toJson() => object; |
| 151 } | 186 } |
| 152 | 187 |
| 153 /** | 188 /** |
| 154 * Checks that the argument can be converted to a JSON string and | 189 * Checks that the argument can be converted to a JSON string and |
| 155 * back, and produce something equivalent to the argument. | 190 * back, and produce something equivalent to the argument. |
| 156 */ | 191 */ |
| 157 validateRoundTrip(expected) { | 192 validateRoundTrip(expected) { |
| 158 expect(json.parse(json.stringify(expected)), equals(expected)); | 193 expect(json.parse(json.stringify(expected)), equals(expected)); |
| 159 } | 194 } |
| 160 | |
| 161 | |
| OLD | NEW |