parseSnapshot function

SplayTreeMap<String, String> parseSnapshot(
  1. String text
)

A committed snapshot's text, each entry re-encoded compactly. Only white space is normalised: key order and number spelling are kept, so an entry written by hand with 1 where geometrySnapshot writes 1.0 reads as changed (the comparison fails closed). Throws FormatException when text is not a JSON object of objects.

Implementation

SplayTreeMap<String, String> parseSnapshot(String text) {
  final Object? json = jsonDecode(text);
  if (json is! Map<String, Object?>) {
    throw const FormatException('a geometry snapshot is a JSON object');
  }
  final snapshot = SplayTreeMap<String, String>();
  for (final MapEntry(:key, :value) in json.entries) {
    if (value is! Map) {
      throw FormatException('the snapshot entry "$key" is not an object');
    }
    snapshot[key] = jsonEncode(value);
  }
  return snapshot;
}