OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 import 'dart:convert'; | 5 import 'dart:convert'; |
6 | 6 |
7 import 'package:expect/expect.dart'; | 7 import 'package:expect/expect.dart'; |
8 | 8 |
9 main() { | 9 main() { |
10 final RAW = '["122ç",50,50,231]'; | 10 final RAW = '["122ç",50,50,231]'; |
11 final ENCODED = const [91, 34, 49, 50, 50, 195, 167, 34, 44, | 11 final ENCODED = const [91, 34, 49, 50, 50, 195, 167, 34, 44, |
12 53, 48, 44, 53, 48, 44, 50, 51, 49, 93]; | 12 53, 48, 44, 53, 48, 44, 50, 51, 49, 93]; |
13 Expect.listEquals(ENCODED, UTF8.encode(RAW)); | 13 Expect.listEquals(ENCODED, UTF8.encode(RAW)); |
14 Expect.equals(RAW, UTF8.decode(ENCODED)); | 14 Expect.equals(RAW, UTF8.decode(ENCODED)); |
15 | 15 |
16 Expect.listEquals([], UTF8.encode("")); | 16 Expect.listEquals([], UTF8.encode("")); |
17 Expect.equals("", UTF8.decode([])); | 17 Expect.equals("", UTF8.decode([])); |
18 | 18 |
19 final JSON_ENCODED = RAW; | 19 final JSON_ENCODED = RAW; |
20 Expect.equals(JSON_ENCODED, JSON.encode(["122ç", 50, 50, 231])); | 20 Expect.equals(JSON_ENCODED, JSON.encode(["122ç", 50, 50, 231])); |
21 Expect.listEquals(["122ç", 50, 50, 231], JSON.decode(JSON_ENCODED)); | 21 Expect.listEquals(["122ç", 50, 50, 231], JSON.decode(JSON_ENCODED)); |
22 | 22 |
23 // Test that the reviver is passed to the decoder. | 23 // Test that the reviver is passed to the decoder. |
24 var decoded = JSON.decode('{"p": 5}', reviver: (k, v) { | 24 var decoded = JSON.decode('{"p": 5}', reviver: (k, v) { |
25 if (k == "") return v; | 25 if (k == null) return v; |
26 return v * 2; | 26 return v * 2; |
27 }); | 27 }); |
28 Expect.equals(10, decoded["p"]); | 28 Expect.equals(10, decoded["p"]); |
29 var jsonWithReviver = new JsonCodec.withReviver((k, v) { | 29 var jsonWithReviver = new JsonCodec.withReviver((k, v) { |
30 if (k == "") return v; | 30 if (k == null) return v; |
31 return v * 2; | 31 return v * 2; |
32 }); | 32 }); |
33 decoded = jsonWithReviver.decode('{"p": 5}'); | 33 decoded = jsonWithReviver.decode('{"p": 5}'); |
34 Expect.equals(10, decoded["p"]); | 34 Expect.equals(10, decoded["p"]); |
35 | 35 |
36 // Test example from comments. | 36 // Test example from comments. |
37 final JSON_TO_BYTES = JSON.fuse(UTF8); | 37 final JSON_TO_BYTES = JSON.fuse(UTF8); |
38 List<int> bytes = JSON_TO_BYTES.encode(["json-object"]); | 38 List<int> bytes = JSON_TO_BYTES.encode(["json-object"]); |
39 decoded = JSON_TO_BYTES.decode(bytes); | 39 decoded = JSON_TO_BYTES.decode(bytes); |
40 Expect.isTrue(decoded is List); | 40 Expect.isTrue(decoded is List); |
41 Expect.equals("json-object", decoded[0]); | 41 Expect.equals("json-object", decoded[0]); |
42 } | 42 } |
OLD | NEW |