| 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 "package:expect/expect.dart"; | 6 import "package:expect/expect.dart"; |
| 7 import "dart:convert"; | 7 import "dart:convert"; |
| 8 import 'dart:html'; | 8 import 'dart:html'; |
| 9 import '../../pkg/unittest/lib/unittest.dart'; | 9 import '../../pkg/unittest/lib/unittest.dart'; |
| 10 import '../../pkg/unittest/lib/html_config.dart'; | 10 import '../../pkg/unittest/lib/html_config.dart'; |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 | 129 |
| 130 }); | 130 }); |
| 131 | 131 |
| 132 test('stringify throws if argument cannot be converted', () { | 132 test('stringify throws if argument cannot be converted', () { |
| 133 /** | 133 /** |
| 134 * Checks that we get an exception (rather than silently returning null) if | 134 * Checks that we get an exception (rather than silently returning null) if |
| 135 * we try to stringify something that cannot be converted to json. | 135 * we try to stringify something that cannot be converted to json. |
| 136 */ | 136 */ |
| 137 expect(() => JSON.encode(new TestClass()), throws); | 137 expect(() => JSON.encode(new TestClass()), throws); |
| 138 }); | 138 }); |
| 139 |
| 140 test('stringify custom converter', () { |
| 141 List l = [const C(0), const C(1)]; |
| 142 expect(JSON.encode(l, (x) => "C(${x.id})"), equals("[C(0),C(1)]")); |
| 143 expect(JSON.encode(l), equals("[C/0,C/1]")); |
| 144 }); |
| 145 } |
| 146 |
| 147 class C { |
| 148 final int id; |
| 149 const C(this.id); |
| 150 toJson() => "C/$id"; |
| 139 } | 151 } |
| 140 | 152 |
| 141 class TestClass { | 153 class TestClass { |
| 142 int x; | 154 int x; |
| 143 String y; | 155 String y; |
| 144 | 156 |
| 145 TestClass() : x = 3, y = 'joe' { } | 157 TestClass() : x = 3, y = 'joe' { } |
| 146 } | 158 } |
| 147 | 159 |
| 148 class ToJson { | 160 class ToJson { |
| 149 final object; | 161 final object; |
| 150 const ToJson(this.object); | 162 const ToJson(this.object); |
| 151 toJson() => object; | 163 toJson() => object; |
| 152 } | 164 } |
| 153 | 165 |
| 154 /** | 166 /** |
| 155 * Checks that the argument can be converted to a JSON string and | 167 * Checks that the argument can be converted to a JSON string and |
| 156 * back, and produce something equivalent to the argument. | 168 * back, and produce something equivalent to the argument. |
| 157 */ | 169 */ |
| 158 validateRoundTrip(expected) { | 170 validateRoundTrip(expected) { |
| 159 expect(JSON.decode(JSON.encode(expected)), equals(expected)); | 171 expect(JSON.decode(JSON.encode(expected)), equals(expected)); |
| 160 } | 172 } |
| 161 | |
| 162 | |
| OLD | NEW |