)
Fuses this
with other
.
When encoding, the resulting codec encodes with this
before
encoding with other
.
When decoding, the resulting codec decodes with other
before decoding
with this
.
In some cases one needs to use the inverted codecs to be able to fuse
them correctly. That is, the output type of this
(T
) must match the
input type of the second codec other
.
Examples:
final JSON_TO_BYTES = JSON.fuse(UTF8);
List<int> bytes = JSON_TO_BYTES.encode(["json-object"]);
var decoded = JSON_TO_BYTES.decode(bytes);
assert(decoded is List && decoded[0] == "json-object");
var inverted = JSON.inverted;
var jsonIdentity = JSON.fuse(inverted);
var jsonObject = jsonIdentity.encode(["1", 2]);
assert(jsonObject is List && jsonObject[0] == "1" && jsonObject[1] == 2);
Source
/**
* Fuses `this` with `other`.
*
* When encoding, the resulting codec encodes with `this` before
* encoding with [other].
*
* When decoding, the resulting codec decodes with [other] before decoding
* with `this`.
*
* In some cases one needs to use the [inverted] codecs to be able to fuse
* them correctly. That is, the output type of `this` ([T]) must match the
* input type of the second codec [other].
*
* Examples:
*
* final JSON_TO_BYTES = JSON.fuse(UTF8);
* List<int> bytes = JSON_TO_BYTES.encode(["json-object"]);
* var decoded = JSON_TO_BYTES.decode(bytes);
* assert(decoded is List && decoded[0] == "json-object");
*
* var inverted = JSON.inverted;
* var jsonIdentity = JSON.fuse(inverted);
* var jsonObject = jsonIdentity.encode(["1", 2]);
* assert(jsonObject is List && jsonObject[0] == "1" && jsonObject[1] == 2);
*/
// TODO(floitsch): use better example with line-splitter once that one is
// in this library.
Codec<S, dynamic> fuse(Codec<T, dynamic> other) {
return new _FusedCodec<S, T, dynamic>(this, other);
}