WitFile.fromJson constructor

WitFile.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 WitFile.fromJson(Object? json_) {
  final json = json_ is Map
      ? _spec.fields.map((f) => json_[f.label]).toList(growable: false)
      : json_;
  return switch (json) {
    [final path, final contents] || (final path, final contents) => WitFile(
        path: path is String ? path : (path! as ParsedString).value,
        contents:
            contents is String ? contents : (contents! as ParsedString).value,
      ),
    _ => throw Exception('Invalid JSON $json_')
  };
}