Object serialization: Difference between revisions

Content added Content deleted
m ("G" before "H")
(added Factor)
Line 813: Line 813:
email: "test+1@localhost.localdomain"
email: "test+1@localhost.localdomain"
Entity: name: "Entity", date: 20111111
Entity: name: "Entity", date: 20111111
</pre>

=={{header|Factor}}==
The <code>serialize</code> vocabulary provides words for serializing and deserializing any Factor object other than continuations. This example demonstrates that objects may be serialized individually, one at a time. In practice, it's probably easier to serialize a collection of objects.
<lang factor>USING: accessors combinators.extras io io.encodings.binary
io.files io.files.info kernel prettyprint serialize ;
IN: rosetta-code.object-serialization

! Define two classes, item and armor. armor is a subclass of
! item.

TUPLE: item name value ;
TUPLE: armor < item physical-resistance fire-resistance ;

! Define boa constructors for both classes using C: shorthand.
! boa means By Order of Arguments, and yes, this is a pun on boa
! constrictors.

C: <item> item
C: <armor> armor

! Create three example items and print them out
! non-destructively.

"Fish scales" 0.05 <item>
"Gold piece" 1 <item>
"Breastplate of Ashannar" 50,000 55 30 <armor>
[ [ . ] keep ] tri@ nl

! Serialize the three objects to a binary file named
! objects.dat.

"Serializing objects to objects.dat . . . " print
"objects.dat" binary [ [ serialize ] tri@ ] with-file-writer

! Check that objects.dat exists.

"objects.dat exists? " write "objects.dat" exists? .
"Size on disk: " write "objects.dat" file-info size>> pprint
" bytes" print nl

! Deserialize three objects from objects.dat.

"Deserializing objects from objects.dat . . . " print nl
"objects.dat" binary [ [ deserialize ] thrice ] with-file-reader

! Print out deserialized objects.

[ . ] tri@</lang>
{{out}}
<pre>
T{ item { name "Fish scales" } { value 0.05 } }
T{ item { name "Gold piece" } { value 1 } }
T{ armor
{ name "Breastplate of Ashannar" }
{ value 50000 }
{ physical-resistance 55 }
{ fire-resistance 30 }
}

Serializing objects to objects.dat . . .
objects.dat exists? t
Size on disk: 206 bytes

Deserializing objects from objects.dat . . .

T{ item { name "Fish scales" } { value 0.05 } }
T{ item { name "Gold piece" } { value 1 } }
T{ armor
{ name "Breastplate of Ashannar" }
{ value 50000 }
{ physical-resistance 55 }
{ fire-resistance 30 }
}
</pre>
</pre>