Registers custom serialization and unserialization functions for sending and receiving reference objects.
Arguments
- fns
either a list comprising 2 functions:
serialization function: must accept a reference object (or list of objects) inheriting from ‘class’ and return a raw vector.
unserialization function: must accept a raw vector and return a reference object (or list of reference objects).
or else NULL to reset.- class
the class of reference object (as a character string) that these functions are applied to, e.g. 'ArrowTabular' or 'torch_tensor'.
- vec
[default FALSE] if FALSE the functions must accept and return reference objects individually e.g.
arrow::write_to_raw
andarrow::read_ipc_stream
. If TRUE, the functions are vectorized and must accept and return a list of reference objects, e.g.torch::torch_serialize
andtorch::torch_load
.
Details
Registering new functions replaces any existing registered functions.
This function may be called prior to or after setting daemons, with the registered functions applying across all compute profiles.
Calling without any arguments returns a list comprising the registered values for 'fns', class', and 'vec', or else NULL if not registered.
Examples
reg <- serialization(
list(function(x) serialize(x, NULL), base::unserialize),
class = "example_class"
)
reg
#> [[1]]
#> [[1]][[1]]
#> function(x) serialize(x, NULL)
#> <environment: 0x556a8849e368>
#>
#> [[1]][[2]]
#> function (connection, refhook = NULL)
#> {
#> if (typeof(connection) != "raw" && !is.character(connection) &&
#> !inherits(connection, "connection"))
#> stop("'connection' must be a connection")
#> .Internal(unserialize(connection, refhook))
#> }
#> <bytecode: 0x556a8845c9e8>
#> <environment: namespace:base>
#>
#>
#> [[2]]
#> [1] "example_class"
#>
#> [[3]]
#> [1] FALSE
#>
serialization(NULL)
print(serialization())
#> NULL