--- CBOR-XS/XS.pm 2020/11/29 21:35:06 1.72 +++ CBOR-XS/XS.pm 2020/12/11 06:03:40 1.79 @@ -66,7 +66,7 @@ use common::sense; -our $VERSION = 1.71; +our $VERSION = 1.83; our @ISA = qw(Exporter); our @EXPORT = qw(encode_cbor decode_cbor); @@ -714,7 +714,8 @@ copy of the value and returns a new value that can be handed over to any CBOR encoder function. -The following casts are currently available (all of which are unary operators): +The following casts are currently available (all of which are unary +operators, that is, have a prototype of C<$>): =over @@ -731,6 +732,18 @@ Forces the value to be encoded as a (binary) string value. +Example: encode a perl string as binary even though C is in +effect. + + CBOR::XS->new->text_strings->encode ([4, "text", CBOR::XS::bytes "bytevalue"]); + +=item CBOR::XS::as_bool $value + +Converts a Perl boolean (which can be any kind of scalar) into a CBOR +boolean. Strictly the same, but shorter to write, than: + + $value ? Types::Serialiser::true : Types::Serialiser::false + =item CBOR::XS::as_float16 $value Forces half-float (IEEE 754 binary16) encoding of the given value. @@ -743,31 +756,50 @@ Forces double-float (IEEE 754 binary64) encoding of the given value. -=item, CBOR::XS::as_cbor $cbor_text +=item CBOR::XS::as_cbor $cbor_text -Bot a type cast per-se, this type cast forces the argument to eb encoded +Not a type cast per-se, this type cast forces the argument to eb encoded as-is. This can be used to embed pre-encoded CBOR data. Note that no checking on the validity of the C<$cbor_text> is done - it's the callers responsibility to correctly encode values. -=back +=item CBOR::XS::as_map [key => value...] -Example: encode a perl string as binary even though C is in -effect. +Treat the array reference as key value pairs and output a CBOR map. This +allows you to generate CBOR maps with arbitrary key types (or, if you +don't care about semantics, duplicate keys or prairs in a custom order), +which is otherwise hard to do with Perl. - CBOR::XS->new->text_strings->encode ([4, "text", CBOR::XS::bytes "bytevalue"]); +The single argument must be an array reference with an even number of +elements. + +Example: encode a CBOR map with a string and an integer as keys. + + encode_cbor CBOR::XS::as_map [string => "value", 5 => "value"] + +=back =cut -sub CBOR::XS::as_int ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: } -sub CBOR::XS::as_cbor ($) { bless [$_[0], 1, undef], CBOR::XS::Tagged:: } +sub CBOR::XS::as_cbor ($) { bless [$_[0], 0, undef], CBOR::XS::Tagged:: } +sub CBOR::XS::as_int ($) { bless [$_[0], 1, undef], CBOR::XS::Tagged:: } sub CBOR::XS::as_bytes ($) { bless [$_[0], 2, undef], CBOR::XS::Tagged:: } sub CBOR::XS::as_text ($) { bless [$_[0], 3, undef], CBOR::XS::Tagged:: } sub CBOR::XS::as_float16 ($) { bless [$_[0], 4, undef], CBOR::XS::Tagged:: } sub CBOR::XS::as_float32 ($) { bless [$_[0], 5, undef], CBOR::XS::Tagged:: } sub CBOR::XS::as_float64 ($) { bless [$_[0], 6, undef], CBOR::XS::Tagged:: } +sub CBOR::XS::as_bool ($) { $_[0] ? $Types::Serialiser::true : $Types::Serialiser::false } + +sub CBOR::XS::as_map ($) { + ARRAY:: eq ref $_[0] + and $#{ $_[0] } & 1 + or do { require Carp; Carp::croak ("CBOR::XS::as_map only acepts array references with an even number of elements, caught") }; + + bless [$_[0], 7, undef], CBOR::XS::Tagged:: +} + =head2 OBJECT SERIALISATION This module implements both a CBOR-specific and the generic