… | |
… | |
66 | |
66 | |
67 | package CBOR::XS; |
67 | package CBOR::XS; |
68 | |
68 | |
69 | use common::sense; |
69 | use common::sense; |
70 | |
70 | |
71 | our $VERSION = 0.07; |
71 | our $VERSION = 0.08; |
72 | our @ISA = qw(Exporter); |
72 | our @ISA = qw(Exporter); |
73 | |
73 | |
74 | our @EXPORT = qw(encode_cbor decode_cbor); |
74 | our @EXPORT = qw(encode_cbor decode_cbor); |
75 | |
75 | |
76 | use Exporter; |
76 | use Exporter; |
… | |
… | |
156 | |
156 | |
157 | 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 |
158 | C<0> is specified). |
158 | C<0> is specified). |
159 | |
159 | |
160 | 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. |
|
|
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). |
161 | |
207 | |
162 | =item $cbor_data = $cbor->encode ($perl_scalar) |
208 | =item $cbor_data = $cbor->encode ($perl_scalar) |
163 | |
209 | |
164 | 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 |
165 | representation. |
211 | representation. |
… | |
… | |
455 | =head1 MAGIC HEADER |
501 | =head1 MAGIC HEADER |
456 | |
502 | |
457 | There is no way to distinguish CBOR from other formats |
503 | There is no way to distinguish CBOR from other formats |
458 | programmatically. To make it easier to distinguish CBOR from other |
504 | programmatically. To make it easier to distinguish CBOR from other |
459 | 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 |
460 | prepended to any CBOR string without changing it's meaning. |
506 | prepended to any CBOR string without changing its meaning. |
461 | |
507 | |
462 | 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 |
463 | 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 |
464 | 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 |
465 | required. |
511 | required. |
466 | |
512 | |
467 | |
513 | |
468 | =head1 THE CBOR::XS::Tagged CLASS |
514 | =head1 THE CBOR::XS::Tagged CLASS |
… | |
… | |
551 | Wrap CBOR data in CBOR: |
597 | Wrap CBOR data in CBOR: |
552 | |
598 | |
553 | my $cbor_cbor = encode_cbor |
599 | my $cbor_cbor = encode_cbor |
554 | CBOR::XS::tag 24, |
600 | CBOR::XS::tag 24, |
555 | encode_cbor [1, 2, 3]; |
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 | |
556 | |
640 | |
557 | =head1 CBOR and JSON |
641 | =head1 CBOR and JSON |
558 | |
642 | |
559 | 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, |
560 | 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 |