RosettaCodeData/Task/Object-serialization/Phix/object-serialization.phix

38 lines
803 B
Plaintext

include builtins\serialize.e
function randobj()
-- test function (generate some random garbage)
object res
if rand(10)<=3 then -- make sequence[1..3]
res = {}
for i=1 to rand(3) do
res = append(res,randobj())
end for
elsif rand(10)<=3 then -- make string
res = repeat('A'+rand(10),rand(10))
else
res = rand(10)/2 -- half int/half float
end if
return res
end function
object o1 = randobj(),
o2 = randobj(),
o3 = randobj()
pp({o1,o2,o3},{pp_Nest,1})
integer fh = open("objects.dat", "wb")
puts(fh, serialize(o1))
puts(fh, serialize(o2))
puts(fh, serialize(o3))
close(fh)
?"==="
fh = open("objects.dat", "rb")
?deserialize(fh)
?deserialize(fh)
?deserialize(fh)
close(fh)
{} = delete_file("objects.dat")