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