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.6 by root, Tue Oct 29 13:08:35 2013 UTC

15 15
16=cut 16=cut
17 17
18package Types::Serialiser; 18package Types::Serialiser;
19 19
20use common::sense; # required to suppress annoying warnings
21
20our $VERSION = 0.01; 22our $VERSION = 0.03;
21 23
22=head1 SIMPLE SCALAR CONSTANTS 24=head1 SIMPLE SCALAR CONSTANTS
23 25
24Simple scalar constants are values that are overloaded to act like simple 26Simple scalar constants are values that are overloaded to act like simple
25Perl values, but have (class) type to differentiate them from normal Perl 27Perl values, but have (class) type to differentiate them from normal Perl
95 97
96=back 98=back
97 99
98=cut 100=cut
99 101
102BEGIN {
103 # for historical reasons, and to avoid extra dependencies in JSON::PP,
104 # we alias *Types::Serialiser::Boolean with JSON::PP::Boolean.
105 package JSON::PP::Boolean;
106
107 *Types::Serialiser::Boolean:: = *JSON::PP::Boolean::;
108}
109
110{
111 # this must done before blessing to work around bugs
112 # in perl < 5.18 (it seems to be fixed in 5.18).
113 package Types::Serialiser::BooleanBase;
114
115 use overload
116 "0+" => sub { ${$_[0]} },
117 "++" => sub { $_[0] = ${$_[0]} + 1 },
118 "--" => sub { $_[0] = ${$_[0]} - 1 },
119 fallback => 1;
120
121 @Types::Serialiser::Boolean::ISA = Types::Serialiser::BooleanBase::;
122}
123
100our $true = do { bless \(my $dummy = 1), Types::Serialiser::Boolean:: }; 124our $true = do { bless \(my $dummy = 1), Types::Serialiser::Boolean:: };
101our $false = do { bless \(my $dummy = 0), Types::Serialiser::Boolean:: }; 125our $false = do { bless \(my $dummy = 0), Types::Serialiser::Boolean:: };
102our $error = do { bless \(my $dummy ), Types::Serialiser::Error:: }; 126our $error = do { bless \(my $dummy ), Types::Serialiser::Error:: };
103 127
104sub true () { $true } 128sub true () { $true }
107 131
108sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } 132sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: }
109sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } 133sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: }
110sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } 134sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: }
111sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: } 135sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: }
112
113package Types::Serialiser::Boolean;
114
115use overload
116 "0+" => sub { ${$_[0]} },
117 "++" => sub { $_[0] = ${$_[0]} + 1 },
118 "--" => sub { $_[0] = ${$_[0]} - 1 },
119 fallback => 1;
120 136
121package Types::Serialiser::Error; 137package Types::Serialiser::Error;
122 138
123sub error { 139sub error {
124 require Carp; 140 require Carp;
139see if it's C<1> (true), C<0> (false) or C<undef> (error). 155see if it's C<1> (true), C<0> (false) or C<undef> (error).
140 156
141While it is possible to use an isa test, directly comparing stash pointers 157While it is possible to use an isa test, directly comparing stash pointers
142is faster and guaranteed to work. 158is faster and guaranteed to work.
143 159
160For historical reasons, the C<Types::Serialiser::Boolean> stash is
161just an alias for C<JSON::PP::Boolean>. When printed, the classname
162withh usually be C<JSON::PP::Boolean>, but isa tests and stash pointer
163comparison will normally work correctly (i.e. Types::Serialiser::true ISA
164JSON::PP::Boolean, but also ISA Types::Serialiser::Boolean).
165
166=head1 A GENERIC OBJECT SERIALIATION PROTOCOL
167
168This section explains the object serialisation protocol used by
169L<CBOR::XS>. It is meant to be generic enough to support any kind of
170generic object serialiser.
171
172This protocol is called "the Types::Serialiser object serialisation
173protocol".
174
175=head2 ENCODING
176
177When the encoder encounters an object that it cannot otherwise encode (for
178example, L<CBOR::XS> can encode a few special types itself, and will first
179attempt to use the special C<TO_CBOR> serialisation protocol), it will
180look up the C<FREEZE> method on the object.
181
182If it exists, it will call it with two arguments: the object to
183serialise, and a constant string that indicates the name of the
184serialisationformat. For example L<CBOR::XS> uses C<CBOR>, and L<JSON> and
185L<JSON::XS> (or any other JSON serialiser), would use C<JSON> as second
186argument.
187
188The C<FREEZE> method can then return zero or more values to identify the
189object instance. The serialiser is then supposed to encode the class name
190and all of these return values (which must be encodable in the format)
191using the relevant form for perl objects. In CBOR for example, there is a
192registered tag number for encoded perl objects.
193
194The values that C<FREEZE> returns must be serialisable with the serialiser
195that calls it. Therefore, it is recommended to use simple types such as
196strings and numbers, and maybe array references and hashes (basically, the
197JSON data model). You can always use a more complex format for a specific
198serialiser by checking the second argument.
199
200=head2 DECODING
201
202When the decoder then encounters such an encoded perl object, it should
203look up the C<THAW> method on the stored classname, and invoke it with the
204classname, the constant string to identify the format, and all the return
205values returned by C<FREEZE>.
206
207=head2 EXAMPLES
208
209See the C<OBJECT SERIALISATION> section in the L<CBOR::XS> manpage for
210more details, an example implementation, and code examples.
211
212Here is an example C<FREEZE>/C<THAW> method pair:
213
214 sub My::Object::FREEZE {
215 my ($self, $serialiser) = @_;
216
217 ($self->{type}, $self->{id}, $self->{variant})
218 }
219
220 sub My::Object::THAW {
221 my ($class, $serialiser, $type, $id, $variant) = @_;
222
223 $class-<new (type => $type, id => $id, variant => $variant)
224 }
225
144=head1 BUGS 226=head1 BUGS
145 227
146The use of L<overload> makes this module much heavier than it should be 228The use of L<overload> makes this module much heavier than it should be
147(on my system, this module: 4kB RSS, overload: 260kB RSS). 229(on my system, this module: 4kB RSS, overload: 260kB RSS).
148 230

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines