… | |
… | |
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.82; |
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; |
… | |
… | |
704 | =back |
704 | =back |
705 | |
705 | |
706 | =head2 TYPE CASTS |
706 | =head2 TYPE CASTS |
707 | |
707 | |
708 | B<EXPERIMENTAL>: As an experimental extension, C<CBOR::XS> allows you to |
708 | B<EXPERIMENTAL>: As an experimental extension, C<CBOR::XS> allows you to |
709 | force specific cbor types to be used when encoding. That allows you to |
709 | force specific CBOR types to be used when encoding. That allows you to |
710 | encode types not normally accessible (e.g. half floats) as well as force |
710 | encode types not normally accessible (e.g. half floats) as well as force |
711 | string types even when C<text_strings> is in effect. |
711 | string types even when C<text_strings> is in effect. |
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 | |
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 | |
732 | Forces the value to be encoded as a (binary) string value. |
733 | Forces the value to be encoded as a (binary) string value. |
733 | |
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 | |
734 | =item CBOR::XS::as_bool $value |
740 | =item CBOR::XS::as_bool $value |
735 | |
741 | |
736 | Converts a Perl boolean (which can be any kind of scalar) into a CBOR |
742 | Converts a Perl boolean (which can be any kind of scalar) into a CBOR |
737 | boolean. Strictly the same, but shorter to write, than: |
743 | boolean. 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 | |
751 | Forces double-float (IEEE 754 binary64) encoding of the given value. |
757 | Forces 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 | |
755 | 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 be encoded |
756 | 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. |
757 | |
763 | |
758 | 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 |
759 | the callers responsibility to correctly encode values. |
765 | the callers responsibility to correctly encode values. |
760 | |
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 pairs 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 | Note that only the reference to the array is copied, the array itself is |
|
|
778 | not. Modifications done to the array before calling an encoding function |
|
|
779 | will be reflected in the encoded output. |
|
|
780 | |
|
|
781 | Example: encode a CBOR map with a string and an integer as keys. |
|
|
782 | |
|
|
783 | encode_cbor CBOR::XS::as_map [string => "value", 5 => "value"] |
|
|
784 | |
761 | =back |
785 | =back |
762 | |
|
|
763 | Example: encode a perl string as binary even though C<text_strings> is in |
|
|
764 | effect. |
|
|
765 | |
|
|
766 | CBOR::XS->new->text_strings->encode ([4, "text", CBOR::XS::bytes "bytevalue"]); |
|
|
767 | |
786 | |
768 | =cut |
787 | =cut |
769 | |
788 | |
770 | sub CBOR::XS::as_cbor ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: } |
789 | sub CBOR::XS::as_cbor ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: } |
771 | sub CBOR::XS::as_int ($) { bless [$_[0], 1, undef], CBOR::XS::Tagged:: } |
790 | sub CBOR::XS::as_int ($) { bless [$_[0], 1, undef], CBOR::XS::Tagged:: } |
… | |
… | |
774 | sub CBOR::XS::as_float16 ($) { bless [$_[0], 4, undef], CBOR::XS::Tagged:: } |
793 | sub CBOR::XS::as_float16 ($) { bless [$_[0], 4, undef], CBOR::XS::Tagged:: } |
775 | sub CBOR::XS::as_float32 ($) { bless [$_[0], 5, undef], CBOR::XS::Tagged:: } |
794 | sub CBOR::XS::as_float32 ($) { bless [$_[0], 5, undef], CBOR::XS::Tagged:: } |
776 | sub CBOR::XS::as_float64 ($) { bless [$_[0], 6, undef], CBOR::XS::Tagged:: } |
795 | sub CBOR::XS::as_float64 ($) { bless [$_[0], 6, undef], CBOR::XS::Tagged:: } |
777 | |
796 | |
778 | sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false } |
797 | sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false } |
|
|
798 | |
|
|
799 | sub CBOR::XS::as_map ($) { |
|
|
800 | ARRAY:: eq ref $_[0] |
|
|
801 | and $#{ $_[0] } & 1 |
|
|
802 | or do { require Carp; Carp::croak ("CBOR::XS::as_map only acepts array references with an even number of elements, caught") }; |
|
|
803 | |
|
|
804 | bless [$_[0], 7, undef], CBOR::XS::Tagged:: |
|
|
805 | } |
779 | |
806 | |
780 | =head2 OBJECT SERIALISATION |
807 | =head2 OBJECT SERIALISATION |
781 | |
808 | |
782 | This module implements both a CBOR-specific and the generic |
809 | This module implements both a CBOR-specific and the generic |
783 | L<Types::Serialier> object serialisation protocol. The following |
810 | L<Types::Serialier> object serialisation protocol. The following |