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 part of dart.convert; | 5 part of dart.convert; |
6 | 6 |
7 /** | 7 /** |
8 * A [Codec] encodes and (if supported) decodes data. | 8 * A [Codec] encodes and (if supported) decodes data. |
9 * | 9 * |
10 * Codecs can be fused. For example fusing [JSON] and [UTF8] produces | 10 * Codecs can be fused. For example fusing [JSON] and [UTF8] produces |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 * var decoded = JSON_TO_BYTES.decode(bytes); | 53 * var decoded = JSON_TO_BYTES.decode(bytes); |
54 * assert(decoded is List && decoded[0] == "json-object"); | 54 * assert(decoded is List && decoded[0] == "json-object"); |
55 * | 55 * |
56 * var inverted = JSON.inverted; | 56 * var inverted = JSON.inverted; |
57 * var jsonIdentity = JSON.fuse(inverted); | 57 * var jsonIdentity = JSON.fuse(inverted); |
58 * var jsonObject = jsonIdentity.encode(["1", 2]); | 58 * var jsonObject = jsonIdentity.encode(["1", 2]); |
59 * assert(jsonObject is List && jsonObject[0] == "1" && jsonObject[1] == 2
); | 59 * assert(jsonObject is List && jsonObject[0] == "1" && jsonObject[1] == 2
); |
60 */ | 60 */ |
61 // TODO(floitsch): use better example with line-splitter once that one is | 61 // TODO(floitsch): use better example with line-splitter once that one is |
62 // in this library. | 62 // in this library. |
63 Codec<S, dynamic/*=R*/> fuse/*<R>*/(Codec<T, dynamic/*=R*/> other) { | 63 Codec<S, R> fuse<R>(Codec<T, R> other) { |
64 return new _FusedCodec<S, T, dynamic/*=R*/>(this, other); | 64 return new _FusedCodec<S, T, R>(this, other); |
65 } | 65 } |
66 | 66 |
67 /** | 67 /** |
68 * Inverts `this`. | 68 * Inverts `this`. |
69 * | 69 * |
70 * The [encoder] and [decoder] of the resulting codec are swapped. | 70 * The [encoder] and [decoder] of the resulting codec are swapped. |
71 */ | 71 */ |
72 Codec<T, S> get inverted => new _InvertedCodec<T, S>(this); | 72 Codec<T, S> get inverted => new _InvertedCodec<T, S>(this); |
73 } | 73 } |
74 | 74 |
75 /** | 75 /** |
76 * Fuses the given codecs. | 76 * Fuses the given codecs. |
77 * | 77 * |
78 * In the non-chunked conversion simply invokes the non-chunked conversions in | 78 * In the non-chunked conversion simply invokes the non-chunked conversions in |
79 * sequence. | 79 * sequence. |
80 */ | 80 */ |
81 class _FusedCodec<S, M, T> extends Codec<S, T> { | 81 class _FusedCodec<S, M, T> extends Codec<S, T> { |
82 final Codec<S, M> _first; | 82 final Codec<S, M> _first; |
83 final Codec<M, T> _second; | 83 final Codec<M, T> _second; |
84 | 84 |
85 Converter<S, T> get encoder => _first.encoder.fuse/*<T>*/(_second.encoder); | 85 Converter<S, T> get encoder => _first.encoder.fuse<T>(_second.encoder); |
86 Converter<T, S> get decoder => _second.decoder.fuse/*<S>*/(_first.decoder); | 86 Converter<T, S> get decoder => _second.decoder.fuse<S>(_first.decoder); |
87 | 87 |
88 _FusedCodec(this._first, this._second); | 88 _FusedCodec(this._first, this._second); |
89 } | 89 } |
90 | 90 |
91 class _InvertedCodec<T, S> extends Codec<T, S> { | 91 class _InvertedCodec<T, S> extends Codec<T, S> { |
92 final Codec<S, T> _codec; | 92 final Codec<S, T> _codec; |
93 | 93 |
94 _InvertedCodec(Codec<S, T> codec) : _codec = codec; | 94 _InvertedCodec(Codec<S, T> codec) : _codec = codec; |
95 | 95 |
96 Converter<T, S> get encoder => _codec.decoder; | 96 Converter<T, S> get encoder => _codec.decoder; |
97 Converter<S, T> get decoder => _codec.encoder; | 97 Converter<S, T> get decoder => _codec.encoder; |
98 | 98 |
99 Codec<S, T> get inverted => _codec; | 99 Codec<S, T> get inverted => _codec; |
100 } | 100 } |
OLD | NEW |