… | |
… | |
17 | |
17 | |
18 | package Types::Serialiser; |
18 | package Types::Serialiser; |
19 | |
19 | |
20 | use common::sense; # required to suppress annoying warnings |
20 | use common::sense; # required to suppress annoying warnings |
21 | |
21 | |
22 | our $VERSION = 0.03; |
22 | our $VERSION = '1.01'; |
23 | |
23 | |
24 | =head1 SIMPLE SCALAR CONSTANTS |
24 | =head1 SIMPLE SCALAR CONSTANTS |
25 | |
25 | |
26 | Simple scalar constants are values that are overloaded to act like simple |
26 | Simple scalar constants are values that are overloaded to act like simple |
27 | Perl values, but have (class) type to differentiate them from normal Perl |
27 | Perl values, but have (class) type to differentiate them from normal Perl |
28 | scalars. This is necessary because these have different representations in |
28 | scalars. This is necessary because these have different representations in |
29 | the serialisation formats. |
29 | the serialisation formats. |
30 | |
30 | |
|
|
31 | In the following, functions with zero or one arguments have a prototype of |
|
|
32 | C<()> and C<($)>, respectively, so act as constants and unary operators. |
|
|
33 | |
31 | =head2 BOOLEANS (Types::Serialiser::Boolean class) |
34 | =head2 BOOLEANS (Types::Serialiser::Boolean class) |
32 | |
35 | |
33 | This type has only two instances, true and false. A natural representation |
36 | This type has only two instances, true and false. A natural representation |
34 | for these in Perl is C<1> and C<0>, but serialisation formats need to be |
37 | for these in Perl is C<1> and C<0>, but serialisation formats need to be |
35 | able to differentiate between them and mere numbers. |
38 | able to differentiate between them and mere numbers. |
… | |
… | |
51 | the number C<0>. It is up to you whether you use the variable form |
54 | the number C<0>. It is up to you whether you use the variable form |
52 | (C<$Types::Serialiser::false>) or the constant form (C<Types::Serialiser::false>). |
55 | (C<$Types::Serialiser::false>) or the constant form (C<Types::Serialiser::false>). |
53 | |
56 | |
54 | The constant is represented as a reference to a scalar containing C<0> - |
57 | The constant is represented as a reference to a scalar containing C<0> - |
55 | implementations are allowed to directly test for this. |
58 | implementations are allowed to directly test for this. |
|
|
59 | |
|
|
60 | =item Types::Serialiser::as_bool $value |
|
|
61 | |
|
|
62 | Converts a Perl scalar into a boolean, which is useful syntactic |
|
|
63 | sugar. Strictly equivalent to: |
|
|
64 | |
|
|
65 | $value ? $Types::Serialiser::true : $Types::Serialiser::false |
56 | |
66 | |
57 | =item $is_bool = Types::Serialiser::is_bool $value |
67 | =item $is_bool = Types::Serialiser::is_bool $value |
58 | |
68 | |
59 | Returns true iff the C<$value> is either C<$Types::Serialiser::true> or |
69 | Returns true iff the C<$value> is either C<$Types::Serialiser::true> or |
60 | C<$Types::Serialiser::false>. |
70 | C<$Types::Serialiser::false>. |
… | |
… | |
127 | |
137 | |
128 | sub true () { $true } |
138 | sub true () { $true } |
129 | sub false () { $false } |
139 | sub false () { $false } |
130 | sub error () { $error } |
140 | sub error () { $error } |
131 | |
141 | |
|
|
142 | sub as_bool($) { $_[0] ? $true : $false } |
|
|
143 | |
132 | sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
144 | sub is_bool ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
133 | sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
145 | sub is_true ($) { $_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
134 | sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
146 | sub is_false ($) { !$_[0] && UNIVERSAL::isa $_[0], Types::Serialiser::Boolean:: } |
135 | sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: } |
147 | sub is_error ($) { UNIVERSAL::isa $_[0], Types::Serialiser::Error:: } |
136 | |
148 | |
… | |
… | |
177 | When the encoder encounters an object that it cannot otherwise encode (for |
189 | When the encoder encounters an object that it cannot otherwise encode (for |
178 | example, L<CBOR::XS> can encode a few special types itself, and will first |
190 | example, L<CBOR::XS> can encode a few special types itself, and will first |
179 | attempt to use the special C<TO_CBOR> serialisation protocol), it will |
191 | attempt to use the special C<TO_CBOR> serialisation protocol), it will |
180 | look up the C<FREEZE> method on the object. |
192 | look up the C<FREEZE> method on the object. |
181 | |
193 | |
|
|
194 | Note that the C<FREEZE> method will normally be called I<during> encoding, |
|
|
195 | and I<MUST NOT> change the data structure that is being encoded in any |
|
|
196 | way, or it might cause memory corruption or worse. |
|
|
197 | |
182 | If it exists, it will call it with two arguments: the object to serialise, |
198 | If it exists, it will call it with two arguments: the object to serialise, |
183 | and a constant string that indicates the name of the data model or data |
199 | and a constant string that indicates the name of the data model. For |
184 | format. For example L<CBOR::XS> uses C<CBOR>, and L<JSON> and L<JSON::XS> |
200 | example L<CBOR::XS> uses C<CBOR>, and the L<JSON> and L<JSON::XS> modules |
185 | (or any other JSON serialiser), would use C<JSON> as second argument. |
201 | (or any other JSON serialiser), would use C<JSON> as second argument. |
186 | |
202 | |
187 | The C<FREEZE> method can then return zero or more values to identify the |
203 | The C<FREEZE> method can then return zero or more values to identify the |
188 | object instance. The serialiser is then supposed to encode the class name |
204 | object instance. The serialiser is then supposed to encode the class name |
189 | and all of these return values (which must be encodable in the format) |
205 | and all of these return values (which must be encodable in the format) |
190 | using the relevant form for perl objects. In CBOR for example, there is a |
206 | using the relevant form for Perl objects. In CBOR for example, there is a |
191 | registered tag number for encoded perl objects. |
207 | registered tag number for encoded perl objects. |
192 | |
208 | |
193 | The values that C<FREEZE> returns must be serialisable with the serialiser |
209 | The values that C<FREEZE> returns must be serialisable with the serialiser |
194 | that calls it. Therefore, it is recommended to use simple types such as |
210 | that calls it. Therefore, it is recommended to use simple types such as |
195 | strings and numbers, and maybe array references and hashes (basically, the |
211 | strings and numbers, and maybe array references and hashes (basically, the |
196 | JSON data model). You can always use a more complex format for a specific |
212 | JSON data model). You can always use a more complex format for a specific |
197 | data model by checking the second argument. |
213 | data model by checking the second argument, the data model. |
|
|
214 | |
|
|
215 | The "data model" is not the same as the "data format" - the data model |
|
|
216 | indicates what types and kinds of return values can be returned from |
|
|
217 | C<FREEZE>. For example, in C<CBOR> it is permissible to return tagged CBOR |
|
|
218 | values, while JSON does not support these at all, so C<JSON> would be a |
|
|
219 | valid (but too limited) data model name for C<CBOR::XS>. similarly, a |
|
|
220 | serialising format that supports more or less the same data model as JSON |
|
|
221 | could use C<JSON> as data model without losing anything. |
198 | |
222 | |
199 | =head2 DECODING |
223 | =head2 DECODING |
200 | |
224 | |
201 | When the decoder then encounters such an encoded perl object, it should |
225 | When the decoder then encounters such an encoded perl object, it should |
202 | look up the C<THAW> method on the stored classname, and invoke it with the |
226 | look up the C<THAW> method on the stored classname, and invoke it with the |