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 the Marshal.from_* functions is given as 'a, but this is misleading: the returned Caml value does not possess type 'a for all '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.
The representation of marshaled values is not human-readable, and uses bytes that are not printable characters. Therefore, input and output channels used in conjunction with Marshal.to_channel and Marshal.from_channel must be opened in binary mode, using e.g. open_out_bin or 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.
type extern_flags = No_sharing (* Don't preserve sharing *) | Closures (* Send function closures *)
val to_channel: out_channel -> 'a -> extern_flags list -> unit
If flags does not contain Marshal.No_sharing, circularities 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. If flags contains 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.
If flags does not contain Marshal.Closures, marshaling fails when it encounters a functional value inside v: only ``pure'' data structures, containing neither functions nor objects, can safely be transmitted between different programs. If flags contains Marshal.Closures, 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
val to_buffer: string -> int -> int -> 'a -> extern_flags list -> int
val from_channel: in_channel -> 'a
val from_string: string -> int -> 'a
val header_size : int val data_size : string -> int -> int val total_size : string -> int -> int
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 representation using Marshal.data_size, make sure the buffer is large enough to hold the variable size, then read it, and finally call Marshal.from_string to unmarshal the value.