Expand/Shrink

serialize.e

The file builtins\serialize.e (an autoinclude) provides routines for serialization of any data objects, ie conversion to and from a flat series of bytes.

For instance, when transmitting some value between programs or over a network connection, you might be fine with say 'X', 42, and even "Hello", but are likely to encounter snags with larger integers, fractions, exponents, and most significantly of all, deeply nested sequences.
Perhaps you could use human-readable strings, but that might introduce discrepancies in some extreme values, and will be a bit longer and therefore slower than it really needs to be.
Instead, this component is easy to use and provides exact results with the bonus of needing a bit less space, not that it incorporates any compression techniques along the lines of zip, bz2, or lzma (as yet).

The code is based on and provides partial compatibility for Euphoria’s std\serialize.e

An additional parameter has been added to deserialize to control whether or not the final pos should be returned when processing a sequence input. (The Euphoria version always returns {value,pos} when given a sequence, but not a file.) The deserialize routine has also been enhanced to allow retrieval directly from a raw memory address.

Obviously these routines should not be changed too much otherwise they might stop working with Euphoria-encoded things, but I thought I had better produce a thread-safe version, because chucking big sequences over a slow network connection is precisely the sort of thing that gets relegated to a background thread.

serialize - return the serialized representation of any object, as a sequence of bytes
deserialize - convert a series of bytes back to a standard useable object.