WasmInput.fromJson constructor

WasmInput.fromJson(
  1. Object? json_
)

Returns a new instance from a JSON value. May throw if the value does not have the expected structure.

Implementation

factory WasmInput.fromJson(Object? json_) {
  Object? json = json_;
  if (json is Map) {
    final MapEntry(:key, :value) = json.entries.firstWhere(
      (e) => e.key != 'runtimeType',
    );
    json = (
      key is int ? key : _spec.cases.indexWhere((c) => c.label == key),
      value,
    );
  }
  return switch (json) {
    (0, final value) || [0, final value] => WasmInputBinary(
      (value is Uint8List
          ? value
          : Uint8List.fromList((value! as List).cast())),
    ),
    (1, final value) || [1, final value] => WasmInputFilePath(
      value is String ? value : (value! as ParsedString).value,
    ),
    _ => throw Exception('Invalid JSON $json_'),
  };
}