fromJson<DataType extends Object> static method
- Map<
String, Object?> json, - StateValueDecoder<
DataType> decodeData
Decodes an explicit payload and allocation-sidecar image.
Implementation
static TaggedStatePayloadSidecarImage<DataType>
fromJson<DataType extends Object>(
Map<String, Object?> json,
StateValueDecoder<DataType> decodeData,
) {
_validateHeader(json, kind, version);
final capacity = _positiveInt(json, 'capacity');
final encodedTags = json['allocatedTags'];
if (encodedTags is! List<Object?>) {
throw const FormatException('allocatedTags must be a list.');
}
final allocated = <int>{};
for (final encoded in encodedTags) {
if (encoded is! int || encoded < 0 || encoded >= capacity) {
throw const FormatException('allocatedTags contains an invalid tag.');
}
if (!allocated.add(encoded)) {
throw FormatException('Duplicate allocated tag $encoded.');
}
}
final payloads = List<DataType?>.filled(capacity, null);
for (final encoded in _entryList({'entries': json['payloads']})) {
final entry = _entryMap(encoded);
final index = _entryIndex(entry, capacity);
if (payloads[index] != null) {
throw FormatException('Duplicate payload index $index.');
}
payloads[index] = decodeData(entry['data']);
}
return TaggedStatePayloadSidecarImage(
payloads: payloads,
allocatedTags: allocated,
);
}