SceneBudget.fromJson constructor

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

Reads a budget from a decoded JSON object.

Throws FormatException rather than a bare cast failure when a field is missing or the wrong type, so a malformed budgets.json names the scene and the field it failed on instead of an opaque type '_Map' is not a subtype.

Implementation

factory SceneBudget.fromJson(Map<String, Object?> json) {
  final sceneId = json['sceneId'];
  if (sceneId is! String) {
    throw const FormatException(
      'SceneBudget.fromJson: "sceneId" must be a string.',
    );
  }
  double field(String name) {
    final value = json[name];
    if (value is num) {
      return value.toDouble();
    }
    throw FormatException(
      'SceneBudget.fromJson("$sceneId"): "$name" must be a number.',
    );
  }

  return SceneBudget(
    sceneId: sceneId,
    rasterP90Ms: field('rasterP90Ms'),
    rasterP99Ms: field('rasterP99Ms'),
    buildP90Ms: field('buildP90Ms'),
  );
}