fromJson static method

ComparisonIndex fromJson(
  1. Map<String, Object?> json
)

Throws FormatException on a version this reader does not know: the alternative is handing back an object decoded by guesswork.

Implementation

static ComparisonIndex fromJson(Map<String, Object?> json) {
  var version = json['version'] as int? ?? 1;
  if (version > comparisonReportVersion) {
    throw FormatException(
      'This comparison is version $version and this is a reader for '
      '$comparisonReportVersion. Upgrade the flutterware dependency of '
      'whatever is reading it.',
    );
  }
  var base = json['base'] as String? ?? '';
  var previews = json['previews'] as Map<String, Object?>? ?? const {};
  var scenarios = json['scenarios'] as Map<String, Object?>?;
  return ComparisonIndex(
    version: version,
    base: base,
    against:
        json['against'] as String? ??
        (base.length > 8 ? base.substring(0, 8) : base),
    head: json['head'] as String?,
    headCommit: json['headCommit'] as String?,
    at: switch (json['at']) {
      String at => DateTime.tryParse(at),
      _ => null,
    },
    ms: json['ms'] as int? ?? 0,
    counts: _counts(json['counts']),
    // Absent means `local`: that is what every file written before this key
    // existed is, and an export is the thing that has to say so.
    frames: json['frames'] == 'relative'
        ? ComparisonFrames.relative
        : ComparisonFrames.local,
    exported: ExportedFrames.fromName(json['exported']),
    narrowed: json['narrowed'] == true,
    caveats: [
      for (var caveat in json['caveats'] as List? ?? const []) '$caveat',
    ],
    host: ComparisonHost.fromJson(json['host']),
    timings: ComparisonTimings.fromJson(json['timings']),
    previewsHalf: ComparedHalf.fromJson(previews),
    scenariosHalf: scenarios == null
        ? null
        : ComparedHalf.fromJson(scenarios),
    previewItems: [
      for (var item in previews['items'] as List? ?? const [])
        ComparedItem.fromJson((item as Map).cast<String, Object?>()),
    ],
    scenarios: [
      for (var scenario in scenarios?['items'] as List? ?? const [])
        ScenarioComparison.fromJson(
          (scenario as Map).cast<String, Object?>(),
        ),
    ],
    export: (json['export'] as Map?)?['output'] as String?,
    report: (json['report'] as Map?)?['comment'] as String?,
  );
}