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.21 by root, Wed Nov 20 16:29:02 2013 UTC vs.
Revision 1.23 by root, Fri Nov 22 16:00:30 2013 UTC

118strings. All boolean flags described below are by default I<disabled>. 118strings. All boolean flags described below are by default I<disabled>.
119 119
120The mutators for flags all return the CBOR object again and thus calls can 120The mutators for flags all return the CBOR object again and thus calls can
121be chained: 121be chained:
122 122
123#TODO
124 my $cbor = CBOR::XS->new->encode ({a => [1,2]}); 123 my $cbor = CBOR::XS->new->encode ({a => [1,2]});
125 124
126=item $cbor = $cbor->max_depth ([$maximum_nesting_depth]) 125=item $cbor = $cbor->max_depth ([$maximum_nesting_depth])
127 126
128=item $max_depth = $cbor->get_max_depth 127=item $max_depth = $cbor->get_max_depth
230exception when it encounters anything it cannot encode as CBOR. 229exception when it encounters anything it cannot encode as CBOR.
231 230
232This option does not affect C<decode> in any way - string references will 231This option does not affect C<decode> in any way - string references will
233always be decoded properly if present. 232always be decoded properly if present.
234 233
234=item $cbor = $cbor->filter ([$cb->($tag, $value)])
235
236=item $cb_or_undef = $cbor->get_filter
237
238TODO
239
235=item $cbor_data = $cbor->encode ($perl_scalar) 240=item $cbor_data = $cbor->encode ($perl_scalar)
236 241
237Converts the given Perl data structure (a scalar value) to its CBOR 242Converts the given Perl data structure (a scalar value) to its CBOR
238representation. 243representation.
239 244
307C<Types:Serialiser::false> and C<Types::Serialiser::error>, 312C<Types:Serialiser::false> and C<Types::Serialiser::error>,
308respectively. They are overloaded to act almost exactly like the numbers 313respectively. They are overloaded to act almost exactly like the numbers
309C<1> and C<0> (for true and false) or to throw an exception on access (for 314C<1> and C<0> (for true and false) or to throw an exception on access (for
310error). See the L<Types::Serialiser> manpage for details. 315error). See the L<Types::Serialiser> manpage for details.
311 316
312=item CBOR tag 256 (perl object) 317=item tagged values
313 318
314The tag value C<256> (TODO: pending iana registration) will be used
315to deserialise a Perl object serialised with C<FREEZE>. See L<OBJECT
316SERIALISATION>, below, for details.
317
318=item CBOR tag 55799 (magic header)
319
320The tag 55799 is ignored (this tag implements the magic header).
321
322=item other CBOR tags
323
324Tagged items consists of a numeric tag and another CBOR value. Tags not 319Tagged items consists of a numeric tag and another CBOR value.
325handled internally are currently converted into a L<CBOR::XS::Tagged>
326object, which is simply a blessed array reference consisting of the
327numeric tag value followed by the (decoded) CBOR value.
328 320
329In the future, support for user-supplied conversions might get added. 321See L<TAG HANDLING AND EXTENSIONS> and the description of C<< ->filter >>
322for details.
330 323
331=item anything else 324=item anything else
332 325
333Anything else (e.g. unsupported simple values) will raise a decoding 326Anything else (e.g. unsupported simple values) will raise a decoding
334error. 327error.
377if you want. 370if you want.
378 371
379=item other blessed objects 372=item other blessed objects
380 373
381Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See 374Other blessed objects are serialised via C<TO_CBOR> or C<FREEZE>. See
382L<OBJECT SERIALISATION>, below, for details. 375L<TAG HANDLING AND EXTENSIONS> for specific classes handled by this
376module, and L<OBJECT SERIALISATION> for generic object serialisation.
383 377
384=item simple scalars 378=item simple scalars
385 379
386TODO
387Simple Perl scalars (any scalar that is not a reference) are the most 380Simple Perl scalars (any scalar that is not a reference) are the most
388difficult objects to encode: CBOR::XS will encode undefined scalars as 381difficult objects to encode: CBOR::XS will encode undefined scalars as
389CBOR null values, scalars that have last been used in a string context 382CBOR null values, scalars that have last been used in a string context
390before encoding as CBOR strings, and anything else as number value: 383before encoding as CBOR strings, and anything else as number value:
391 384
627 CBOR::XS::tag 24, 620 CBOR::XS::tag 24,
628 encode_cbor [1, 2, 3]; 621 encode_cbor [1, 2, 3];
629 622
630=head1 TAG HANDLING AND EXTENSIONS 623=head1 TAG HANDLING AND EXTENSIONS
631 624
632This section describes how this module handles specific tagged values and 625This section describes how this module handles specific tagged values
633extensions. If a tag is not mentioned here, then the default handling 626and extensions. If a tag is not mentioned here and no additional filters
627are provided for it, then the default handling applies (creating a
634applies (creating a CBOR::XS::Tagged object on decoding, and only encoding 628CBOR::XS::Tagged object on decoding, and only encoding the tag when
635the tag when explicitly requested). 629explicitly requested).
630
631Tags not handled specifically are currently converted into a
632L<CBOR::XS::Tagged> object, which is simply a blessed array reference
633consisting of the numeric tag value followed by the (decoded) CBOR value.
636 634
637Future versions of this module reserve the right to special case 635Future versions of this module reserve the right to special case
638additional tags (such as bigfloat or base64url). 636additional tags (such as base64url).
637
638=head2 ENFORCED TAGS
639
640These tags are always handled when decoding, and their handling cannot be
641overriden by the user.
639 642
640=over 4 643=over 4
641 644
642=item <unassigned> (perl-object, L<http://cbor.schmorp.de/perl-object>) 645=item <unassigned> (perl-object, L<http://cbor.schmorp.de/perl-object>)
643 646
644These tags are automatically created for serialisable objects using the 647These tags are automatically created (and decoded) for serialisable
645C<FREEZE/THAW> methods (the L<Types::Serialier> object serialisation 648objects using the C<FREEZE/THAW> methods (the L<Types::Serialier> object
646protocol). 649serialisation protocol). See L<OBJECT SERIALISATION> for details.
647 650
648=item <unassigned>, <unassigned> (sharable, sharedref, L <http://cbor.schmorp.de/value-sharing>) 651=item <unassigned>, <unassigned> (sharable, sharedref, L <http://cbor.schmorp.de/value-sharing>)
649 652
650These tags are automatically decoded when encountered, resulting in 653These tags are automatically decoded when encountered, resulting in
651shared values in the decoded object. They are only encoded, however, when 654shared values in the decoded object. They are only encoded, however, when
666 669
667This value is not generated on encoding (unless explicitly requested by 670This value is not generated on encoding (unless explicitly requested by
668the user), and is simply ignored when decoding. 671the user), and is simply ignored when decoding.
669 672
670=back 673=back
674
675=head2 OPTIONAL TAGS
676
677These tags have default filters provided when decoding. Their handling can
678be overriden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by
679providing a custom C<filter> function when decoding.
680
681When they result in decoding into a specific Perl class, the module
682usually provides a corresponding C<TO_CBOR> method as well.
683
684When any of these need to load additional modules that are not part of the
685perl core distribution (e.g. L<URI>), it is (currently) up to the user to
686provide these modules. The decoding usually fails with an exception if the
687required module cannot be loaded.
688
689=over 4
690
691=item 2, 3 (positive/negative bignum)
692
693These tags are decoded into L<Math::BigInt> objects. The corresponding
694C<Math::BigInt::TO_CBOR> method encodes "small" bigints into normal CBOR
695integers, and others into positive/negative CBOR bignums.
696
697=item 4, 5 (decimal fraction/bigfloat)
698
699Both decimal fractions and bigfloats are decoded into L<Math::BigFloat>
700objects. The corresponding C<Math::BigFloat::TO_CBOR> method I<always>
701encodes into a decimal fraction.
702
703CBOR cannot represent bigfloats with I<very> large exponents - conversion
704of such big float objects is undefined.
705
706Also, NaN and infinities are not encoded properly.
707
708=item 21, 22, 23 (expected later JSON conversion)
709
710CBOR::XS is not a CBOR-to-JSON converter, and will simply ignore these
711tags.
712
713=item 32 (URI)
714
715These objects decode into L<URI> objects. The corresponding
716C<URI::TO_CBOR> method again results in a CBOR URI value.
717
718=back
719
720=cut
721
722our %FILTER = (
723 # 0 # rfc4287 datetime, utf-8
724 # 1 # unix timestamp, any
725
726 2 => sub { # pos bigint
727 require Math::BigInt;
728 Math::BigInt->new ("0x" . unpack "H*", pop)
729 },
730
731 3 => sub { # neg bigint
732 require Math::BigInt;
733 -Math::BigInt->new ("0x" . unpack "H*", pop)
734 },
735
736 4 => sub { # decimal fraction, array
737 require Math::BigFloat;
738 Math::BigFloat->new ($_[1][1] . "E" . $_[1][0])
739 },
740
741 5 => sub { # bigfloat, array
742 require Math::BigFloat;
743 scalar Math::BigFloat->new ($_[1][1])->blsft ($_[1][0], 2)
744 },
745
746 21 => sub { pop }, # expected conversion to base64url encoding
747 22 => sub { pop }, # expected conversion to base64 encoding
748 23 => sub { pop }, # expected conversion to base16 encoding
749
750 # 24 # embedded cbor, byte string
751
752 32 => sub {
753 require URI;
754 URI->new (pop)
755 },
756
757 # 33 # base64url rfc4648, utf-8
758 # 34 # base64 rfc46484, utf-8
759 # 35 # regex pcre/ecma262, utf-8
760 # 36 # mime message rfc2045, utf-8
761);
671 762
672 763
673=head1 CBOR and JSON 764=head1 CBOR and JSON
674 765
675CBOR is supposed to implement a superset of the JSON data model, and is, 766CBOR is supposed to implement a superset of the JSON data model, and is,
758Please refrain from using rt.cpan.org or any other bug reporting 849Please refrain from using rt.cpan.org or any other bug reporting
759service. I put the contact address into my modules for a reason. 850service. I put the contact address into my modules for a reason.
760 851
761=cut 852=cut
762 853
854our %FILTER = (
855 # 0 # rfc4287 datetime, utf-8
856 # 1 # unix timestamp, any
857
858 2 => sub { # pos bigint
859 require Math::BigInt;
860 Math::BigInt->new ("0x" . unpack "H*", pop)
861 },
862
863 3 => sub { # neg bigint
864 require Math::BigInt;
865 -Math::BigInt->new ("0x" . unpack "H*", pop)
866 },
867
868 4 => sub { # decimal fraction, array
869 require Math::BigFloat;
870 Math::BigFloat->new ($_[1][1] . "E" . $_[1][0])
871 },
872
873 5 => sub { # bigfloat, array
874 require Math::BigFloat;
875 scalar Math::BigFloat->new ($_[1][1])->blsft ($_[1][0], 2)
876 },
877
878 21 => sub { pop }, # expected conversion to base64url encoding
879 22 => sub { pop }, # expected conversion to base64 encoding
880 23 => sub { pop }, # expected conversion to base16 encoding
881
882 # 24 # embedded cbor, byte string
883
884 32 => sub {
885 require URI;
886 URI->new (pop)
887 },
888
889 # 33 # base64url rfc4648, utf-8
890 # 34 # base64 rfc46484, utf-8
891 # 35 # regex pcre/ecma262, utf-8
892 # 36 # mime message rfc2045, utf-8
893);
894
895sub CBOR::XS::default_filter {
896 &{ $FILTER{$_[0]} or return }
897}
898
899sub URI::TO_CBOR {
900 my $uri = $_[0]->as_string;
901 utf8::upgrade $uri;
902 CBOR::XS::tag 32, $uri
903}
904
905sub Math::BigInt::TO_CBOR {
906 if ($_[0] >= -2147483648 && $_[0] <= 2147483647) {
907 $_[0]->numify
908 } else {
909 my $hex = substr $_[0]->as_hex, 2;
910 $hex = "0$hex" if 1 & length $hex; # sigh
911 CBOR::XS::tag $_[0] >= 0 ? 2 : 3, pack "H*", $hex
912 }
913}
914
915sub Math::BigFloat::TO_CBOR {
916 my ($m, $e) = $_[0]->parts;
917 CBOR::XS::tag 4, [$e->numify, $m]
918}
919
763XSLoader::load "CBOR::XS", $VERSION; 920XSLoader::load "CBOR::XS", $VERSION;
764 921
765=head1 SEE ALSO 922=head1 SEE ALSO
766 923
767The L<JSON> and L<JSON::XS> modules that do similar, but human-readable, 924The L<JSON> and L<JSON::XS> modules that do similar, but human-readable,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines