BenchmarkBudgets.fromJsonString constructor

BenchmarkBudgets.fromJsonString(
  1. String source
)

Parses the checked-in budgets file.

The file's shape is {"scenes": [ {...SceneBudget...}, ... ]}, plus an optional "_provenance" string field the schema ignores -- present in the shipped file to record where its numbers came from without every reader needing a side document to find out.

Implementation

factory BenchmarkBudgets.fromJsonString(String source) {
  final decoded = jsonDecode(source);
  if (decoded is! Map<String, Object?>) {
    throw const FormatException(
      'budgets.json: expected a JSON object at the top level.',
    );
  }
  final scenes = decoded['scenes'];
  if (scenes is! List) {
    throw const FormatException(
      'budgets.json: expected a "scenes" array.',
    );
  }
  final budgets = <String, SceneBudget>{};
  for (final entry in scenes) {
    if (entry is! Map) {
      throw const FormatException(
        'budgets.json: every entry in "scenes" must be an object.',
      );
    }
    final budget = SceneBudget.fromJson(Map<String, Object?>.from(entry));
    budgets[budget.sceneId] = budget;
  }
  return BenchmarkBudgets(budgets);
}