serializeResponse method
Future<Response>
serializeResponse(
- FutureOr valueOrFuture,
- TypeCapture typeCapture,
- String? explicitContentType
)
Implementation
Future<Response> serializeResponse(FutureOr<dynamic> valueOrFuture, TypeCapture typeCapture,
String? explicitContentType) async {
var type = typeCapture.typeArgument;
if (type == dynamic) {
type = (const TypeToken<void>()).typeArgument;
assert((){
logger.warning("Rewriting 'dynamic' response type to 'void'.");
return true;
}());
}
var value = await valueOrFuture;
if (type == Response || (type == dynamic && value is Response)) {
return value;
}
if ((type == List<int>) || (type == dynamic && value is List<int>)) {
return Response.ok(value, headers: {
"Content-Type": "application/octet-stream"
});
}
if (value is Stream) value = await value.toList();
var context =
SerializationContext(MarshalTarget(typeCapture), explicitContentType, {}, marshal);
var serializer = marshal.findSerializer(context);
if (serializer == null) {
throw Exception("Didn't find matching serializer for ${typeCapture.typeArgument}");
}
context.mime ??= serializer.outputMime;
var data = serializer.serialize(value, context);
assert((){
if (serializer.outputMime == null) {
logger.warning("The converter $serializer for ${typeCapture.typeArgument} "
"doesn't specify an outputMime type so text/plain will be used. "
"Consider explicitly defining a content type.");
}
return true;
}());
return Response.ok(data, headers: {"Content-Type": context.mime ?? "text/plain"});
}