serialize method

List<int> serialize(
  1. T object
)

Serialize the given object to bytes.

Implementation

List<int> serialize(T object) {
  final writer = Payload.write();
  for (final argument in arguments) {
    final value = argument.from(object);

    if (value == null && !argument.isNullable) {
      throw Exception(
        'Tried to retrieve field "${argument.name}" of $T, which is not '
        'nullable but it returned null\n\n'
        'Did you forget to wrap the payload type in "nullable"?',
      );
    }

    writer.set(argument.type, value);
  }

  try {
    return binarize(writer);
  } catch (err) {
    throw Exception('Failed to serialize $T: $err');
  }
}