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.3 by root, Sun Oct 27 22:46:21 2013 UTC vs.
Revision 1.5 by root, Mon Oct 28 21:52:22 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.02;
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 *Types::Serialiser::Boolean:: = *JSON::PP::Boolean::;
107}
108
100our $true = do { bless \(my $dummy = 1), Types::Serialiser::Boolean:: }; 109our $true = do { bless \(my $dummy = 1), Types::Serialiser::Boolean:: };
101our $false = do { bless \(my $dummy = 0), Types::Serialiser::Boolean:: }; 110our $false = do { bless \(my $dummy = 0), Types::Serialiser::Boolean:: };
102our $error = do { bless \(my $dummy ), Types::Serialiser::Error:: }; 111our $error = do { bless \(my $dummy ), Types::Serialiser::Error:: };
103 112
104sub true () { $true } 113sub true () { $true }
108sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } 117sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: }
109sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } 118sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: }
110sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } 119sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: }
111sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: } 120sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: }
112 121
113package Types::Serialiser::Boolean; 122package Types::Serialiser::BooleanBase;
114 123
115use overload 124use overload
116 "0+" => sub { ${$_[0]} }, 125 "0+" => sub { ${$_[0]} },
117 "++" => sub { $_[0] = ${$_[0]} + 1 }, 126 "++" => sub { $_[0] = ${$_[0]} + 1 },
118 "--" => sub { $_[0] = ${$_[0]} - 1 }, 127 "--" => sub { $_[0] = ${$_[0]} - 1 },
119 fallback => 1; 128 fallback => 1;
120 129
130@Types::Serialiser::Boolean::ISA = Types::Serialiser::BooleanBase::;
131
121package Types::Serialiser::Error; 132package Types::Serialiser::Error;
122 133
123sub error { 134sub error {
124 require Carp; 135 require Carp;
125 Carp::croak ("caught attempt to use the Types::Serialiser::error value"); 136 Carp::croak ("caught attempt to use the Types::Serialiser::error value");
139see if it's C<1> (true), C<0> (false) or C<undef> (error). 150see if it's C<1> (true), C<0> (false) or C<undef> (error).
140 151
141While it is possible to use an isa test, directly comparing stash pointers 152While it is possible to use an isa test, directly comparing stash pointers
142is faster and guaranteed to work. 153is faster and guaranteed to work.
143 154
155For historical reasons, the C<Types::Serialiser::Boolean> stash is
156just an alias for C<JSON::PP::Boolean>. When printed, the classname
157withh usually be C<JSON::PP::Boolean>, but isa tests and stash pointer
158comparison will normally work correctly (i.e. Types::Serialiser::true ISA
159JSON::PP::Boolean, but also ISA Types::Serialiser::Boolean).
160
144=head1 A GENERIC OBJECT SERIALIATION PROTOCOL 161=head1 A GENERIC OBJECT SERIALIATION PROTOCOL
145 162
146This section explains the object serialisation protocol used by 163This section explains the object serialisation protocol used by
147L<CBOR::XS>. It is meant to be generic enough to support any kind of 164L<CBOR::XS>. It is meant to be generic enough to support any kind of
148generic object serialiser. 165generic object serialiser.
167object instance. The serialiser is then supposed to encode the class name 184object instance. The serialiser is then supposed to encode the class name
168and all of these return values (which must be encodable in the format) 185and 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 186using the relevant form for perl objects. In CBOR for example, there is a
170registered tag number for encoded perl objects. 187registered tag number for encoded perl objects.
171 188
189The values that C<FREEZE> returns must be serialisable with the serialiser
190that calls it. Therefore, it is recommended to use simple types such as
191strings and numbers, and maybe array references and hashes (basically, the
192JSON data model). You can always use a more complex format for a specific
193serialiser by checking the second argument.
194
172=head2 DECODING 195=head2 DECODING
173 196
174When the decoder then encounters such an encoded perl object, it should 197When 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 198look 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 199classname, the constant string to identify the format, and all the return

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines