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.71 by root, Sun Nov 29 21:32:01 2020 UTC vs.
Revision 1.78 by root, Tue Dec 8 08:29:44 2020 UTC

64 64
65package CBOR::XS; 65package CBOR::XS;
66 66
67use common::sense; 67use common::sense;
68 68
69our $VERSION = 1.71; 69our $VERSION = 1.83;
70our @ISA = qw(Exporter); 70our @ISA = qw(Exporter);
71 71
72our @EXPORT = qw(encode_cbor decode_cbor); 72our @EXPORT = qw(encode_cbor decode_cbor);
73 73
74use Exporter; 74use Exporter;
716 716
717The following casts are currently available (all of which are unary operators): 717The following casts are currently available (all of which are unary operators):
718 718
719=over 719=over
720 720
721=item CBOR::XS::as_int $value
722
723Forces the value to be encoded as some form of (basic, not bignum) integer
724type.
725
721=item CBOR::XS::as_text $value 726=item CBOR::XS::as_text $value
722 727
723Forces the value to be encoded as (UTF-8) text values. 728Forces the value to be encoded as (UTF-8) text values.
724 729
725=item CBOR::XS::as_bytes $value 730=item CBOR::XS::as_bytes $value
726 731
727Forces the value to be encoded as a (binary) string value. 732Forces the value to be encoded as a (binary) string value.
728 733
734Example: encode a perl string as binary even though C<text_strings> is in
735effect.
736
737 CBOR::XS->new->text_strings->encode ([4, "text", CBOR::XS::bytes "bytevalue"]);
738
739=item CBOR::XS::as_bool $value
740
741Converts a Perl boolean (which can be any kind of scalar) into a CBOR
742boolean. Strictly the same, but shorter to write, than:
743
744 $value ? Types::Serialiser::true : Types::Serialiser::false
745
729=item CBOR::XS::as_float16 $value 746=item CBOR::XS::as_float16 $value
730 747
731Forces half-float (IEEE 754 binary16) encoding of the given value. 748Forces half-float (IEEE 754 binary16) encoding of the given value.
732 749
733=item CBOR::XS::as_float32 $value 750=item CBOR::XS::as_float32 $value
736 753
737=item CBOR::XS::as_float64 $value 754=item CBOR::XS::as_float64 $value
738 755
739Forces double-float (IEEE 754 binary64) encoding of the given value. 756Forces double-float (IEEE 754 binary64) encoding of the given value.
740 757
741=item, CBOR::XS::as_cbor $cbor_text 758=item CBOR::XS::as_cbor $cbor_text
742 759
743Bot a type cast per-se, this type cast forces the argument to eb encoded 760Not a type cast per-se, this type cast forces the argument to eb encoded
744as-is. This can be used to embed pre-encoded CBOR data. 761as-is. This can be used to embed pre-encoded CBOR data.
745 762
746Note that no checking on the validity of the C<$cbor_text> is done - it's 763Note that no checking on the validity of the C<$cbor_text> is done - it's
747the callers responsibility to correctly encode values. 764the callers responsibility to correctly encode values.
748 765
766=item CBOR::XS::as_map [key => value...]
767
768Treat the array reference as key value pairs and output a CBOR map. This
769allows you to generate CBOR maps with arbitrary key types (or, if you
770don't care about semantics, duplicate keys or prairs in a custom order),
771which is otherwise hard to do with Perl.
772
773The single argument must be an array reference with an even number of
774elements.
775
776Example: encode a CBOR map with a string and an integer as keys.
777
778 encode_cbor CBOR::XS::as_map [string => "value", 5 => "value"]
779
749=back 780=back
750 781
751Example: encode a perl string as binary even though C<text_strings> is in
752effect.
753
754 CBOR::XS->new->text_strings->encode ([4, "text", CBOR::XS::bytes "bytevalue"]);
755
756=cut 782=cut
757 783
758sub CBOR::XS::as_cbor ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: } 784sub CBOR::XS::as_cbor ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: }
785sub CBOR::XS::as_int ($) { bless [$_[0], 1, undef], CBOR::XS::Tagged:: }
759sub CBOR::XS::as_bytes ($) { bless [$_[0], 1, undef], CBOR::XS::Tagged:: } 786sub CBOR::XS::as_bytes ($) { bless [$_[0], 2, undef], CBOR::XS::Tagged:: }
760sub CBOR::XS::as_text ($) { bless [$_[0], 2, undef], CBOR::XS::Tagged:: } 787sub CBOR::XS::as_text ($) { bless [$_[0], 3, undef], CBOR::XS::Tagged:: }
761sub CBOR::XS::as_float16 ($) { bless [$_[0], 3, undef], CBOR::XS::Tagged:: } 788sub CBOR::XS::as_float16 ($) { bless [$_[0], 4, undef], CBOR::XS::Tagged:: }
762sub CBOR::XS::as_float32 ($) { bless [$_[0], 4, undef], CBOR::XS::Tagged:: } 789sub CBOR::XS::as_float32 ($) { bless [$_[0], 5, undef], CBOR::XS::Tagged:: }
763sub CBOR::XS::as_float64 ($) { bless [$_[0], 5, undef], CBOR::XS::Tagged:: } 790sub CBOR::XS::as_float64 ($) { bless [$_[0], 6, undef], CBOR::XS::Tagged:: }
791
792sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false }
793
794sub CBOR::XS::as_map ($) {
795 ARRAY:: eq ref $_[0]
796 and $#{ $_[0] } & 1
797 or do { require Carp; Carp::croak ("CBOR::XS::as_map only acepts array references with an even number of elements, caught") };
798
799 bless [$_[0], 7, undef], CBOR::XS::Tagged::
800}
764 801
765=head2 OBJECT SERIALISATION 802=head2 OBJECT SERIALISATION
766 803
767This module implements both a CBOR-specific and the generic 804This module implements both a CBOR-specific and the generic
768L<Types::Serialier> object serialisation protocol. The following 805L<Types::Serialier> object serialisation protocol. The following

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines