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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « tests/language/type_check_const_function_typedef_test.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
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.
4
5 library json_tests;
6 import 'package:expect/expect.dart';
7 import 'dart:convert';
8
9 class A {
10 final x;
11 A(this.x);
12 }
13
14 toEncodable(A a) => { "A": a.x };
15 reviver(key, value) {
16 if (value is Map && value.length == 1 && value["A"] != null) {
17 return new A(value["A"]);
18 }
19 return value;
20 }
21
22 const extendedJson =
23 const JsonCodec(toEncodable: toEncodable, reviver: reviver);
24
25 main() {
26 var encoded = extendedJson.encode([new A(0), { "2": new A(1) }]);
27 Expect.equals('[{"A":0},{"2":{"A":1}}]', encoded);
28 var decoded = extendedJson.decode(encoded);
29 Expect.isTrue(decoded is List);
30 Expect.equals(2, decoded.length);
31 Expect.isTrue(decoded[0] is A);
32 Expect.equals(0, decoded[0].x);
33 Expect.isTrue(decoded[1] is Map);
34 Expect.isNotNull(decoded[1]["2"]);
35 Expect.isTrue(decoded[1]["2"] is A);
36 Expect.equals(1, decoded[1]["2"].x);
37
38 var a = extendedJson.decode(extendedJson.encode(new A(499)));
39 Expect.isTrue(a is A);
40 Expect.equals(499, a.x);
41 }
OLDNEW
« 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