… | |
… | |
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_sharable ([$enable]) |
|
|
177 | |
|
|
178 | =item $enabled = $cbor->get_allow_sharable |
|
|
179 | |
|
|
180 | If C<$enable> is true (or missing), then C<encode> will not double-encode |
|
|
181 | values that have been seen before (e.g. when the same object, such as an |
|
|
182 | array, is referenced multiple times), but instead will emit a reference to |
|
|
183 | 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, all shared values will be detected, even weird and unusual |
|
|
195 | cases, such as an array with multiple "copies" of the I<same> scalar, |
|
|
196 | which are hard but not impossible to create in Perl (L<Storable> for |
|
|
197 | example doesn't handle these cases). If this turns out ot be a performance |
|
|
198 | issue then future versions might limit the shared value detection to |
|
|
199 | references only. |
|
|
200 | |
|
|
201 | If C<$enable> is false (the default), then C<encode> will encode |
|
|
202 | exception when it encounters anything it cannot encode as CBOR. |
|
|
203 | |
|
|
204 | This option does not affect C<decode> in any way - shared values and |
|
|
205 | references will always be decoded properly if present. It is recommended |
|
|
206 | to leave it off unless you know your communications partner supports the |
|
|
207 | value sharing extensions to CBOR (http://cbor.schmorp.de/value-sharing). |
|
|
208 | |
154 | =item $cbor_data = $cbor->encode ($perl_scalar) |
209 | =item $cbor_data = $cbor->encode ($perl_scalar) |
155 | |
210 | |
156 | Converts the given Perl data structure (a scalar value) to its CBOR |
211 | Converts the given Perl data structure (a scalar value) to its CBOR |
157 | representation. |
212 | representation. |
158 | |
213 | |
… | |
… | |
283 | C<1>, which get turned into false and true in CBOR. |
338 | C<1>, which get turned into false and true in CBOR. |
284 | |
339 | |
285 | =item CBOR::XS::Tagged objects |
340 | =item CBOR::XS::Tagged objects |
286 | |
341 | |
287 | Objects of this type must be arrays consisting of a single C<[tag, value]> |
342 | 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 |
343 | pair. The (numerical) tag will be encoded as a CBOR tag, the value will |
289 | encoded as appropriate for the value. |
344 | be encoded as appropriate for the value. You cna use C<CBOR::XS::tag> to |
|
|
345 | create such objects. |
290 | |
346 | |
291 | =item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error |
347 | =item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error |
292 | |
348 | |
293 | These special values become CBOR true, CBOR false and CBOR undefined |
349 | 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 |
350 | values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly |
… | |
… | |
446 | =head1 MAGIC HEADER |
502 | =head1 MAGIC HEADER |
447 | |
503 | |
448 | There is no way to distinguish CBOR from other formats |
504 | There is no way to distinguish CBOR from other formats |
449 | programmatically. To make it easier to distinguish CBOR from other |
505 | programmatically. To make it easier to distinguish CBOR from other |
450 | formats, the CBOR specification has a special "magic string" that can be |
506 | formats, the CBOR specification has a special "magic string" that can be |
451 | prepended to any CBOR string without changing it's meaning. |
507 | prepended to any CBOR string without changing its meaning. |
452 | |
508 | |
453 | This string is available as C<$CBOR::XS::MAGIC>. This module does not |
509 | 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 |
510 | 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 |
511 | if present, so users can prepend this string as a "file type" indicator as |
456 | required. |
512 | required. |
457 | |
513 | |
458 | |
514 | |
459 | =head1 THE CBOR::XS::Tagged CLASS |
515 | =head1 THE CBOR::XS::Tagged CLASS |
… | |
… | |
515 | |
571 | |
516 | sub CBOR::XS::Tagged::value { |
572 | sub CBOR::XS::Tagged::value { |
517 | $_[0][1] = $_[1] if $#_; |
573 | $_[0][1] = $_[1] if $#_; |
518 | $_[0][1] |
574 | $_[0][1] |
519 | } |
575 | } |
|
|
576 | |
|
|
577 | =head2 EXAMPLES |
|
|
578 | |
|
|
579 | Here are some examples of C<CBOR::XS::Tagged> uses to tag objects. |
|
|
580 | |
|
|
581 | You can look up CBOR tag value and emanings in the IANA registry at |
|
|
582 | L<http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>. |
|
|
583 | |
|
|
584 | Prepend a magic header (C<$CBOR::XS::MAGIC>): |
|
|
585 | |
|
|
586 | my $cbor = encode_cbor CBOR::XS::tag 55799, $value; |
|
|
587 | # same as: |
|
|
588 | my $cbor = $CBOR::XS::MAGIC . encode_cbor $value; |
|
|
589 | |
|
|
590 | Serialise some URIs and a regex in an array: |
|
|
591 | |
|
|
592 | my $cbor = encode_cbor [ |
|
|
593 | (CBOR::XS::tag 32, "http://www.nethype.de/"), |
|
|
594 | (CBOR::XS::tag 32, "http://software.schmorp.de/"), |
|
|
595 | (CBOR::XS::tag 35, "^[Pp][Ee][Rr][lL]\$"), |
|
|
596 | ]; |
|
|
597 | |
|
|
598 | Wrap CBOR data in CBOR: |
|
|
599 | |
|
|
600 | my $cbor_cbor = encode_cbor |
|
|
601 | CBOR::XS::tag 24, |
|
|
602 | encode_cbor [1, 2, 3]; |
|
|
603 | |
|
|
604 | =head1 TAG HANDLING AND EXTENSIONS |
|
|
605 | |
|
|
606 | This section describes how this module handles specific tagged values and |
|
|
607 | extensions. If a tag is not mentioned here, then the default handling |
|
|
608 | applies (creating a CBOR::XS::Tagged object on decoding, and only encoding |
|
|
609 | the tag when explicitly requested). |
|
|
610 | |
|
|
611 | Future versions of this module reserve the right to special case |
|
|
612 | additional tags (such as bigfloat or base64url). |
|
|
613 | |
|
|
614 | =over 4 |
|
|
615 | |
|
|
616 | =item <unassigned> (perl-object, L<http://cbor.schmorp.de/perl-object>) |
|
|
617 | |
|
|
618 | These tags are automatically created for serialisable objects using the |
|
|
619 | C<FREEZE/THAW> methods (the L<Types::Serialier> object serialisation |
|
|
620 | protocol). |
|
|
621 | |
|
|
622 | =item <unassigned>, <unassigned> (sharable, sharedref, L <http://cbor.schmorp.de/value-sharing>) |
|
|
623 | |
|
|
624 | These tags are automatically decoded when encountered, resulting in |
|
|
625 | shared values in the decoded object. They are only encoded, however, when |
|
|
626 | C<allow_sharable> is enabled. |
|
|
627 | |
|
|
628 | =item 22098 (indirection, L<http://cbor.schmorp.de/indirection>) |
|
|
629 | |
|
|
630 | This tag is automatically generated when a reference are encountered (with |
|
|
631 | the exception of hash and array refernces). It is converted to a reference |
|
|
632 | when decoding. |
|
|
633 | |
|
|
634 | =item 55799 (self-describe CBOR, RFC 7049) |
|
|
635 | |
|
|
636 | This value is not generated on encoding (unless explicitly requested by |
|
|
637 | the user), and is simply ignored when decoding. |
|
|
638 | |
|
|
639 | =back |
|
|
640 | |
520 | |
641 | |
521 | =head1 CBOR and JSON |
642 | =head1 CBOR and JSON |
522 | |
643 | |
523 | CBOR is supposed to implement a superset of the JSON data model, and is, |
644 | 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 |
645 | with some coercion, able to represent all JSON texts (something that other |