… | |
… | |
26 | substr $many_cbor_strings, 0, $length, ""; # remove decoded cbor string |
26 | substr $many_cbor_strings, 0, $length, ""; # remove decoded cbor string |
27 | } |
27 | } |
28 | |
28 | |
29 | =head1 DESCRIPTION |
29 | =head1 DESCRIPTION |
30 | |
30 | |
31 | WARNING! THIS IS A PRE-ALPHA RELEASE! IT WILL CRASH, CORRUPT YOUR DATA |
31 | WARNING! This module is very new, and not very well tested (that's up to |
32 | AND EAT YOUR CHILDREN! (Actually, apart from being untested and a bit |
32 | you to do). Furthermore, details of the implementation might change freely |
33 | feature-limited, it might already be useful). |
33 | before version 1.0. And lastly, the object serialisation protocol depends |
|
|
34 | on a pending IANA assignment, and until that assignment is official, this |
|
|
35 | implementation is not interoperable with other implementations (even |
|
|
36 | future versions of this module) until the assignment is done. |
|
|
37 | |
|
|
38 | You are still invited to try out CBOR, and this module. |
34 | |
39 | |
35 | This module converts Perl data structures to the Concise Binary Object |
40 | This module converts Perl data structures to the Concise Binary Object |
36 | Representation (CBOR) and vice versa. CBOR is a fast binary serialisation |
41 | 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 |
42 | 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 |
43 | can represent something in JSON, you should be able to represent it in |
39 | CBOR. |
44 | CBOR. |
40 | |
45 | |
41 | This makes it a faster and more compact binary alternative to JSON, with |
46 | In short, CBOR is a faster and very compact binary alternative to JSON, |
42 | the added ability of supporting serialising of perl objects. |
47 | with the added ability of supporting serialisation of Perl objects. (JSON |
|
|
48 | often compresses better than CBOR though, so if you plan to compress the |
|
|
49 | data later you might want to compare both formats first). |
|
|
50 | |
|
|
51 | To give you a general idea about speed, with texts in the megabyte range, |
|
|
52 | C<CBOR::XS> usually encodes roughly twice as fast as L<Storable> or |
|
|
53 | L<JSON::XS> and decodes about 15%-30% faster than those. The shorter the |
|
|
54 | data, the worse L<Storable> performs in comparison. |
|
|
55 | |
|
|
56 | As for compactness, C<CBOR::XS> encoded data structures are usually about |
|
|
57 | 20% smaller than the same data encoded as (compact) JSON or L<Storable>. |
43 | |
58 | |
44 | The primary goal of this module is to be I<correct> and the secondary goal |
59 | The primary goal of this module is to be I<correct> and the secondary goal |
45 | is to be I<fast>. To reach the latter goal it was written in C. |
60 | is to be I<fast>. To reach the latter goal it was written in C. |
46 | |
61 | |
47 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
62 | See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and |
… | |
… | |
51 | |
66 | |
52 | package CBOR::XS; |
67 | package CBOR::XS; |
53 | |
68 | |
54 | use common::sense; |
69 | use common::sense; |
55 | |
70 | |
56 | our $VERSION = 0.03; |
71 | our $VERSION = 0.08; |
57 | our @ISA = qw(Exporter); |
72 | our @ISA = qw(Exporter); |
58 | |
73 | |
59 | our @EXPORT = qw(encode_cbor decode_cbor); |
74 | our @EXPORT = qw(encode_cbor decode_cbor); |
60 | |
75 | |
61 | use Exporter; |
76 | use Exporter; |
… | |
… | |
142 | If no argument is given, the limit check will be deactivated (same as when |
157 | If no argument is given, the limit check will be deactivated (same as when |
143 | C<0> is specified). |
158 | C<0> is specified). |
144 | |
159 | |
145 | See SECURITY CONSIDERATIONS, below, for more info on why this is useful. |
160 | See SECURITY CONSIDERATIONS, below, for more info on why this is useful. |
146 | |
161 | |
|
|
162 | =item $cbor = $cbor->allow_unknown ([$enable]) |
|
|
163 | |
|
|
164 | =item $enabled = $cbor->get_allow_unknown |
|
|
165 | |
|
|
166 | If C<$enable> is true (or missing), then C<encode> will I<not> throw an |
|
|
167 | exception when it encounters values it cannot represent in CBOR (for |
|
|
168 | example, filehandles) but instead will encode a CBOR C<error> value. |
|
|
169 | |
|
|
170 | If C<$enable> is false (the default), then C<encode> will throw an |
|
|
171 | exception when it encounters anything it cannot encode as CBOR. |
|
|
172 | |
|
|
173 | This option does not affect C<decode> in any way, and it is recommended to |
|
|
174 | leave it off unless you know your communications partner. |
|
|
175 | |
|
|
176 | =item $cbor = $cbor->allow_sharing ([$enable]) |
|
|
177 | |
|
|
178 | =item $enabled = $cbor->get_allow_sharing |
|
|
179 | |
|
|
180 | If C<$enable> is true (or missing), then C<encode> will not double-encode |
|
|
181 | values that have been referenced before (e.g. when the same object, such |
|
|
182 | as an array, is referenced multiple times), but instead will emit a |
|
|
183 | reference to the earlier value. |
|
|
184 | |
|
|
185 | This means that such values will only be encoded once, and will not result |
|
|
186 | in a deep cloning of the value on decode, in decoders supporting the value |
|
|
187 | sharing extension. |
|
|
188 | |
|
|
189 | Detecting shared values incurs a runtime overhead when values are encoded |
|
|
190 | that have a reference counter large than one, and might unnecessarily |
|
|
191 | increase the encoded size, as potentially shared values are encode as |
|
|
192 | sharable whether or not they are actually shared. |
|
|
193 | |
|
|
194 | At the moment, only targets of references can be shared (e.g. scalars, |
|
|
195 | arrays or hashes pointed to by a reference). Weirder constructs, such as |
|
|
196 | an array with multiple "copies" of the I<same> string, which are hard but |
|
|
197 | not impossible to create in Perl, are not supported (this is the same as |
|
|
198 | for L<Storable>). |
|
|
199 | |
|
|
200 | If C<$enable> is false (the default), then C<encode> will encode |
|
|
201 | exception when it encounters anything it cannot encode as CBOR. |
|
|
202 | |
|
|
203 | This option does not affect C<decode> in any way - shared values and |
|
|
204 | references will always be decoded properly if present. It is recommended |
|
|
205 | to leave it off unless you know your communications partner supports the |
|
|
206 | value sharing extensions to CBOR (http://cbor.schmorp.de/value-sharing). |
|
|
207 | |
147 | =item $cbor_data = $cbor->encode ($perl_scalar) |
208 | =item $cbor_data = $cbor->encode ($perl_scalar) |
148 | |
209 | |
149 | Converts the given Perl data structure (a scalar value) to its CBOR |
210 | Converts the given Perl data structure (a scalar value) to its CBOR |
150 | representation. |
211 | representation. |
151 | |
212 | |
… | |
… | |
221 | C<1> and C<0> (for true and false) or to throw an exception on access (for |
282 | C<1> and C<0> (for true and false) or to throw an exception on access (for |
222 | error). See the L<Types::Serialiser> manpage for details. |
283 | error). See the L<Types::Serialiser> manpage for details. |
223 | |
284 | |
224 | =item CBOR tag 256 (perl object) |
285 | =item CBOR tag 256 (perl object) |
225 | |
286 | |
226 | The tag value C<256> (TODO: pending iana registration) will be used to |
287 | The tag value C<256> (TODO: pending iana registration) will be used |
227 | deserialise a Perl object. |
288 | to deserialise a Perl object serialised with C<FREEZE>. See L<OBJECT |
228 | |
289 | SERIALISATION>, below, for details. |
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). |
|
|
233 | |
|
|
234 | The C<FROM_CBOR> method must return a single value that will then be used |
|
|
235 | as the deserialised value. |
|
|
236 | |
290 | |
237 | =item CBOR tag 55799 (magic header) |
291 | =item CBOR tag 55799 (magic header) |
238 | |
292 | |
239 | The tag 55799 is ignored (this tag implements the magic header). |
293 | The tag 55799 is ignored (this tag implements the magic header). |
240 | |
294 | |
… | |
… | |
283 | C<1>, which get turned into false and true in CBOR. |
337 | C<1>, which get turned into false and true in CBOR. |
284 | |
338 | |
285 | =item CBOR::XS::Tagged objects |
339 | =item CBOR::XS::Tagged objects |
286 | |
340 | |
287 | Objects of this type must be arrays consisting of a single C<[tag, value]> |
341 | Objects of this type must be arrays consisting of a single C<[tag, value]> |
288 | pair. The (numerical) tag will be encoded as a CBOR tag, the value will be |
342 | pair. The (numerical) tag will be encoded as a CBOR tag, the value will |
289 | encoded as appropriate for the value. |
343 | be encoded as appropriate for the value. You cna use C<CBOR::XS::tag> to |
|
|
344 | create such objects. |
290 | |
345 | |
291 | =item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error |
346 | =item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error |
292 | |
347 | |
293 | These special values become CBOR true, CBOR false and CBOR undefined |
348 | These special values become CBOR true, CBOR false and CBOR undefined |
294 | values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly |
349 | values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly |
295 | if you want. |
350 | if you want. |
296 | |
351 | |
297 | =item blessed objects |
352 | =item other blessed objects |
298 | |
353 | |
299 | Other blessed objects currently need to have a C<TO_CBOR> method. It |
354 | Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See |
300 | will be called on every object that is being serialised, and must return |
355 | L<OBJECT SERIALISATION>, below, for details. |
301 | something that can be encoded in CBOR. |
|
|
302 | |
356 | |
303 | =item simple scalars |
357 | =item simple scalars |
304 | |
358 | |
305 | TODO |
359 | TODO |
306 | Simple Perl scalars (any scalar that is not a reference) are the most |
360 | Simple Perl scalars (any scalar that is not a reference) are the most |
… | |
… | |
344 | represent numerical values are supported, but might suffer loss of |
398 | represent numerical values are supported, but might suffer loss of |
345 | precision. |
399 | precision. |
346 | |
400 | |
347 | =back |
401 | =back |
348 | |
402 | |
|
|
403 | =head2 OBJECT SERIALISATION |
349 | |
404 | |
|
|
405 | This module knows two way to serialise a Perl object: The CBOR-specific |
|
|
406 | way, and the generic way. |
|
|
407 | |
|
|
408 | Whenever the encoder encounters a Perl object that it cnanot serialise |
|
|
409 | directly (most of them), it will first look up the C<TO_CBOR> method on |
|
|
410 | it. |
|
|
411 | |
|
|
412 | If it has a C<TO_CBOR> method, it will call it with the object as only |
|
|
413 | argument, and expects exactly one return value, which it will then |
|
|
414 | substitute and encode it in the place of the object. |
|
|
415 | |
|
|
416 | Otherwise, it will look up the C<FREEZE> method. If it exists, it will |
|
|
417 | call it with the object as first argument, and the constant string C<CBOR> |
|
|
418 | as the second argument, to distinguish it from other serialisers. |
|
|
419 | |
|
|
420 | The C<FREEZE> method can return any number of values (i.e. zero or |
|
|
421 | more). These will be encoded as CBOR perl object, together with the |
|
|
422 | classname. |
|
|
423 | |
|
|
424 | If an object supports neither C<TO_CBOR> nor C<FREEZE>, encoding will fail |
|
|
425 | with an error. |
|
|
426 | |
|
|
427 | Objects encoded via C<TO_CBOR> cannot be automatically decoded, but |
|
|
428 | objects encoded via C<FREEZE> can be decoded using the following protocol: |
|
|
429 | |
|
|
430 | When an encoded CBOR perl object is encountered by the decoder, it will |
|
|
431 | look up the C<THAW> method, by using the stored classname, and will fail |
|
|
432 | if the method cannot be found. |
|
|
433 | |
|
|
434 | After the lookup it will call the C<THAW> method with the stored classname |
|
|
435 | as first argument, the constant string C<CBOR> as second argument, and all |
|
|
436 | values returned by C<FREEZE> as remaining arguments. |
|
|
437 | |
|
|
438 | =head4 EXAMPLES |
|
|
439 | |
|
|
440 | Here is an example C<TO_CBOR> method: |
|
|
441 | |
|
|
442 | sub My::Object::TO_CBOR { |
|
|
443 | my ($obj) = @_; |
|
|
444 | |
|
|
445 | ["this is a serialised My::Object object", $obj->{id}] |
|
|
446 | } |
|
|
447 | |
|
|
448 | When a C<My::Object> is encoded to CBOR, it will instead encode a simple |
|
|
449 | array with two members: a string, and the "object id". Decoding this CBOR |
|
|
450 | string will yield a normal perl array reference in place of the object. |
|
|
451 | |
|
|
452 | A more useful and practical example would be a serialisation method for |
|
|
453 | the URI module. CBOR has a custom tag value for URIs, namely 32: |
|
|
454 | |
|
|
455 | sub URI::TO_CBOR { |
|
|
456 | my ($self) = @_; |
|
|
457 | my $uri = "$self"; # stringify uri |
|
|
458 | utf8::upgrade $uri; # make sure it will be encoded as UTF-8 string |
|
|
459 | CBOR::XS::tagged 32, "$_[0]" |
|
|
460 | } |
|
|
461 | |
|
|
462 | This will encode URIs as a UTF-8 string with tag 32, which indicates an |
|
|
463 | URI. |
|
|
464 | |
|
|
465 | Decoding such an URI will not (currently) give you an URI object, but |
|
|
466 | instead a CBOR::XS::Tagged object with tag number 32 and the string - |
|
|
467 | exactly what was returned by C<TO_CBOR>. |
|
|
468 | |
|
|
469 | To serialise an object so it can automatically be deserialised, you need |
|
|
470 | to use C<FREEZE> and C<THAW>. To take the URI module as example, this |
|
|
471 | would be a possible implementation: |
|
|
472 | |
|
|
473 | sub URI::FREEZE { |
|
|
474 | my ($self, $serialiser) = @_; |
|
|
475 | "$self" # encode url string |
|
|
476 | } |
|
|
477 | |
|
|
478 | sub URI::THAW { |
|
|
479 | my ($class, $serialiser, $uri) = @_; |
|
|
480 | |
|
|
481 | $class->new ($uri) |
|
|
482 | } |
|
|
483 | |
|
|
484 | Unlike C<TO_CBOR>, multiple values can be returned by C<FREEZE>. For |
|
|
485 | example, a C<FREEZE> method that returns "type", "id" and "variant" values |
|
|
486 | would cause an invocation of C<THAW> with 5 arguments: |
|
|
487 | |
|
|
488 | sub My::Object::FREEZE { |
|
|
489 | my ($self, $serialiser) = @_; |
|
|
490 | |
|
|
491 | ($self->{type}, $self->{id}, $self->{variant}) |
|
|
492 | } |
|
|
493 | |
|
|
494 | sub My::Object::THAW { |
|
|
495 | my ($class, $serialiser, $type, $id, $variant) = @_; |
|
|
496 | |
|
|
497 | $class-<new (type => $type, id => $id, variant => $variant) |
|
|
498 | } |
|
|
499 | |
|
|
500 | |
350 | =head2 MAGIC HEADER |
501 | =head1 MAGIC HEADER |
351 | |
502 | |
352 | There is no way to distinguish CBOR from other formats |
503 | There is no way to distinguish CBOR from other formats |
353 | programmatically. To make it easier to distinguish CBOR from other |
504 | programmatically. To make it easier to distinguish CBOR from other |
354 | formats, the CBOR specification has a special "magic string" that can be |
505 | formats, the CBOR specification has a special "magic string" that can be |
355 | prepended to any CBOR string without changing it's meaning. |
506 | prepended to any CBOR string without changing its meaning. |
356 | |
507 | |
357 | This string is available as C<$CBOR::XS::MAGIC>. This module does not |
508 | This string is available as C<$CBOR::XS::MAGIC>. This module does not |
358 | prepend this string tot he CBOR data it generates, but it will ignroe it |
509 | prepend this string to the CBOR data it generates, but it will ignore it |
359 | if present, so users can prepend this string as a "file type" indicator as |
510 | if present, so users can prepend this string as a "file type" indicator as |
360 | required. |
511 | required. |
361 | |
512 | |
362 | |
513 | |
|
|
514 | =head1 THE CBOR::XS::Tagged CLASS |
|
|
515 | |
|
|
516 | CBOR has the concept of tagged values - any CBOR value can be tagged with |
|
|
517 | a numeric 64 bit number, which are centrally administered. |
|
|
518 | |
|
|
519 | C<CBOR::XS> handles a few tags internally when en- or decoding. You can |
|
|
520 | also create tags yourself by encoding C<CBOR::XS::Tagged> objects, and the |
|
|
521 | decoder will create C<CBOR::XS::Tagged> objects itself when it hits an |
|
|
522 | unknown tag. |
|
|
523 | |
|
|
524 | These objects are simply blessed array references - the first member of |
|
|
525 | the array being the numerical tag, the second being the value. |
|
|
526 | |
|
|
527 | You can interact with C<CBOR::XS::Tagged> objects in the following ways: |
|
|
528 | |
|
|
529 | =over 4 |
|
|
530 | |
|
|
531 | =item $tagged = CBOR::XS::tag $tag, $value |
|
|
532 | |
|
|
533 | This function(!) creates a new C<CBOR::XS::Tagged> object using the given |
|
|
534 | C<$tag> (0..2**64-1) to tag the given C<$value> (which can be any Perl |
|
|
535 | value that can be encoded in CBOR, including serialisable Perl objects and |
|
|
536 | C<CBOR::XS::Tagged> objects). |
|
|
537 | |
|
|
538 | =item $tagged->[0] |
|
|
539 | |
|
|
540 | =item $tagged->[0] = $new_tag |
|
|
541 | |
|
|
542 | =item $tag = $tagged->tag |
|
|
543 | |
|
|
544 | =item $new_tag = $tagged->tag ($new_tag) |
|
|
545 | |
|
|
546 | Access/mutate the tag. |
|
|
547 | |
|
|
548 | =item $tagged->[1] |
|
|
549 | |
|
|
550 | =item $tagged->[1] = $new_value |
|
|
551 | |
|
|
552 | =item $value = $tagged->value |
|
|
553 | |
|
|
554 | =item $new_value = $tagged->value ($new_value) |
|
|
555 | |
|
|
556 | Access/mutate the tagged value. |
|
|
557 | |
|
|
558 | =back |
|
|
559 | |
|
|
560 | =cut |
|
|
561 | |
|
|
562 | sub tag($$) { |
|
|
563 | bless [@_], CBOR::XS::Tagged::; |
|
|
564 | } |
|
|
565 | |
|
|
566 | sub CBOR::XS::Tagged::tag { |
|
|
567 | $_[0][0] = $_[1] if $#_; |
|
|
568 | $_[0][0] |
|
|
569 | } |
|
|
570 | |
|
|
571 | sub CBOR::XS::Tagged::value { |
|
|
572 | $_[0][1] = $_[1] if $#_; |
|
|
573 | $_[0][1] |
|
|
574 | } |
|
|
575 | |
|
|
576 | =head2 EXAMPLES |
|
|
577 | |
|
|
578 | Here are some examples of C<CBOR::XS::Tagged> uses to tag objects. |
|
|
579 | |
|
|
580 | You can look up CBOR tag value and emanings in the IANA registry at |
|
|
581 | L<http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>. |
|
|
582 | |
|
|
583 | Prepend a magic header (C<$CBOR::XS::MAGIC>): |
|
|
584 | |
|
|
585 | my $cbor = encode_cbor CBOR::XS::tag 55799, $value; |
|
|
586 | # same as: |
|
|
587 | my $cbor = $CBOR::XS::MAGIC . encode_cbor $value; |
|
|
588 | |
|
|
589 | Serialise some URIs and a regex in an array: |
|
|
590 | |
|
|
591 | my $cbor = encode_cbor [ |
|
|
592 | (CBOR::XS::tag 32, "http://www.nethype.de/"), |
|
|
593 | (CBOR::XS::tag 32, "http://software.schmorp.de/"), |
|
|
594 | (CBOR::XS::tag 35, "^[Pp][Ee][Rr][lL]\$"), |
|
|
595 | ]; |
|
|
596 | |
|
|
597 | Wrap CBOR data in CBOR: |
|
|
598 | |
|
|
599 | my $cbor_cbor = encode_cbor |
|
|
600 | CBOR::XS::tag 24, |
|
|
601 | encode_cbor [1, 2, 3]; |
|
|
602 | |
|
|
603 | =head1 TAG HANDLING AND EXTENSIONS |
|
|
604 | |
|
|
605 | This section describes how this module handles specific tagged values and |
|
|
606 | extensions. If a tag is not mentioned here, then the default handling |
|
|
607 | applies (creating a CBOR::XS::Tagged object on decoding, and only encoding |
|
|
608 | the tag when explicitly requested). |
|
|
609 | |
|
|
610 | Future versions of this module reserve the right to special case |
|
|
611 | additional tags (such as bigfloat or base64url). |
|
|
612 | |
|
|
613 | =over 4 |
|
|
614 | |
|
|
615 | =item <unassigned> (perl-object, L<http://cbor.schmorp.de/perl-object>) |
|
|
616 | |
|
|
617 | These tags are automatically created for serialisable objects using the |
|
|
618 | C<FREEZE/THAW> methods (the L<Types::Serialier> object serialisation |
|
|
619 | protocol). |
|
|
620 | |
|
|
621 | =item <unassigned>, <unassigned> (sharable, sharedref, L <http://cbor.schmorp.de/value-sharing>) |
|
|
622 | |
|
|
623 | These tags are automatically decoded when encountered, resulting in |
|
|
624 | shared values in the decoded object. They are only encoded, however, when |
|
|
625 | C<allow_sharable> is enabled. |
|
|
626 | |
|
|
627 | =item 22098 (indirection, L<http://cbor.schmorp.de/indirection>) |
|
|
628 | |
|
|
629 | This tag is automatically generated when a reference are encountered (with |
|
|
630 | the exception of hash and array refernces). It is converted to a reference |
|
|
631 | when decoding. |
|
|
632 | |
|
|
633 | =item 55799 (self-describe CBOR, RFC 7049) |
|
|
634 | |
|
|
635 | This value is not generated on encoding (unless explicitly requested by |
|
|
636 | the user), and is simply ignored when decoding. |
|
|
637 | |
|
|
638 | =back |
|
|
639 | |
|
|
640 | |
363 | =head2 CBOR and JSON |
641 | =head1 CBOR and JSON |
364 | |
642 | |
365 | CBOR is supposed to implement a superset of the JSON data model, and is, |
643 | CBOR is supposed to implement a superset of the JSON data model, and is, |
366 | with some coercion, able to represent all JSON texts (something that other |
644 | with some coercion, able to represent all JSON texts (something that other |
367 | "binary JSON" formats such as BSON generally do not support). |
645 | "binary JSON" formats such as BSON generally do not support). |
368 | |
646 | |