ser method

BcsWriter ser(
  1. dynamic type,
  2. dynamic data, [
  3. BcsWriterOptions? options
])

Serialize data into bcs.

bcs.registerVectorType('vector<u8>');

final serialized = bcs
  .ser('vector<u8>', [1,2,3,4,5,6])
  .toBytes();

expect(toHEX(serialized), '06010203040506');

Implementation

BcsWriter ser(
  dynamic type,
  dynamic data,
  [BcsWriterOptions? options]
) {
  if (type is String || type is Iterable) {
    final (name, params) = parseTypeName(type);
    return getTypeInterface(name).encode(
      data,
      options,
      params
    );
  }

  // Quick serialization without registering the type in the main struct.
  if (type is StructTypeDefinition) {
    final key = tempKey();
    final temp = LegacyBCS.fromBCS(this);
    return temp.registerStructType(key, type).ser(key, data, options);
  }

  throw ArgumentError(
    "Incorrect type passed into the '.ser()' function. \n${jsonEncode(type)}"
  );
}