#* @get /json
#* @serializer unboxedJSON
function() {
return(iris)
}
#* @get /csv
#* @serializer csv list(type="text/plain; charset=UTF-8")
function() {
return(iris)
}
#* @param type csv or json
#* @get /data
function(type = 'csv') {
if (type == 'cvs') {
#* @serializer csv list(type="text/plain; charset=UTF-8")
return(iris)
} else {
#* @serializer unboxedJSON
return(iris)
}
}
The first 2 endpoints above each work fine, but the 3rd endpoint does not work as the #* @serializer unboxedJSON
cannot be inside the function it seems. However, it would be great if I could somehow have a single endpoint which handles serializing before returning. plumber
has plumber::serializer_csv
and plumber::serializer_unboxed_json()
and all of their serializers as functions, however I'm not sure how to use them inside the endpoint functions (or if this is even possible)
Thanks!
You can create a custom serializer that changes the serialization format depending on the the response object. Here is an example. In this case, I attach an attribute to my response that provides the format that I want: