# serialize

Definition: | sequence s = serialize(object x)
Description: | Converts any object to a serialized (flat series of bytes) version of it. x: any valid object (integer, floating point, string, or [nested] sequence). Returns: the serialized version of x.
pwa/p2js: | Supported, note however that no special effort has been made to avoid calling atom_to_float80(), which is not supported under pwa/p2js, for atoms that do not fit in 64 bits. Running some things/atoms through float64_to_atom(atom_to_float64()) to crop precision might in some cases improve matters, not that I’ve tried that.
Comments: | A serialized object is one that has been converted to a set of byte values. It can then be written directly out to a file for storage, sent over’tinternet, poked into shared memory, etc. You can use the deserialize function to convert it back into a standard phix object.
Example 1: |
    integer fh = open("cust.dat", "wb")
    puts(fh, serialize(FirstName))
    puts(fh, serialize(LastName))
    puts(fh, serialize(PhoneNumber))
    puts(fh, serialize(Address))
    close(fh)

    fh = open("cust.dat", "rb")
    FirstName = deserialize(fh)
    LastName = deserialize(fh)
    PhoneNumber = deserialize(fh)
    Address = deserialize(fh)
    close(fh)
Example 2: |
    integer fh
    fh = open("cust.dat", "wb")
    puts(fh, serialize({FirstName,LastName,PhoneNumber,Address}))
    close(fh)

    fh = open("cust.dat", "rb")
    {FirstName,LastName,PhoneNumber,Address} = deserialize(fh)
    close(fh)
Example 3: |
    include builtins\serialize.e
    sequence s = serialize({"Pete",{1,0,-1},PI,-PI})
    ?s      -- {254,4,254,4,89,110,125,110,254,3,10,9,8,253,24,45,
            --  68,84,251,33,9,64,253,24,45,68,84,251,33,9,192}
    ?deserialize(s) -- {"Pete",{1,0,-1},3.141592654,-3.141592654}
See Also: | deserialize
Technicalia | Technically the result is a dword-sequence, but can be treated as a binary string that only contains integer values in the range 0..255.