fromJson static method

Metadata? fromJson(
  1. Map<String, dynamic>? json, {
  2. LinkHrefNormalizer normalizeHref = linkHrefNormalizerIdentity,
})

Parses a Metadata from its RWPM JSON representation.

If the metadata can't be parsed, a warning will be logged with warnings.

Implementation

static Metadata? fromJson(Map<String, dynamic>? json,
    {LinkHrefNormalizer normalizeHref = linkHrefNormalizerIdentity}) {
  if (json == null) {
    return null;
  }
  LocalizedString? localizedTitle =
      LocalizedString.fromJson(json.remove("title"));
  if (localizedTitle == null) {
    Fimber.i("[title] is required $json");
    return null;
  }
  String? identifier = json.remove("identifier") as String?;
  String? type = json.remove("@type") as String?;
  LocalizedString? localizedSubtitle =
      LocalizedString.fromJson(json.remove("subtitle"));
  DateTime? modified = (json.remove("modified") as String?)?.iso8601ToDate();
  DateTime? published =
      (json.remove("published") as String?)?.iso8601ToDate();

  List<String> languages =
      json.optStringsFromArrayOrSingle("language", remove: true);
  LocalizedString? localizedSortAs =
      LocalizedString.fromJson(json.remove("sortAs"));
  List<Subject> subjects = Subject.fromJSONArray(json.remove("subject"),
      normalizeHref: normalizeHref);
  List<Contributor> authors = Contributor.fromJsonArray(json.remove("author"),
      normalizeHref: normalizeHref);
  List<Contributor> translators = Contributor.fromJsonArray(
      json.remove("translator"),
      normalizeHref: normalizeHref);
  List<Contributor> editors = Contributor.fromJsonArray(json.remove("editor"),
      normalizeHref: normalizeHref);
  List<Contributor> artists = Contributor.fromJsonArray(json.remove("artist"),
      normalizeHref: normalizeHref);
  List<Contributor> illustrators = Contributor.fromJsonArray(
      json.remove("illustrator"),
      normalizeHref: normalizeHref);
  List<Contributor> letterers = Contributor.fromJsonArray(
      json.remove("letterer"),
      normalizeHref: normalizeHref);
  List<Contributor> pencilers = Contributor.fromJsonArray(
      json.remove("penciler"),
      normalizeHref: normalizeHref);
  List<Contributor> colorists = Contributor.fromJsonArray(
      json.remove("colorist"),
      normalizeHref: normalizeHref);
  List<Contributor> inkers = Contributor.fromJsonArray(json.remove("inker"),
      normalizeHref: normalizeHref);
  List<Contributor> narrators = Contributor.fromJsonArray(
      json.remove("narrator"),
      normalizeHref: normalizeHref);
  List<Contributor> contributors = Contributor.fromJsonArray(
      json.remove("contributor"),
      normalizeHref: normalizeHref);
  List<Contributor> publishers = Contributor.fromJsonArray(
      json.remove("publisher"),
      normalizeHref: normalizeHref);
  List<Contributor> imprints = Contributor.fromJsonArray(
      json.remove("imprint"),
      normalizeHref: normalizeHref);
  ReadingProgression readingProgression = ReadingProgression.fromValue(
      json.remove("readingProgression") as String?);
  String? description = json.remove("description") as String?;
  double? duration = json.optPositiveDouble("duration", remove: true);
  int? numberOfPages = json.optPositiveInt("numberOfPages", remove: true);

  Map<String, dynamic> belongsToJson =
      (json.remove("belongsTo") as Map<String, dynamic>? ??
          json.remove("belongs_to") as Map<String, dynamic>? ??
          {});
  Map<String, List<Collection>> belongsTo = {};
  for (String key in belongsToJson.keys) {
    if (!belongsToJson.isNull(key)) {
      dynamic value = belongsToJson[key];
      belongsTo[key] =
          Contributor.fromJsonArray(value, normalizeHref: normalizeHref);
    }
  }

  return Metadata(
    identifier: identifier,
    type: type,
    localizedTitle: localizedTitle,
    localizedSubtitle: localizedSubtitle,
    localizedSortAs: localizedSortAs,
    modified: modified,
    published: published,
    languages: languages,
    subjects: subjects,
    authors: authors,
    translators: translators,
    editors: editors,
    artists: artists,
    illustrators: illustrators,
    letterers: letterers,
    pencilers: pencilers,
    colorists: colorists,
    inkers: inkers,
    narrators: narrators,
    contributors: contributors,
    publishers: publishers,
    imprints: imprints,
    readingProgression: readingProgression,
    description: description,
    duration: duration,
    numberOfPages: numberOfPages,
    belongsTo: belongsTo,
    otherMetadata: json,
  );
}