| 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('Reviver', () { |
| 86 reviver(k, v) { |
| 87 if (k == '') return [v]; // wrap top level in a list. |
| 88 if (v is num || v is String || v is bool || v == null) return '$k$v'; |
| 89 return v; |
| 90 } |
| 91 |
| 92 go(input, expected) { |
| 93 expect(json.parse(input, reviver), equals(expected)); |
| 94 } |
| 95 |
| 96 go('null', [null]); |
| 97 go('["a", "b", true]', [['0a', '1b', '2true']]); |
| 98 go('{"a":100, "b":200}', [{"a":'a100', "b":'b200'}]); |
| 99 go('{"a":100, "b":{"c":200}}', [{"a":'a100', "b":{"c":'c200'}}]); |
| 100 }); |
| 101 |
| 74 test('stringify', () { | 102 test('stringify', () { |
| 75 // Scalars. | 103 // Scalars. |
| 76 expect(json.stringify(5), equals('5')); | 104 expect(json.stringify(5), equals('5')); |
| 77 expect(json.stringify(-42), equals('-42')); | 105 expect(json.stringify(-42), equals('-42')); |
| 78 // Dart does not guarantee a formatting for doubles, | 106 // Dart does not guarantee a formatting for doubles, |
| 79 // so reparse and compare to the original. | 107 // so reparse and compare to the original. |
| 80 validateRoundTrip(3.14); | 108 validateRoundTrip(3.14); |
| 81 expect(json.stringify(true), equals('true')); | 109 expect(json.stringify(true), equals('true')); |
| 82 expect(json.stringify(false), equals('false')); | 110 expect(json.stringify(false), equals('false')); |
| 83 expect(json.stringify(null), equals('null')); | 111 expect(json.stringify(null), equals('null')); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 126 }); | 154 }); |
| 127 | 155 |
| 128 }); | 156 }); |
| 129 | 157 |
| 130 test('stringify throws if argument cannot be converted', () { | 158 test('stringify throws if argument cannot be converted', () { |
| 131 /** | 159 /** |
| 132 * Checks that we get an exception (rather than silently returning null) if | 160 * Checks that we get an exception (rather than silently returning null) if |
| 133 * we try to stringify something that cannot be converted to json. | 161 * we try to stringify something that cannot be converted to json. |
| 134 */ | 162 */ |
| 135 expect(() => json.stringify(new TestClass()), throws); | 163 expect(() => json.stringify(new TestClass()), throws); |
| 136 }); | |
| 137 }); | 164 }); |
| 138 } | 165 } |
| 139 | 166 |
| 140 class TestClass { | 167 class TestClass { |
| 141 int x; | 168 int x; |
| 142 String y; | 169 String y; |
| 143 | 170 |
| 144 TestClass() : x = 3, y = 'joe' { } | 171 TestClass() : x = 3, y = 'joe' { } |
| 145 } | 172 } |
| 146 | 173 |
| 147 class ToJson { | 174 class ToJson { |
| 148 final object; | 175 final object; |
| 149 const ToJson(this.object); | 176 const ToJson(this.object); |
| 150 toJson() => object; | 177 toJson() => object; |
| 151 } | 178 } |
| 152 | 179 |
| 153 /** | 180 /** |
| 154 * Checks that the argument can be converted to a JSON string and | 181 * Checks that the argument can be converted to a JSON string and |
| 155 * back, and produce something equivalent to the argument. | 182 * back, and produce something equivalent to the argument. |
| 156 */ | 183 */ |
| 157 validateRoundTrip(expected) { | 184 validateRoundTrip(expected) { |
| 158 expect(json.parse(json.stringify(expected)), equals(expected)); | 185 expect(json.parse(json.stringify(expected)), equals(expected)); |
| 159 } | 186 } |
| 160 | |
| 161 | |
| OLD | NEW |