encodeValue<V> method

Object? encodeValue<V>(
  1. V value, [
  2. EncodingOptions? options,
  3. MapperContainer? container
])
inherited

Implementation

Object? encodeValue<V>(V value,
    [EncodingOptions? options, MapperContainer? container]) {
  try {
    var includeTypeId = options?.includeTypeId;
    includeTypeId ??= this.includeTypeId<V>(value);

    var result = this.encoder(
      value as T,
      EncodingContext(
        container: container,
        options: options?.inheritOptions ?? false ? options : null,
        args: () {
          Type type = V;
          if (includeTypeId ?? false) {
            type = value.runtimeType;
          }

          var typeArgs =
              type.args.map((t) => t == UnresolvedType ? dynamic : t);

          var fallback = this.type.base.args;
          if (typeArgs.length != fallback.length) {
            typeArgs = fallback;
          }
          return typeArgs.toList();
        },
      ),
    );

    if (includeTypeId && result is Map<String, dynamic>) {
      result['__type'] = value.runtimeType.id;
    }

    return result;
  } catch (e, stacktrace) {
    Error.throwWithStackTrace(
      MapperException.chain(MapperMethod.encode, '(${value.runtimeType})', e),
      stacktrace,
    );
  }
}