… | |
… | |
729 | |
729 | |
730 | =item CBOR::XS::as_bytes $value |
730 | =item CBOR::XS::as_bytes $value |
731 | |
731 | |
732 | Forces the value to be encoded as a (binary) string value. |
732 | Forces the value to be encoded as a (binary) string value. |
733 | |
733 | |
|
|
734 | Example: encode a perl string as binary even though C<text_strings> is in |
|
|
735 | effect. |
|
|
736 | |
|
|
737 | CBOR::XS->new->text_strings->encode ([4, "text", CBOR::XS::bytes "bytevalue"]); |
|
|
738 | |
734 | =item CBOR::XS::as_bool $value |
739 | =item CBOR::XS::as_bool $value |
735 | |
740 | |
736 | Converts a Perl boolean (which can be any kind of scalar) into a CBOR |
741 | Converts a Perl boolean (which can be any kind of scalar) into a CBOR |
737 | boolean. Strictly the same, but shorter to write, than: |
742 | boolean. Strictly the same, but shorter to write, than: |
738 | |
743 | |
… | |
… | |
748 | |
753 | |
749 | =item CBOR::XS::as_float64 $value |
754 | =item CBOR::XS::as_float64 $value |
750 | |
755 | |
751 | Forces double-float (IEEE 754 binary64) encoding of the given value. |
756 | Forces double-float (IEEE 754 binary64) encoding of the given value. |
752 | |
757 | |
753 | =item, CBOR::XS::as_cbor $cbor_text |
758 | =item CBOR::XS::as_cbor $cbor_text |
754 | |
759 | |
755 | Bot a type cast per-se, this type cast forces the argument to eb encoded |
760 | Not a type cast per-se, this type cast forces the argument to eb encoded |
756 | as-is. This can be used to embed pre-encoded CBOR data. |
761 | as-is. This can be used to embed pre-encoded CBOR data. |
757 | |
762 | |
758 | Note that no checking on the validity of the C<$cbor_text> is done - it's |
763 | Note that no checking on the validity of the C<$cbor_text> is done - it's |
759 | the callers responsibility to correctly encode values. |
764 | the callers responsibility to correctly encode values. |
760 | |
765 | |
|
|
766 | =item CBOR::XS::as_map [key => value...] |
|
|
767 | |
|
|
768 | Treat the array reference as key value pairs and output a CBOR map. This |
|
|
769 | allows you to generate CBOR maps with arbitrary key types (or, if you |
|
|
770 | don't care about semantics, duplicate keys or prairs in a custom order), |
|
|
771 | which is otherwise hard to do with Perl. |
|
|
772 | |
|
|
773 | The single argument must be an array reference with an even number of |
|
|
774 | elements. |
|
|
775 | |
|
|
776 | Example: 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 | |
761 | =back |
780 | =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 | |
781 | |
768 | =cut |
782 | =cut |
769 | |
783 | |
770 | sub CBOR::XS::as_cbor ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: } |
784 | 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:: } |
785 | 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:: } |
788 | 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:: } |
789 | 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:: } |
790 | sub CBOR::XS::as_float64 ($) { bless [$_[0], 6, undef], CBOR::XS::Tagged:: } |
777 | |
791 | |
778 | sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false } |
792 | sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false } |
|
|
793 | |
|
|
794 | sub 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, found ") }; |
|
|
798 | |
|
|
799 | bless [$_[0], 7, undef], CBOR::XS::Tagged:: |
|
|
800 | } |
779 | |
801 | |
780 | =head2 OBJECT SERIALISATION |
802 | =head2 OBJECT SERIALISATION |
781 | |
803 | |
782 | This module implements both a CBOR-specific and the generic |
804 | This module implements both a CBOR-specific and the generic |
783 | L<Types::Serialier> object serialisation protocol. The following |
805 | L<Types::Serialier> object serialisation protocol. The following |