… | |
… | |
12 | $perl_value = decode_cbor $binary_cbor_data; |
12 | $perl_value = decode_cbor $binary_cbor_data; |
13 | |
13 | |
14 | # OO-interface |
14 | # OO-interface |
15 | |
15 | |
16 | $coder = CBOR::XS->new; |
16 | $coder = CBOR::XS->new; |
17 | #TODO |
17 | $binary_cbor_data = $coder->encode ($perl_value); |
|
|
18 | $perl_value = $coder->decode ($binary_cbor_data); |
|
|
19 | |
|
|
20 | # prefix decoding |
|
|
21 | |
|
|
22 | my $many_cbor_strings = ...; |
|
|
23 | while (length $many_cbor_strings) { |
|
|
24 | my ($data, $length) = $cbor->decode_prefix ($many_cbor_strings); |
|
|
25 | # data was decoded |
|
|
26 | substr $many_cbor_strings, 0, $length, ""; # remove decoded cbor string |
|
|
27 | } |
18 | |
28 | |
19 | =head1 DESCRIPTION |
29 | =head1 DESCRIPTION |
20 | |
30 | |
21 | WARNING! THIS IS A PRE-ALPHA RELEASE! IT WILL CRASH, CORRUPT YOUR DATA AND |
31 | WARNING! THIS IS A PRE-ALPHA RELEASE! IT WILL CRASH, CORRUPT YOUR DATA |
22 | EAT YOUR CHILDREN! |
32 | AND EAT YOUR CHILDREN! (Actually, apart from being untested and a bit |
|
|
33 | feature-limited, it might already be useful). |
23 | |
34 | |
24 | This module converts Perl data structures to CBOR and vice versa. Its |
35 | This module converts Perl data structures to the Concise Binary Object |
|
|
36 | Representation (CBOR) and vice versa. CBOR is a fast binary serialisation |
|
|
37 | format that aims to use a superset of the JSON data model, i.e. when you |
|
|
38 | can represent something in JSON, you should be able to represent it in |
|
|
39 | CBOR. |
|
|
40 | |
|
|
41 | This makes it a faster and more compact binary alternative to JSON, with |
|
|
42 | the added ability of supporting serialising of perl objects. |
|
|
43 | |
25 | primary goal is to be I<correct> and its secondary goal is to be |
44 | The primary goal of this module is to be I<correct> and the secondary goal |
26 | I<fast>. To reach the latter goal it was written in C. |
45 | is to be I<fast>. To reach the latter goal it was written in C. |
27 | |
46 | |
28 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
47 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
29 | vice versa. |
48 | vice versa. |
30 | |
49 | |
31 | =cut |
50 | =cut |
32 | |
51 | |
33 | package CBOR::XS; |
52 | package CBOR::XS; |
34 | |
53 | |
35 | use common::sense; |
54 | use common::sense; |
36 | |
55 | |
37 | our $VERSION = 0.02; |
56 | our $VERSION = 0.03; |
38 | our @ISA = qw(Exporter); |
57 | our @ISA = qw(Exporter); |
39 | |
58 | |
40 | our @EXPORT = qw(encode_cbor decode_cbor); |
59 | our @EXPORT = qw(encode_cbor decode_cbor); |
41 | |
60 | |
42 | use Exporter; |
61 | use Exporter; |
43 | use XSLoader; |
62 | use XSLoader; |
|
|
63 | |
|
|
64 | use Types::Serialiser; |
44 | |
65 | |
45 | our $MAGIC = "\xd9\xd9\xf7"; |
66 | our $MAGIC = "\xd9\xd9\xf7"; |
46 | |
67 | |
47 | =head1 FUNCTIONAL INTERFACE |
68 | =head1 FUNCTIONAL INTERFACE |
48 | |
69 | |
… | |
… | |
186 | |
207 | |
187 | CBOR arrays and CBOR maps will be converted into references to a Perl |
208 | CBOR arrays and CBOR maps will be converted into references to a Perl |
188 | array or hash, respectively. The keys of the map will be stringified |
209 | array or hash, respectively. The keys of the map will be stringified |
189 | during this process. |
210 | during this process. |
190 | |
211 | |
|
|
212 | =item null |
|
|
213 | |
|
|
214 | CBOR null becomes C<undef> in Perl. |
|
|
215 | |
191 | =item true, false |
216 | =item true, false, undefined |
192 | |
217 | |
193 | These CBOR values become C<CBOR::XS::true> and C<CBOR::XS::false>, |
218 | These CBOR values become C<Types:Serialiser::true>, |
|
|
219 | C<Types:Serialiser::false> and C<Types::Serialiser::error>, |
194 | respectively. They are overloaded to act almost exactly like the numbers |
220 | respectively. They are overloaded to act almost exactly like the numbers |
195 | C<1> and C<0>. You can check whether a scalar is a CBOR boolean by using |
221 | C<1> and C<0> (for true and false) or to throw an exception on access (for |
196 | the C<CBOR::XS::is_bool> function. |
222 | error). See the L<Types::Serialiser> manpage for details. |
197 | |
223 | |
198 | =item null, undefined |
224 | =item CBOR tag 256 (perl object) |
199 | |
225 | |
200 | CBOR null and undefined values becomes C<undef> in Perl (in the future, |
226 | The tag value C<256> (TODO: pending iana registration) will be used to |
201 | Undefined may raise an exception or something else). |
227 | deserialise a Perl object. |
202 | |
228 | |
203 | =item tags |
229 | TODO For this to work, the class must be loaded and must have a |
|
|
230 | C<FROM_CBOR> method. The decoder will then call the C<FROM_CBOR> method |
|
|
231 | with the constructor arguments provided by the C<TO_CBOR> method (see |
|
|
232 | below). |
204 | |
233 | |
|
|
234 | The C<FROM_CBOR> method must return a single value that will then be used |
|
|
235 | as the deserialised value. |
|
|
236 | |
|
|
237 | =item CBOR tag 55799 (magic header) |
|
|
238 | |
|
|
239 | The tag 55799 is ignored (this tag implements the magic header). |
|
|
240 | |
|
|
241 | =item other CBOR tags |
|
|
242 | |
205 | Tagged items consists of a numeric tag and another CBOR value. The tag |
243 | Tagged items consists of a numeric tag and another CBOR value. Tags not |
206 | 55799 is ignored (this tag implements the magic header). |
244 | handled internally are currently converted into a L<CBOR::XS::Tagged> |
207 | |
|
|
208 | All other tags are currently converted into a L<CBOR::XS::Tagged> object, |
|
|
209 | which is simply a blessed array reference consistsing of the numeric tag |
245 | object, which is simply a blessed array reference consisting of the |
210 | value followed by the (decoded) BOR value. |
246 | numeric tag value followed by the (decoded) CBOR value. |
|
|
247 | |
|
|
248 | In the future, support for user-supplied conversions might get added. |
211 | |
249 | |
212 | =item anything else |
250 | =item anything else |
213 | |
251 | |
214 | Anything else (e.g. unsupported simple values) will raise a decoding |
252 | Anything else (e.g. unsupported simple values) will raise a decoding |
215 | error. |
253 | error. |
… | |
… | |
248 | |
286 | |
249 | Objects of this type must be arrays consisting of a single C<[tag, value]> |
287 | Objects of this type must be arrays consisting of a single C<[tag, value]> |
250 | pair. The (numerical) tag will be encoded as a CBOR tag, the value will be |
288 | pair. The (numerical) tag will be encoded as a CBOR tag, the value will be |
251 | encoded as appropriate for the value. |
289 | encoded as appropriate for the value. |
252 | |
290 | |
253 | =item CBOR::XS::true, CBOR::XS::false |
291 | =item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error |
254 | |
292 | |
255 | These special values become CBOR true and CBOR false values, |
293 | These special values become CBOR true, CBOR false and CBOR undefined |
256 | respectively. You can also use C<\1> and C<\0> directly if you want. |
294 | values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly |
|
|
295 | if you want. |
257 | |
296 | |
258 | =item blessed objects |
297 | =item blessed objects |
259 | |
298 | |
260 | Other blessed objects currently need to have a C<TO_CBOR> method. It |
299 | Other blessed objects currently need to have a C<TO_CBOR> method. It |
261 | will be called on every object that is being serialised, and must return |
300 | will be called on every object that is being serialised, and must return |
… | |
… | |
409 | Please refrain from using rt.cpan.org or any other bug reporting |
448 | Please refrain from using rt.cpan.org or any other bug reporting |
410 | service. I put the contact address into my modules for a reason. |
449 | service. I put the contact address into my modules for a reason. |
411 | |
450 | |
412 | =cut |
451 | =cut |
413 | |
452 | |
414 | our $true = do { bless \(my $dummy = 1), "CBOR::XS::Boolean" }; |
|
|
415 | our $false = do { bless \(my $dummy = 0), "CBOR::XS::Boolean" }; |
|
|
416 | |
|
|
417 | sub true() { $true } |
|
|
418 | sub false() { $false } |
|
|
419 | |
|
|
420 | sub is_bool($) { |
|
|
421 | UNIVERSAL::isa $_[0], "CBOR::XS::Boolean" |
|
|
422 | # or UNIVERSAL::isa $_[0], "CBOR::Literal" |
|
|
423 | } |
|
|
424 | |
|
|
425 | XSLoader::load "CBOR::XS", $VERSION; |
453 | XSLoader::load "CBOR::XS", $VERSION; |
426 | |
|
|
427 | package CBOR::XS::Boolean; |
|
|
428 | |
|
|
429 | use overload |
|
|
430 | "0+" => sub { ${$_[0]} }, |
|
|
431 | "++" => sub { $_[0] = ${$_[0]} + 1 }, |
|
|
432 | "--" => sub { $_[0] = ${$_[0]} - 1 }, |
|
|
433 | fallback => 1; |
|
|
434 | |
|
|
435 | 1; |
|
|
436 | |
454 | |
437 | =head1 SEE ALSO |
455 | =head1 SEE ALSO |
438 | |
456 | |
439 | The L<JSON> and L<JSON::XS> modules that do similar, but human-readable, |
457 | The L<JSON> and L<JSON::XS> modules that do similar, but human-readable, |
440 | serialisation. |
458 | serialisation. |
441 | |
459 | |
|
|
460 | The L<Types::Serialiser> module provides the data model for true, false |
|
|
461 | and error values. |
|
|
462 | |
442 | =head1 AUTHOR |
463 | =head1 AUTHOR |
443 | |
464 | |
444 | Marc Lehmann <schmorp@schmorp.de> |
465 | Marc Lehmann <schmorp@schmorp.de> |
445 | http://home.schmorp.de/ |
466 | http://home.schmorp.de/ |
446 | |
467 | |
447 | =cut |
468 | =cut |
448 | |
469 | |
|
|
470 | 1 |
|
|
471 | |