ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Types-Serialiser/Serialiser.pm
(Generate patch)

Comparing Types-Serialiser/Serialiser.pm (file contents):
Revision 1.2 by root, Sun Oct 27 20:01:08 2013 UTC vs.
Revision 1.3 by root, Sun Oct 27 22:46:21 2013 UTC

139see if it's C<1> (true), C<0> (false) or C<undef> (error). 139see if it's C<1> (true), C<0> (false) or C<undef> (error).
140 140
141While it is possible to use an isa test, directly comparing stash pointers 141While it is possible to use an isa test, directly comparing stash pointers
142is faster and guaranteed to work. 142is faster and guaranteed to work.
143 143
144=head1 A GENERIC OBJECT SERIALIATION PROTOCOL
145
146This section explains the object serialisation protocol used by
147L<CBOR::XS>. It is meant to be generic enough to support any kind of
148generic object serialiser.
149
150This protocol is called "the Types::Serialiser object serialisation
151protocol".
152
153=head2 ENCODING
154
155When the encoder encounters an object that it cannot otherwise encode (for
156example, L<CBOR::XS> can encode a few special types itself, and will first
157attempt to use the special C<TO_CBOR> serialisation protocol), it will
158look up the C<FREEZE> method on the object.
159
160If it exists, it will call it with two arguments: the object to
161serialise, and a constant string that indicates the name of the
162serialisationformat. For example L<CBOR::XS> uses C<CBOR>, and L<JSON> and
163L<JSON::XS> (or any other JSON serialiser), would use C<JSON> as second
164argument.
165
166The C<FREEZE> method can then return zero or more values to identify the
167object instance. The serialiser is then supposed to encode the class name
168and all of these return values (which must be encodable in the format)
169using the relevant form for perl objects. In CBOR for example, there is a
170registered tag number for encoded perl objects.
171
172=head2 DECODING
173
174When the decoder then encounters such an encoded perl object, it should
175look up the C<THAW> method on the stored classname, and invoke it with the
176classname, the constant string to identify the format, and all the return
177values returned by C<FREEZE>.
178
179=head2 EXAMPLES
180
181See the C<OBJECT SERIALISATION> section in the L<CBOR::XS> manpage for
182more details, an example implementation, and code examples.
183
184Here is an example C<FREEZE>/C<THAW> method pair:
185
186 sub My::Object::FREEZE {
187 my ($self, $serialiser) = @_;
188
189 ($self->{type}, $self->{id}, $self->{variant})
190 }
191
192 sub My::Object::THAW {
193 my ($class, $serialiser, $type, $id, $variant) = @_;
194
195 $class-<new (type => $type, id => $id, variant => $variant)
196 }
197
144=head1 BUGS 198=head1 BUGS
145 199
146The use of L<overload> makes this module much heavier than it should be 200The use of L<overload> makes this module much heavier than it should be
147(on my system, this module: 4kB RSS, overload: 260kB RSS). 201(on my system, this module: 4kB RSS, overload: 260kB RSS).
148 202

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines