module Marshal:Marshaling of data structures.
This module provides functions to encode arbitrary data structures as sequences of bytes, which can then be written on a file or sent over a pipe or network connection. The bytes can then be read back later, possibly in another process, and decoded back into a data structure. The format for the byte sequences is compatible across all machines for a given version of Objective Caml.
Warning: marshaling is currently not type-safe. The type
of marshaled data is not transmitted along the value of the data,
making it impossible to check that the data read back possesses the
type expected by the context. In particular, the result type of
Marshal.from_* functions is given as
'a, but this is
misleading: the returned Caml value does not possess type
'a; it has one, unique type which cannot be determined
at compile-type. The programmer should explicitly give the expected
type of the returned value, using the following syntax:
(Marshal.from_channel chan : type). Anything can happen at run-time if the object in the file does not belong to the given type.
Marshal.from_channelmust be opened in binary mode, using e.g.
open_in_bin; channels opened in text mode will cause unmarshaling errors on platforms where text channels behave differently than binary channels, e.g. Windows.
||Don't preserve sharing||
||Send function closures||
val to_channel :
out_channel -> 'a -> extern_flags list -> unit
Marshal.to_channel chan v flagswrites the representation of
flagsargument is a possibly empty list of flags that governs the marshaling behavior with respect to sharing and functional values.
flags does not contain
and sharing inside the value
v are detected and preserved
in the sequence of bytes produced. In particular, this
guarantees that marshaling always terminates. Sharing
between values marshaled by successive calls to
Marshal.to_channel is not detected, though.
Marshal.No_sharing, sharing is ignored.
This results in faster marshaling if
v contains no shared
substructures, but may cause slower marshaling and larger
byte representations if
v actually contains sharing,
or even non-termination if
v contains cycles.
flags does not contain
marshaling fails when it encounters a functional value
v: only ``pure'' data structures, containing neither
functions nor objects, can safely be transmitted between
different programs. If
functional values will be marshaled as a position in the code
of the program. In this case, the output of marshaling can
only be read back in processes that run exactly the same program,
with exactly the same compiled code. (This is checked
at un-marshaling time, using an MD5 digest of the code
transmitted along with the code position.)
val to_string :
'a -> extern_flags list -> string
Marshal.to_string v flagsreturns a string containing the representation of
vas a sequence of bytes. The
flagsargument has the same meaning as for
val to_buffer :
string -> int -> int -> 'a -> extern_flags list -> int
Marshal.to_buffer buff ofs len v flagsmarshals the value
v, storing its byte representation in the string
buff, starting at character number
ofs, and writing at most
lencharacters. It returns the number of characters actually written to the string. If the byte representation of
vdoes not fit in
lencharacters, the exception
val from_channel :
in_channel -> 'a
Marshal.from_channel chanreads from channel
chanthe byte representation of a structured value, as produced by one of the
Marshal.to_*functions, and reconstructs and returns the corresponding value.
val from_string :
string -> int -> 'a
Marshal.from_string buff ofsunmarshals a structured value like
Marshal.from_channeldoes, except that the byte representation is not read from a channel, but taken from the string
buff, starting at position
val header_size :
Marshal.header_sizeis the size, in characters, of the header.
buff ofsis the size, in characters, of the data part, assuming a valid header is stored in
buffstarting at position
buff ofsis the total size, in characters, of the marshaled value. Both
ofsdoes not contain a valid header.
To read the byte representation of a marshaled value into
a string buffer, the program needs to read first
Marshal.header_size characters into the buffer,
then determine the length of the remainder of the
make sure the buffer is large enough to hold the remaining
data, then read it, and finally call
to unmarshal the value.
val data_size :
string -> int -> int
val total_size :
string -> int -> int