fromJson static method

PublishModel fromJson(
  1. Object? json
)

Decodes a publish model of schema kPublishSchemaVersion. A map of any other schema, or with no schema, is refused: it throws an UnsupportedSchemaException. Otherwise it never throws (JSON that is not a map is an empty model).

Implementation

static PublishModel fromJson(Object? json) {
  final m = readMap(json) ?? const {};
  if (json is Map) requireSchema(m, what: 'The publish model', version: kPublishSchemaVersion);
  final layouts = readMap(m['layouts']) ?? const {};
  final fonts = readMap(m['fonts']) ?? const {};
  final components = readMap(m['components']) ?? const {};
  return PublishModel(
    projectId: readString(m['projectId']) ?? '',
    slug: readString(m['slug']),
    entryLayoutId: readString(m['entryLayoutId']),
    entryPageId: readString(m['entryPageId']),
    entryCanvasId: readString(m['entryCanvasId']),
    pages: readStrings(m['pages']),
    layouts: {
      for (final e in layouts.entries)
        if (PublishLayoutInfo.fromJson(e.value, id: e.key) case final PublishLayoutInfo l) e.key: l,
    },
    layoutIDMap: {
      for (final e in (readMap(m['layoutIDMap']) ?? const {}).entries)
        if (e.value is String) e.key: e.value as String,
    },
    disabledLayouts: readStrings(m['disabledLayouts']),
    overlayLayoutIds: readStrings(m['overlayLayoutIds']),
    backdropLayoutIds: readStrings(m['backdropLayoutIds']),
    theme: ThemeTokens.fromJson(m['theme']),
    fonts: {
      for (final e in fonts.entries)
        if (PublishFont.fromJson(e.value, id: e.key) case final PublishFont f) e.key: f,
    },
    apis: readMap(m['apis']) ?? const {},
    variables: readMap(m['variables']) ?? const {},
    components: {
      for (final e in components.entries)
        if (PublishComponentInfo.fromJson(e.value, id: e.key) case final PublishComponentInfo c) e.key: c,
    },
    appIdentity: AppIdentity.fromJson(m['appIdentity']),
    breakpoints: Breakpoints.fromJson(m['breakpoints']),
    lastUpdated: readTimestamp(m['lastUpdated']),
    extra: {
      for (final e in m.entries)
        if (!_keys.contains(e.key)) e.key: e.value,
    },
  );
}