OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library dart.codec; | |
6 | |
7 import 'dart:convert'; | |
8 | |
9 part 'encoding.dart'; | |
10 part 'json.dart'; | |
11 | |
12 /** | |
13 * A [Codec] encodes and (if supported) decodes data. | |
14 * | |
15 * Codecs can be fused. For example fusing [JSON] and [Encoding.UTF8] produces | |
16 * an encoder that can convert Json objects directly to bytes, or can decode | |
17 * bytes directly to json objects. | |
18 * | |
19 * Fused codecs generally work in an optimized way and can be faster than | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
generally work in an optimized way ->
generally at
floitsch
2013/07/12 16:09:15
Done.
| |
20 * executing each step of an encoding separately. | |
21 * | |
22 * *Codecs are still experimental and are subject to change without notice.* | |
23 */ | |
24 abstract class Codec<S, T> { | |
25 const Codec(); | |
26 | |
27 T encode(S input) => encoder.convert(input); | |
28 S decode(T encoded) => decoder.convert(encoded); | |
29 | |
30 Converter<S, T> get encoder; | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
Documentation!
E.g.:
This encoder converts from S
floitsch
2013/07/12 16:09:15
Done.
| |
31 Converter<T, S> get decoder; | |
32 | |
33 /** | |
34 * Fuses `this` with `other`. | |
35 * | |
36 * When encoding, the resulting codec encodes with `this` before | |
37 * encoding with [other]. | |
38 * | |
39 * When decoding, the resulting codec decodes with [other] before decoding | |
40 * with `this`. | |
41 * | |
42 * In some cases one needs to use the [inverted] codecs to be able to fuse | |
43 * them correctly. | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
Elaborate. Is this just a case of "output of this
floitsch
2013/07/12 16:09:15
Done.
| |
44 * | |
45 * Examples: | |
46 * | |
47 * final JSON_TO_BYTES = JSON.fuse(Encoding.UTF8); | |
48 * List<int> bytes = JSON_TO_BYTES.encode(["json-object"]); | |
49 * var decoded = JSON_TO_BYTES.decode(bytes); | |
50 * assert(decoded is List && decoded[0] == "json-object"); | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
Example using [inverted]?
floitsch
2013/07/12 16:09:15
Added one, but the best example would be with the
| |
51 */ | |
52 Codec<S, dynamic> fuse(Codec<T, dynamic> other) { | |
53 return new _FusedCodec<S, T, dynamic>(this, other); | |
54 } | |
55 | |
56 /** | |
57 * Inverts `this`. | |
58 * | |
59 * The [encoder] and [decoder] of the resulting codec are inversed. | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
inversed -> swapped.
floitsch
2013/07/12 16:09:15
Done.
| |
60 */ | |
61 Codec<T, S> get inverted => new _InvertedCodec<T, S>(this); | |
62 } | |
63 | |
64 class _FusedCodec<S, M, T> extends Codec<S, T> { | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
Documentation for class, please.
This seems to be
floitsch
2013/07/12 16:09:15
correct.
| |
65 final Codec<S, M> _first; | |
66 final Codec<M, T> _second; | |
67 | |
68 Converter<S, T> get encoder => _first.encoder.fuse(_second.encoder); | |
69 Converter<T, S> get decoder => _second.decoder.fuse(_first.decoder); | |
70 | |
71 _FusedCodec(Codec<S, M> first, Codec<M, T> second) | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
_FusedCodec(this._first, this._second);
I don't u
floitsch
2013/07/12 16:09:15
Done.
| |
72 : this._first = first, | |
73 this._second = second; | |
74 } | |
75 | |
76 class _InvertedCodec<S, T> extends Codec<S, T> { | |
Lasse Reichstein Nielsen
2013/07/12 11:50:47
Consider swapping S and T here, so it reads better
floitsch
2013/07/12 16:09:15
Done.
| |
77 final Codec<T, S> _codec; | |
78 | |
79 _InvertedCodec(Codec<T, S> codec) : _codec = codec; | |
80 | |
81 Converter<S, T> get encoder => _codec.decoder; | |
82 Converter<T, S> get decoder => _codec.encoder; | |
83 | |
84 Codec<T, S> get inverted => _codec; | |
85 } | |
OLD | NEW |