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 [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 attempt to optimize the operations and can be faster |
| 20 * than 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 /** |
| 31 * Returns the encoder from [S] to [T]. |
| 32 * |
| 33 * It may be stateful and should not be reused. |
| 34 */ |
| 35 Converter<S, T> get encoder; |
| 36 /** |
| 37 * Returns the decoder of `this`, converting from [T] to [S]. |
| 38 * |
| 39 * It may be stateful an should not be reused. |
| 40 */ |
| 41 Converter<T, S> get decoder; |
| 42 |
| 43 /** |
| 44 * Fuses `this` with `other`. |
| 45 * |
| 46 * When encoding, the resulting codec encodes with `this` before |
| 47 * encoding with [other]. |
| 48 * |
| 49 * When decoding, the resulting codec decodes with [other] before decoding |
| 50 * with `this`. |
| 51 * |
| 52 * In some cases one needs to use the [inverted] codecs to be able to fuse |
| 53 * them correctly. That is, the output type of `this` ([T]) must match the |
| 54 * input type of the second codec [other]. |
| 55 * |
| 56 * Examples: |
| 57 * |
| 58 * final JSON_TO_BYTES = JSON.fuse(UTF8); |
| 59 * List<int> bytes = JSON_TO_BYTES.encode(["json-object"]); |
| 60 * var decoded = JSON_TO_BYTES.decode(bytes); |
| 61 * assert(decoded is List && decoded[0] == "json-object"); |
| 62 * |
| 63 * var inverted = JSON.inverted; |
| 64 * var jsonIdentity = JSON.fuse(inverted); |
| 65 * var jsonObject = jsonIdentity.encode(["1", 2]); |
| 66 * assert(jsonObject is List && jsonObject[0] == "1" && jsonObject[1] == 2
); |
| 67 */ |
| 68 // TODO(floitsch): use better example with line-splitter once that one is |
| 69 // in this library. |
| 70 Codec<S, dynamic> fuse(Codec<T, dynamic> other) { |
| 71 return new _FusedCodec<S, T, dynamic>(this, other); |
| 72 } |
| 73 |
| 74 /** |
| 75 * Inverts `this`. |
| 76 * |
| 77 * The [encoder] and [decoder] of the resulting codec are swapped. |
| 78 */ |
| 79 Codec<T, S> get inverted => new _InvertedCodec<T, S>(this); |
| 80 } |
| 81 |
| 82 /** |
| 83 * Fuses the given codecs. |
| 84 * |
| 85 * In the non-chunked conversion simply invokes the non-chunked conversions in |
| 86 * sequence. |
| 87 */ |
| 88 class _FusedCodec<S, M, T> extends Codec<S, T> { |
| 89 final Codec<S, M> _first; |
| 90 final Codec<M, T> _second; |
| 91 |
| 92 Converter<S, T> get encoder => _first.encoder.fuse(_second.encoder); |
| 93 Converter<T, S> get decoder => _second.decoder.fuse(_first.decoder); |
| 94 |
| 95 _FusedCodec(this._first, this._second); |
| 96 } |
| 97 |
| 98 class _InvertedCodec<T, S> extends Codec<T, S> { |
| 99 final Codec<S, T> _codec; |
| 100 |
| 101 _InvertedCodec(Codec<S, T> codec) : _codec = codec; |
| 102 |
| 103 Converter<T, S> get encoder => _codec.decoder; |
| 104 Converter<S, T> get decoder => _codec.encoder; |
| 105 |
| 106 Codec<S, T> get inverted => _codec; |
| 107 } |
OLD | NEW |