ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CBOR-XS/XS.pm
(Generate patch)

Comparing CBOR-XS/XS.pm (file contents):
Revision 1.7 by root, Sun Oct 27 22:35:15 2013 UTC vs.
Revision 1.20 by root, Wed Nov 20 02:03:08 2013 UTC

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
31WARNING! THIS IS A PRE-ALPHA RELEASE! IT WILL CRASH, CORRUPT YOUR DATA 31WARNING! This module is very new, and not very well tested (that's up to
32AND EAT YOUR CHILDREN! (Actually, apart from being untested and a bit 32you to do). Furthermore, details of the implementation might change freely
33feature-limited, it might already be useful). 33before version 1.0. And lastly, the object serialisation protocol depends
34on a pending IANA assignment, and until that assignment is official, this
35implementation is not interoperable with other implementations (even
36future versions of this module) until the assignment is done.
37
38You are still invited to try out CBOR, and this module.
34 39
35This module converts Perl data structures to the Concise Binary Object 40This module converts Perl data structures to the Concise Binary Object
36Representation (CBOR) and vice versa. CBOR is a fast binary serialisation 41Representation (CBOR) and vice versa. CBOR is a fast binary serialisation
37format that aims to use a superset of the JSON data model, i.e. when you 42format that aims to use a superset of the JSON data model, i.e. when you
38can represent something in JSON, you should be able to represent it in 43can represent something in JSON, you should be able to represent it in
39CBOR. 44CBOR.
40 45
41This makes it a faster and more compact binary alternative to JSON, with 46In short, CBOR is a faster and very compact binary alternative to JSON,
42the added ability of supporting serialising of perl objects. 47with the added ability of supporting serialisation of Perl objects. (JSON
48often compresses better than CBOR though, so if you plan to compress the
49data later you might want to compare both formats first).
50
51To give you a general idea about speed, with texts in the megabyte range,
52C<CBOR::XS> usually encodes roughly twice as fast as L<Storable> or
53L<JSON::XS> and decodes about 15%-30% faster than those. The shorter the
54data, the worse L<Storable> performs in comparison.
55
56As for compactness, C<CBOR::XS> encoded data structures are usually about
5720% smaller than the same data encoded as (compact) JSON or L<Storable>.
43 58
44The primary goal of this module is to be I<correct> and the secondary goal 59The primary goal of this module is to be I<correct> and the secondary goal
45is to be I<fast>. To reach the latter goal it was written in C. 60is to be I<fast>. To reach the latter goal it was written in C.
46 61
47See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and 62See MAPPING, below, on how CBOR::XS maps perl values to CBOR values and
51 66
52package CBOR::XS; 67package CBOR::XS;
53 68
54use common::sense; 69use common::sense;
55 70
56our $VERSION = 0.03; 71our $VERSION = 0.08;
57our @ISA = qw(Exporter); 72our @ISA = qw(Exporter);
58 73
59our @EXPORT = qw(encode_cbor decode_cbor); 74our @EXPORT = qw(encode_cbor decode_cbor);
60 75
61use Exporter; 76use Exporter;
142If no argument is given, the limit check will be deactivated (same as when 157If no argument is given, the limit check will be deactivated (same as when
143C<0> is specified). 158C<0> is specified).
144 159
145See SECURITY CONSIDERATIONS, below, for more info on why this is useful. 160See 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
166If C<$enable> is true (or missing), then C<encode> will I<not> throw an
167exception when it encounters values it cannot represent in CBOR (for
168example, filehandles) but instead will encode a CBOR C<error> value.
169
170If C<$enable> is false (the default), then C<encode> will throw an
171exception when it encounters anything it cannot encode as CBOR.
172
173This option does not affect C<decode> in any way, and it is recommended to
174leave 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
180If C<$enable> is true (or missing), then C<encode> will not double-encode
181values that have been referenced before (e.g. when the same object, such
182as an array, is referenced multiple times), but instead will emit a
183reference to the earlier value.
184
185This means that such values will only be encoded once, and will not result
186in a deep cloning of the value on decode, in decoders supporting the value
187sharing extension.
188
189Detecting shared values incurs a runtime overhead when values are encoded
190that have a reference counter large than one, and might unnecessarily
191increase the encoded size, as potentially shared values are encode as
192sharable whether or not they are actually shared.
193
194At the moment, only targets of references can be shared (e.g. scalars,
195arrays or hashes pointed to by a reference). Weirder constructs, such as
196an array with multiple "copies" of the I<same> string, which are hard but
197not impossible to create in Perl, are not supported (this is the same as
198for L<Storable>).
199
200If C<$enable> is false (the default), then C<encode> will encode
201exception when it encounters anything it cannot encode as CBOR.
202
203This option does not affect C<decode> in any way - shared values and
204references will always be decoded properly if present. It is recommended
205to leave it off unless you know your communications partner supports the
206value 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
149Converts the given Perl data structure (a scalar value) to its CBOR 210Converts the given Perl data structure (a scalar value) to its CBOR
150representation. 211representation.
151 212
222error). See the L<Types::Serialiser> manpage for details. 283error). 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
226The tag value C<256> (TODO: pending iana registration) will be used 287The tag value C<256> (TODO: pending iana registration) will be used
227to deserialise a Perl object serialised with C<FREEZE>. See "OBJECT 288to deserialise a Perl object serialised with C<FREEZE>. See L<OBJECT
228SERIALISATION", below, for details. 289SERIALISATION>, below, for details.
229 290
230=item CBOR tag 55799 (magic header) 291=item CBOR tag 55799 (magic header)
231 292
232The tag 55799 is ignored (this tag implements the magic header). 293The tag 55799 is ignored (this tag implements the magic header).
233 294
276C<1>, which get turned into false and true in CBOR. 337C<1>, which get turned into false and true in CBOR.
277 338
278=item CBOR::XS::Tagged objects 339=item CBOR::XS::Tagged objects
279 340
280Objects of this type must be arrays consisting of a single C<[tag, value]> 341Objects of this type must be arrays consisting of a single C<[tag, value]>
281pair. The (numerical) tag will be encoded as a CBOR tag, the value will be 342pair. The (numerical) tag will be encoded as a CBOR tag, the value will
282encoded as appropriate for the value. 343be encoded as appropriate for the value. You cna use C<CBOR::XS::tag> to
344create such objects.
283 345
284=item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error 346=item Types::Serialiser::true, Types::Serialiser::false, Types::Serialiser::error
285 347
286These special values become CBOR true, CBOR false and CBOR undefined 348These special values become CBOR true, CBOR false and CBOR undefined
287values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly 349values, respectively. You can also use C<\1>, C<\0> and C<\undef> directly
288if you want. 350if you want.
289 351
290=item other blessed objects 352=item other blessed objects
291 353
292Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See 354Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See
293"OBJECT SERIALISATION", below, for details. 355L<OBJECT SERIALISATION>, below, for details.
294 356
295=item simple scalars 357=item simple scalars
296 358
297TODO 359TODO
298Simple Perl scalars (any scalar that is not a reference) are the most 360Simple Perl scalars (any scalar that is not a reference) are the most
439=head1 MAGIC HEADER 501=head1 MAGIC HEADER
440 502
441There is no way to distinguish CBOR from other formats 503There is no way to distinguish CBOR from other formats
442programmatically. To make it easier to distinguish CBOR from other 504programmatically. To make it easier to distinguish CBOR from other
443formats, the CBOR specification has a special "magic string" that can be 505formats, the CBOR specification has a special "magic string" that can be
444prepended to any CBOR string without changing it's meaning. 506prepended to any CBOR string without changing its meaning.
445 507
446This string is available as C<$CBOR::XS::MAGIC>. This module does not 508This string is available as C<$CBOR::XS::MAGIC>. This module does not
447prepend this string tot he CBOR data it generates, but it will ignroe it 509prepend this string to the CBOR data it generates, but it will ignore it
448if present, so users can prepend this string as a "file type" indicator as 510if present, so users can prepend this string as a "file type" indicator as
449required. 511required.
512
513
514=head1 THE CBOR::XS::Tagged CLASS
515
516CBOR has the concept of tagged values - any CBOR value can be tagged with
517a numeric 64 bit number, which are centrally administered.
518
519C<CBOR::XS> handles a few tags internally when en- or decoding. You can
520also create tags yourself by encoding C<CBOR::XS::Tagged> objects, and the
521decoder will create C<CBOR::XS::Tagged> objects itself when it hits an
522unknown tag.
523
524These objects are simply blessed array references - the first member of
525the array being the numerical tag, the second being the value.
526
527You 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
533This function(!) creates a new C<CBOR::XS::Tagged> object using the given
534C<$tag> (0..2**64-1) to tag the given C<$value> (which can be any Perl
535value that can be encoded in CBOR, including serialisable Perl objects and
536C<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
546Access/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
556Access/mutate the tagged value.
557
558=back
559
560=cut
561
562sub tag($$) {
563 bless [@_], CBOR::XS::Tagged::;
564}
565
566sub CBOR::XS::Tagged::tag {
567 $_[0][0] = $_[1] if $#_;
568 $_[0][0]
569}
570
571sub CBOR::XS::Tagged::value {
572 $_[0][1] = $_[1] if $#_;
573 $_[0][1]
574}
575
576=head2 EXAMPLES
577
578Here are some examples of C<CBOR::XS::Tagged> uses to tag objects.
579
580You can look up CBOR tag value and emanings in the IANA registry at
581L<http://www.iana.org/assignments/cbor-tags/cbor-tags.xhtml>.
582
583Prepend 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
589Serialise 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
597Wrap 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
605This section describes how this module handles specific tagged values and
606extensions. If a tag is not mentioned here, then the default handling
607applies (creating a CBOR::XS::Tagged object on decoding, and only encoding
608the tag when explicitly requested).
609
610Future versions of this module reserve the right to special case
611additional tags (such as bigfloat or base64url).
612
613=over 4
614
615=item <unassigned> (perl-object, L<http://cbor.schmorp.de/perl-object>)
616
617These tags are automatically created for serialisable objects using the
618C<FREEZE/THAW> methods (the L<Types::Serialier> object serialisation
619protocol).
620
621=item <unassigned>, <unassigned> (sharable, sharedref, L <http://cbor.schmorp.de/value-sharing>)
622
623These tags are automatically decoded when encountered, resulting in
624shared values in the decoded object. They are only encoded, however, when
625C<allow_sharable> is enabled.
626
627=item 22098 (indirection, L<http://cbor.schmorp.de/indirection>)
628
629This tag is automatically generated when a reference are encountered (with
630the exception of hash and array refernces). It is converted to a reference
631when decoding.
632
633=item 55799 (self-describe CBOR, RFC 7049)
634
635This value is not generated on encoding (unless explicitly requested by
636the user), and is simply ignored when decoding.
637
638=back
450 639
451 640
452=head1 CBOR and JSON 641=head1 CBOR and JSON
453 642
454CBOR is supposed to implement a superset of the JSON data model, and is, 643CBOR is supposed to implement a superset of the JSON data model, and is,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines