de method

dynamic de(
  1. dynamic type,
  2. dynamic data, [
  3. Encoding? encoding
])

Deserialize BCS into a Dart type.

final num = bcs.ser('u64', '4294967295').hex();
final deNum = bcs.de('u64', num, Encoding.hex);
expect(deNum.toString(), '4294967295');

Implementation

dynamic de(
  dynamic type,
  dynamic data,
  [Encoding? encoding]
) {
  if (data is String) {
    if (encoding != null) {
      data = decodeStr(data, encoding);
    } else {
      throw ArgumentError("To pass a string to `bcs.de`, specify encoding");
    }
  }

  // In case the type specified is already registered.
  if (type is String || type is Iterable) {
    final (name, params) = parseTypeName(type);
    return getTypeInterface(name).decode(data, params);
  }

  // Deserialize without registering a type using a temporary clone.
  if (type is StructTypeDefinition) {
    final temp = LegacyBCS.fromBCS(this);
    final key = tempKey();
    return temp.registerStructType(key, type).de(key, data, encoding);
  }

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