decodeAnnotations function
Decodes an encodeAnnotations envelope. Tolerant of per-entry damage: invalid entries are
skipped, while a non-map or undecodable envelope, or an unknown (or missing) formatVersion,
yields null. Missing collections decode to empty ones. Round-trips encodeAnnotations
losslessly.
Implementation
AnnotationCollection? decodeAnnotations(final String json) {
final Object? decoded;
try {
decoded = jsonDecode(json);
} on FormatException {
return null;
}
if (decoded is! Map<String, dynamic>) return null;
if (decoded['formatVersion'] != _annotationFormatVersion) return null;
return AnnotationCollection(
highlights: _recordsFromJson(decoded['highlights'], _highlightFromJson),
bookmarks: _recordsFromJson(decoded['bookmarks'], _bookmarkFromJson),
);
}