Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(367)

Unified Diff: tests/lib/convert/json_toEncodable_reviver_test.dart

Issue 133923004: Improve Json constructor. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tests/language/type_check_const_function_typedef_test.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/lib/convert/json_toEncodable_reviver_test.dart
diff --git a/tests/lib/convert/json_toEncodable_reviver_test.dart b/tests/lib/convert/json_toEncodable_reviver_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..58438875832ba8af0ec99feb114e2914d174648b
--- /dev/null
+++ b/tests/lib/convert/json_toEncodable_reviver_test.dart
@@ -0,0 +1,41 @@
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library json_tests;
+import 'package:expect/expect.dart';
+import 'dart:convert';
+
+class A {
+ final x;
+ A(this.x);
+}
+
+toEncodable(A a) => { "A": a.x };
+reviver(key, value) {
+ if (value is Map && value.length == 1 && value["A"] != null) {
+ return new A(value["A"]);
+ }
+ return value;
+}
+
+const extendedJson =
+ const JsonCodec(toEncodable: toEncodable, reviver: reviver);
+
+main() {
+ var encoded = extendedJson.encode([new A(0), { "2": new A(1) }]);
+ Expect.equals('[{"A":0},{"2":{"A":1}}]', encoded);
+ var decoded = extendedJson.decode(encoded);
+ Expect.isTrue(decoded is List);
+ Expect.equals(2, decoded.length);
+ Expect.isTrue(decoded[0] is A);
+ Expect.equals(0, decoded[0].x);
+ Expect.isTrue(decoded[1] is Map);
+ Expect.isNotNull(decoded[1]["2"]);
+ Expect.isTrue(decoded[1]["2"] is A);
+ Expect.equals(1, decoded[1]["2"].x);
+
+ var a = extendedJson.decode(extendedJson.encode(new A(499)));
+ Expect.isTrue(a is A);
+ Expect.equals(499, a.x);
+}
« no previous file with comments | « tests/language/type_check_const_function_typedef_test.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698