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.76 by root, Tue Dec 1 01:49:47 2020 UTC vs.
Revision 1.79 by root, Fri Dec 11 06:03:40 2020 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines