decodeBallFileJson function
Decodes a proto3-JSON ball file (an Any with an @type field).
Implementation
BallFile decodeBallFileJson(Object? json) {
if (json is! Map) {
throw BallFileFormatException('ball file JSON must be an object');
}
final type = json['@type'];
if (type is! String) {
throw BallFileFormatException(
'ball file JSON is not self-describing: missing "@type" '
'(expected a google.protobuf.Any envelope)',
);
}
final body = Map<String, dynamic>.from(json)..remove('@type');
if (_isProgramUrl(type)) {
return BallProgramFile(
Program()..mergeFromProto3Json(body, ignoreUnknownFields: true),
);
}
if (_isModuleUrl(type)) {
return BallModuleFile(
Module()..mergeFromProto3Json(body, ignoreUnknownFields: true),
);
}
throw BallFileFormatException('unknown ball file @type: "$type"');
}