parse static method

StateStore parse(
  1. String content
)

Implementation

static StateStore parse(String content) {
  final Object? root;
  try {
    root = jsonDecode(content);
  } on FormatException catch (e) {
    throw FormatException(
      '.dialect/state.json is not valid JSON: ${e.message}',
    );
  }
  if (root is! Map<String, Object?>) {
    throw const FormatException('.dialect/state.json must be a JSON object.');
  }

  final version = root['version'];
  final checks = <String, AckRecord>{};
  final rawChecks = root['checks'];
  if (rawChecks is Map<String, Object?>) {
    for (final entry in rawChecks.entries) {
      final v = entry.value;
      if (v is Map<String, Object?>) {
        checks[entry.key] = AckRecord.fromJson(v);
      }
    }
  }

  final extras = <String, Object?>{};
  for (final entry in root.entries) {
    if (entry.key == 'version' || entry.key == 'checks') continue;
    extras[entry.key] = entry.value;
  }

  return StateStore(
    version: version is int ? version : 1,
    checks: checks,
    extras: extras,
  );
}