toJson method

Map<String, Object?> toJson({
  1. bool compact = false,
})

The tree as JSON, in one of two spellings.

Verbose is the original and the default: every node's id absolute, every source its own object with a whole file:// path. It is what the panel, the scenario artifacts and the comparison caches have always been written in, and changing it under them would be changing files already on disk.

Compact is the same tree written for a reader that pays per byte. Two substitutions, both undone by fromJson before anything downstream sees a node:

  • Ids relative to the parent. A node twenty levels down spells its id 0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/1/0 and its child spells the same twenty characters again plus one. Measured: 34 KB of a 234 KB read, for a prefix the reader already has.
  • A file table. 436 nodes of the Changes screen named 13 distinct files between them, and repeated the absolute path of one on every node: 71 KB, 30% of the read, to say changes_screen.dart 153 times. Compact writes files once and "source": "0:402:11" per node.

Sub-pixel geometry is rounded to two decimals here too — 7.26 rather than 7.261507987976074. Only in this spelling: the comparison caches diff trees field by field and a rounded number would read as a change.

Implementation

Map<String, Object?> toJson({bool compact = false}) {
  if (!compact) {
    return {'entry': entryId, 'format': ?format, 'root': root?.toJson()};
  }
  var files = <String, int>{};
  var written = root?._toCompactJson('', files);
  return {
    'entry': entryId,
    'format': ?format,
    'compact': true,
    // Filled by the walk above, so it is read after it.
    'files': [for (var file in files.keys) file],
    'root': written,
  };
}