parseComparisons function
Parses tables written as JSON: one table per line (as --save and the
json format write them), a JSON array of tables, or a single table.
Throws:
- FormatException if the text is not valid JSON, or not tables.
Implementation
List<Comparison> parseComparisons(String text) {
final trimmed = text.trim();
if (trimmed.isEmpty) return [];
Object? decoded;
try {
decoded = jsonDecode(trimmed);
} on FormatException {
// Several lines, one table each.
return [
for (final line in const LineSplitter().convert(trimmed))
if (line.trim().isNotEmpty)
Comparison.fromJson(_object(jsonDecode(line))),
];
}
return switch (decoded) {
List<Object?> tables => [
for (final t in tables) Comparison.fromJson(_object(t)),
],
_ => [Comparison.fromJson(_object(decoded))],
};
}