Object serialization: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) m (→{{header|Perl 6}}: fix formatting) |
|||
Line 1,802: | Line 1,802: | ||
Rectangle2 is of type (Rectangle) |
Rectangle2 is of type (Rectangle) |
||
Rectangle2 area is 100</pre> |
Rectangle2 area is 100</pre> |
||
=={{header|Phix}}== |
|||
The serialize() and deserialize() functions handle any kind of data. Whether they are binary, text, data types, classes, |
|||
or whatever you choose to treat as such, is up to you. |
|||
<lang Phix>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")</lang> |
|||
{{out}} |
|||
<pre> |
|||
{1.5, |
|||
{"JJJJJJJJ", "FFFF", {4}}, |
|||
{{{0.5}, 3}, 3,1}} |
|||
"===" |
|||
1.5 |
|||
{"JJJJJJJJ","FFFF",{4}} |
|||
{{{0.5},3},3,1} |
|||
</pre> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |