fromJson static method

Manifest? fromJson(
  1. Map<String, dynamic>? json, {
  2. bool packaged = false,
})

Parses a Publication from its RWPM JSON representation.

If the publication can't be parsed, a warning will be logged with warnings. https://readium.org/webpub-manifest/ https://readium.org/webpub-manifest/schema/publication.schema.json

Implementation

static Manifest? fromJson(
  Map<String, dynamic>? json, {
  bool packaged = false,
}) {
  if (json == null) {
    return null;
  }
  String baseUrl;
  if (packaged) {
    baseUrl = "/";
  } else {
    String? href = Link.fromJSONArray(json.optJSONArray("links"))
        .firstWithRel("self")
        ?.href;
    baseUrl = href?.let(
            (it) => Uri.tryParse(it)?.removeLastComponent().toString()) ??
        "/";
  }

  List<String> context =
      json.optStringsFromArrayOrSingle("@context", remove: true);
  Metadata? metadata = Metadata.fromJson(
      json.remove("metadata") as Map<String, dynamic>?,
      normalizeHref: normalizeHref(baseUrl));
  if (metadata == null) {
    Fimber.i("[metadata] is required $json");
    return null;
  }

  List<Link> links = Link.fromJSONArray(
          json.remove("links") as List<dynamic>?,
          normalizeHref: normalizeHref(baseUrl))
      .map((it) => (!packaged || !it.rels.contains("self"))
          ? it
          : it.copy(
              rels: it.rels
                ..remove("self")
                ..add("alternate")))
      .toList();
  // [readingOrder] used to be [spine], so we parse [spine] as a fallback.
  List<dynamic>? readingOrderJSON =
      (json.remove("readingOrder") ?? json.remove("spine")) as List<dynamic>?;
  List<Link> readingOrder = Link.fromJSONArray(readingOrderJSON,
          normalizeHref: normalizeHref(baseUrl))
      .where((it) => it.type != null)
      .toList();

  List<Link> resources = Link.fromJSONArray(
          json.remove("resources") as List<dynamic>?,
          normalizeHref: normalizeHref(baseUrl))
      .where((it) => it.type != null)
      .toList();

  List<Link> tableOfContents = Link.fromJSONArray(
      json.remove("toc") as List<dynamic>?,
      normalizeHref: normalizeHref(baseUrl));

  // Parses subcollections from the remaining JSON properties.
  Map<String, List<PublicationCollection>> subcollections =
      PublicationCollection.collectionsFromJSON(json,
          normalizeHref: normalizeHref(baseUrl));

  return Manifest(
      context: context,
      metadata: metadata,
      links: links,
      readingOrder: readingOrder,
      resources: resources,
      tableOfContents: tableOfContents,
      subcollections: subcollections);
}