… | |
… | |
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.0'; |
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 |
… | |
… | |
177 | When the encoder encounters an object that it cannot otherwise encode (for |
177 | 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 |
178 | 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 |
179 | attempt to use the special C<TO_CBOR> serialisation protocol), it will |
180 | look up the C<FREEZE> method on the object. |
180 | look up the C<FREEZE> method on the object. |
181 | |
181 | |
|
|
182 | Note that the C<FREEZE> method will normally be called I<during> encoding, |
|
|
183 | and I<MUST NOT> change the data structure that is being encoded in any |
|
|
184 | way, or it might cause memory corruption or worse. |
|
|
185 | |
182 | If it exists, it will call it with two arguments: the object to serialise, |
186 | 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 |
187 | 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> |
188 | 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. |
189 | (or any other JSON serialiser), would use C<JSON> as second argument. |
186 | |
190 | |
187 | The C<FREEZE> method can then return zero or more values to identify the |
191 | 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 |
192 | 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) |
193 | 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 |
194 | using the relevant form for Perl objects. In CBOR for example, there is a |
191 | registered tag number for encoded perl objects. |
195 | registered tag number for encoded perl objects. |
192 | |
196 | |
193 | The values that C<FREEZE> returns must be serialisable with the serialiser |
197 | 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 |
198 | 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 |
199 | 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 |
200 | JSON data model). You can always use a more complex format for a specific |
197 | data model by checking the second argument. |
201 | data model by checking the second argument, the data model. |
|
|
202 | |
|
|
203 | The "data model" is not the same as the "data format" - the data model |
|
|
204 | indicates what types and kinds of return values can be returned from |
|
|
205 | C<FREEZE>. For example, in C<CBOR> it is permissible to return tagged CBOR |
|
|
206 | values, while JSON does not support these at all, so C<JSON> would be a |
|
|
207 | valid (but too limited) data model name for C<CBOR::XS>. similarly, a |
|
|
208 | serialising format that supports more or less the same data model as JSON |
|
|
209 | could use C<JSON> as data model without losing anything. |
198 | |
210 | |
199 | =head2 DECODING |
211 | =head2 DECODING |
200 | |
212 | |
201 | When the decoder then encounters such an encoded perl object, it should |
213 | 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 |
214 | look up the C<THAW> method on the stored classname, and invoke it with the |