| 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_map_test; | 5 library json_map_test; |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 import 'dart:convert' show JSON; | 8 import 'dart:convert' show JSON; |
| 9 import 'dart:collection' show LinkedHashMap, HashMap; | 9 import 'dart:collection' show LinkedHashMap, HashMap; |
| 10 | 10 |
| 11 Map jsonify(Map map) => JSON.decode(JSON.encode(map)); | 11 bool useReviver = false; |
| 12 Map jsonify(Map map) { |
| 13 String encoded = JSON.encode(map); |
| 14 return useReviver |
| 15 ? JSON.decode(encoded, reviver: (key, value) => value) |
| 16 : JSON.decode(encoded); |
| 17 } |
| 12 | 18 |
| 13 List listEach(Map map) { | 19 List listEach(Map map) { |
| 14 var result = []; | 20 var result = []; |
| 15 map.forEach((String key, value) { | 21 map.forEach((String key, value) { |
| 16 result.add(key); | 22 result.add(key); |
| 17 result.add(value); | 23 result.add(value); |
| 18 }); | 24 }); |
| 19 return result; | 25 return result; |
| 20 } | 26 } |
| 21 | 27 |
| 22 void main() { | 28 void main() { |
| 29 test(false); |
| 30 test(true); |
| 31 } |
| 32 |
| 33 void test(bool revive) { |
| 34 useReviver = revive; |
| 23 testEmpty(jsonify({})); | 35 testEmpty(jsonify({})); |
| 24 testAtoB(jsonify({'a': 'b'})); | 36 testAtoB(jsonify({'a': 'b'})); |
| 25 | 37 |
| 26 Map map = jsonify({}); | 38 Map map = jsonify({}); |
| 27 map['a'] = 'b'; | 39 map['a'] = 'b'; |
| 28 testAtoB(map); | 40 testAtoB(map); |
| 29 | 41 |
| 30 map = jsonify({}); | 42 map = jsonify({}); |
| 31 Expect.equals('b', map.putIfAbsent('a', () => 'b')); | 43 Expect.equals('b', map.putIfAbsent('a', () => 'b')); |
| 32 testAtoB(map); | 44 testAtoB(map); |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 315 } | 327 } |
| 316 | 328 |
| 317 void testListEntry() { | 329 void testListEntry() { |
| 318 Map map = jsonify({'a': [7, 8, {'b': 9}]}); | 330 Map map = jsonify({'a': [7, 8, {'b': 9}]}); |
| 319 List list = map['a']; | 331 List list = map['a']; |
| 320 Expect.equals(3, list.length); | 332 Expect.equals(3, list.length); |
| 321 Expect.equals(7, list[0]); | 333 Expect.equals(7, list[0]); |
| 322 Expect.equals(8, list[1]); | 334 Expect.equals(8, list[1]); |
| 323 Expect.equals(9, list[2]['b']); | 335 Expect.equals(9, list[2]['b']); |
| 324 } | 336 } |
| OLD | NEW |