fuse method

Codec<S, R> fuse<R>(Codec<T, R> other)

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

// TODO(floitsch): use better example with line-splitter once that one is
// in this library.
Codec<S, R> fuse<R>(Codec<T, R> other) {
  return new _FusedCodec<S, T, R>(this, other);
}

© 2012 the Dart project authors
Licensed under the Creative Commons Attribution-ShareAlike License v4.0.
https://api.dartlang.org/stable/1.24.3/dart-convert/Codec/fuse.html