TarHeader.fromJson constructor

TarHeader.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 TarHeader.fromJson(Object? json_) {
  final json = json_ is Map
      ? _spec.fields.map((f) => json_[f.label]).toList(growable: false)
      : json_;
  return switch (json) {
    [
      final entryType,
      final bytes,
      final pathBytes,
      final path,
      final linkNameBytes,
      final linkName,
      final mode,
      final uid,
      final gid,
      final mtime,
      final usernameBytes,
      final username,
      final groupnameBytes,
      final groupname,
      final deviceMajor,
      final deviceMinor,
      final cksum,
      final formatErrors
    ] ||
    (
      final entryType,
      final bytes,
      final pathBytes,
      final path,
      final linkNameBytes,
      final linkName,
      final mode,
      final uid,
      final gid,
      final mtime,
      final usernameBytes,
      final username,
      final groupnameBytes,
      final groupname,
      final deviceMajor,
      final deviceMinor,
      final cksum,
      final formatErrors
    ) =>
      TarHeader(
        entryType: TarEntryType.fromJson(entryType),
        bytes: (bytes is Uint8List
            ? bytes
            : Uint8List.fromList((bytes! as List).cast())),
        pathBytes: (pathBytes is Uint8List
            ? pathBytes
            : Uint8List.fromList((pathBytes! as List).cast())),
        path: Option.fromJson(
            path,
            (some) =>
                some is String ? some : (some! as ParsedString).value).value,
        linkNameBytes: Option.fromJson(
            linkNameBytes,
            (some) => (some is Uint8List
                ? some
                : Uint8List.fromList((some! as List).cast()))).value,
        linkName: Option.fromJson(
            linkName,
            (some) =>
                some is String ? some : (some! as ParsedString).value).value,
        mode: Option.fromJson(mode, (some) => some! as int).value,
        uid: Option.fromJson(uid, (some) => bigIntFromJson(some)).value,
        gid: Option.fromJson(gid, (some) => bigIntFromJson(some)).value,
        mtime: Option.fromJson(mtime, (some) => bigIntFromJson(some)).value,
        usernameBytes: Option.fromJson(
            usernameBytes,
            (some) => (some is Uint8List
                ? some
                : Uint8List.fromList((some! as List).cast()))).value,
        username: Option.fromJson(
            username,
            (some) =>
                some is String ? some : (some! as ParsedString).value).value,
        groupnameBytes: Option.fromJson(
            groupnameBytes,
            (some) => (some is Uint8List
                ? some
                : Uint8List.fromList((some! as List).cast()))).value,
        groupname: Option.fromJson(
            groupname,
            (some) =>
                some is String ? some : (some! as ParsedString).value).value,
        deviceMajor:
            Option.fromJson(deviceMajor, (some) => some! as int).value,
        deviceMinor:
            Option.fromJson(deviceMinor, (some) => some! as int).value,
        cksum: Option.fromJson(cksum, (some) => some! as int).value,
        formatErrors: (formatErrors! as Iterable)
            .map((e) => e is String ? e : (e! as ParsedString).value)
            .toList(),
      ),
    _ => throw Exception('Invalid JSON $json_')
  };
}