A serializer is responsible for translating a generated R value into output
that a remote user can understand. For instance, the serializer_json
serializes R objects into JSON before returning them to the user. The list of
available serializers in plumber is global.
Arguments
- name
The name of the serializer (character string)
- serializer
The serializer function to be added. This function should accept arguments that can be supplied when
plumb()
ing a file. This function should return a function that accepts four arguments:value
,req
,res
, anderrorHandler
. Seeprint(serializer_json)
for an example.- verbose
Logical value which determines if a message should be printed when overwriting serializers
Details
There are three main building-block serializers:
serializer_headers
: the base building-block serializer that is required to haveas_attachment()
workserializer_content_type()
: for setting the content type. (Callsserializer_headers()
)serializer_device()
: add endpoint hooks to turn a graphics device on and off in addition to setting the content type. (Usesserializer_content_type()
)
Functions
register_serializer()
: Register a serializer with a nameregistered_serializers()
: Return a list of all registered serializers
Examples
# `serializer_json()` calls `serializer_content_type()` and supplies a serialization function
print(serializer_json)
#> function (..., type = "application/json")
#> {
#> serializer_content_type(type, function(val) {
#> toJSON(val, ...)
#> })
#> }
#> <bytecode: 0x55905e115a38>
#> <environment: namespace:plumber>
# serializer_content_type() calls `serializer_headers()` and supplies a serialization function
print(serializer_content_type)
#> function (type, serialize_fn = identity)
#> {
#> if (missing(type)) {
#> stop("You must provide the custom content type to the serializer_content_type")
#> }
#> stopifnot(length(type) == 1)
#> stopifnot(is.character(type))
#> stopifnot(nchar(type) > 0)
#> serializer_headers(list(`Content-Type` = type), serialize_fn)
#> }
#> <bytecode: 0x55905e0f45f8>
#> <environment: namespace:plumber>