… | |
… | |
46 | In short, CBOR is a faster and very compact binary alternative to JSON, |
46 | In short, CBOR is a faster and very compact binary alternative to JSON, |
47 | with the added ability of supporting serialisation of Perl objects. (JSON |
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 |
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). |
49 | data later you might want to compare both formats first). |
50 | |
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>. |
|
|
58 | |
51 | 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 |
52 | 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. |
53 | |
61 | |
54 | 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 |
55 | vice versa. |
63 | vice versa. |
… | |
… | |
58 | |
66 | |
59 | package CBOR::XS; |
67 | package CBOR::XS; |
60 | |
68 | |
61 | use common::sense; |
69 | use common::sense; |
62 | |
70 | |
63 | our $VERSION = 0.05; |
71 | our $VERSION = 0.08; |
64 | our @ISA = qw(Exporter); |
72 | our @ISA = qw(Exporter); |
65 | |
73 | |
66 | our @EXPORT = qw(encode_cbor decode_cbor); |
74 | our @EXPORT = qw(encode_cbor decode_cbor); |
67 | |
75 | |
68 | use Exporter; |
76 | use Exporter; |
… | |
… | |
149 | 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 |
150 | C<0> is specified). |
158 | C<0> is specified). |
151 | |
159 | |
152 | 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. |
153 | |
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 | |
154 | =item $cbor_data = $cbor->encode ($perl_scalar) |
208 | =item $cbor_data = $cbor->encode ($perl_scalar) |
155 | |
209 | |
156 | 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 |
157 | representation. |
211 | representation. |
158 | |
212 | |
… | |
… | |
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 |
… | |
… | |
446 | =head1 MAGIC HEADER |
501 | =head1 MAGIC HEADER |
447 | |
502 | |
448 | There is no way to distinguish CBOR from other formats |
503 | There is no way to distinguish CBOR from other formats |
449 | programmatically. To make it easier to distinguish CBOR from other |
504 | programmatically. To make it easier to distinguish CBOR from other |
450 | 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 |
451 | prepended to any CBOR string without changing it's meaning. |
506 | prepended to any CBOR string without changing its meaning. |
452 | |
507 | |
453 | 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 |
454 | 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 |
455 | 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 |
456 | required. |
511 | required. |
457 | |
512 | |
458 | |
513 | |
459 | =head1 THE CBOR::XS::Tagged CLASS |
514 | =head1 THE CBOR::XS::Tagged CLASS |
… | |
… | |
515 | |
570 | |
516 | sub CBOR::XS::Tagged::value { |
571 | sub CBOR::XS::Tagged::value { |
517 | $_[0][1] = $_[1] if $#_; |
572 | $_[0][1] = $_[1] if $#_; |
518 | $_[0][1] |
573 | $_[0][1] |
519 | } |
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 | |
520 | |
640 | |
521 | =head1 CBOR and JSON |
641 | =head1 CBOR and JSON |
522 | |
642 | |
523 | 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, |
524 | 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 |