fromJson static method
Parses a profile from JSON produced by toJson.
Throws FormatException for missing/wrong-type fields or wrong schema version.
Implementation
static MemProfile fromJson(Map<String, dynamic> json) {
try {
final schema = json['schema'];
if (schema == null) {
throw const FormatException('Missing required field: schema');
}
if (schema != _schemaVersion) {
throw FormatException(
'Incompatible profile schema: expected $_schemaVersion, got $schema',
);
}
final isolateId = json['isolateId'];
if (isolateId is! String) {
throw const FormatException('Missing or invalid field: isolateId');
}
final isolateName = json['isolateName'];
if (isolateName is! String) {
throw const FormatException('Missing or invalid field: isolateName');
}
final capturedAtRaw = json['capturedAt'];
if (capturedAtRaw is! String) {
throw const FormatException('Missing or invalid field: capturedAt');
}
final capturedAt = DateTime.tryParse(capturedAtRaw);
if (capturedAt == null) {
throw FormatException('Invalid capturedAt value: $capturedAtRaw');
}
final rawClasses = json['classes'];
if (rawClasses is! List) {
throw const FormatException('Missing or invalid field: classes');
}
final classes = rawClasses.map((e) {
if (e is! Map<String, dynamic>) {
throw const FormatException('Invalid class entry in classes list');
}
final className = e['className'];
if (className is! String) {
throw const FormatException('Missing or invalid field: className');
}
final libraryUri = e['libraryUri'];
if (libraryUri is! String) {
throw const FormatException('Missing or invalid field: libraryUri');
}
final instancesCurrent = e['instancesCurrent'];
if (instancesCurrent is! int) {
throw const FormatException('Missing or invalid field: instancesCurrent');
}
final bytesCurrent = e['bytesCurrent'];
if (bytesCurrent is! int) {
throw const FormatException('Missing or invalid field: bytesCurrent');
}
return ClassAlloc(
className: className,
libraryUri: libraryUri,
instancesCurrent: instancesCurrent,
bytesCurrent: bytesCurrent,
);
}).toList();
return MemProfile(
isolateId: isolateId,
isolateName: isolateName,
capturedAt: capturedAt,
classes: classes,
);
} on FormatException {
rethrow;
} catch (e) {
throw FormatException('Failed to parse profile: $e');
}
}