fromJson<DataType extends Object> static method
- Map<
String, Object?> json, - StateValueDecoder<
DataType> decodeData
Decodes an explicit replay payload and sidecar image.
Implementation
static TaggedReplayBufferImage<DataType> fromJson<DataType extends Object>(
Map<String, Object?> json,
StateValueDecoder<DataType> decodeData,
) {
_validateHeader(json, kind, version);
final capacity = _positiveInt(json, 'capacity');
final occupied = _decodeTagSet(json, 'occupiedTags', capacity);
final eligible = _decodeTagSet(json, 'eligibleTags', capacity);
final encodedBackoff = json['backoffRemaining'];
if (encodedBackoff is! List<Object?> ||
encodedBackoff.length != capacity ||
encodedBackoff.any((value) => value is! int || value < 0)) {
throw const FormatException(
'backoffRemaining must contain non-negative values for every tag.');
}
final payloads = List<DataType?>.filled(capacity, null);
final reasons = List<int?>.filled(capacity, null);
for (final encoded in _entryList({'entries': json['payloads']})) {
final entry = _entryMap(encoded);
final index = _entryIndex(entry, capacity);
if (payloads[index] != null || reasons[index] != null) {
throw FormatException('Duplicate replay payload index $index.');
}
final reason = entry['reason'];
if (reason is! int) {
throw const FormatException('Replay reason must be an integer.');
}
payloads[index] = decodeData(entry['data']);
reasons[index] = reason;
}
return TaggedReplayBufferImage(
payloads: payloads,
reasons: reasons,
occupiedTags: occupied,
eligibleTags: eligible,
backoffRemaining: encodedBackoff.cast<int>(),
);
}