fromJson static method

Contributor? fromJson(
  1. dynamic json, {
  2. LinkHrefNormalizer normalizeHref = linkHrefNormalizerIdentity,
})
override

Parses a Contributor from its RWPM JSON representation.

A contributor can be parsed from a single string, or a full-fledged object. The links' href and their children's will be normalized recursively using the provided normalizeHref closure. If the contributor can't be parsed, a warning will be logged with warnings.

Implementation

static Contributor? fromJson(dynamic json,
    {LinkHrefNormalizer normalizeHref = linkHrefNormalizerIdentity}) {
  if (json == null) {
    return null;
  }

  LocalizedString? localizedName;
  if (json is String) {
    localizedName = LocalizedString.fromJson(json);
  } else if (json is Map<String, dynamic>) {
    localizedName = LocalizedString.fromJson(json.opt("name"));
  }
  if (localizedName == null) {
    Fimber.i("[name] is required");
    return null;
  }

  Map<String, dynamic> jsonObject =
      (json is Map<String, dynamic>) ? json : {};
  return Contributor(
      localizedName: localizedName,
      identifier: jsonObject.optNullableString("identifier"),
      localizedSortAs: LocalizedString.fromJson(jsonObject.remove("sortAs")),
      roles: jsonObject.optStringsFromArrayOrSingle("role").toSet(),
      position: jsonObject.optNullableDouble("position"),
      links: Link.fromJSONArray(jsonObject.optJSONArray("links"),
          normalizeHref: normalizeHref));
}