serialize {base} | R Documentation |
A simple low-level interface for serializing to connections.
serialize(object, connection, ascii = FALSE, refhook = NULL)
unserialize(connection, refhook = NULL)
object |
R object to serialize. |
connection |
an open connection or (for |
ascii |
a logical. If |
refhook |
a hook function for handling reference objects. |
The function serialize
writes object
to the specified
connection. If connection
is NULL
then object
is
serialized to a raw vector, which is returned as the result of
serialize
.
Sharing of reference objects is preserved within the object but not
across separate calls to serialize
.
unserialize
reads an object (as written by serialize
)
from connection
or a raw vector.
The refhook
functions can be used to customize handling of
non-system reference objects (all external pointers and weak
references, and all environments other than name space and package
environments and .GlobalEnv
). The hook function for
serialize
should return a raw vector for references it
wants to handle; otherwise it should return NULL
. The hook for
unserialize
will be called with raw vectors supplied to
serialize
and should return an appropriate object.
For a text-mode connection, the default value of ascii
is set
to TRUE
: only ASCII representations can be written to text-mode
connections and attempting to use ascii = FALSE
will throw an
error.
For serialize
, NULL
unless connection = NULL
, when
the result is returned in a raw vector.
For unserialize
an R object.
These functions are still experimental. Names, interfaces and
values might change in future versions (and the value of
serialize
was changed for R 2.4.0).
A raw vector is limited to 2^{31} - 1
bytes, but R
objects can exceed this and their serializations will normally be
larger than the objects.
x <- serialize(list(1,2,3), NULL)
unserialize(x)