--- CBOR-XS/XS.pm 2016/02/08 04:26:01 1.47 +++ CBOR-XS/XS.pm 2016/04/25 18:20:22 1.53 @@ -66,7 +66,7 @@ use common::sense; -our $VERSION = 1.4; +our $VERSION = 1.41; our @ISA = qw(Exporter); our @EXPORT = qw(encode_cbor decode_cbor); @@ -249,6 +249,50 @@ This option does not affect C in any way - string references will always be decoded properly if present. +=item $cbor = $cbor->text_keys ([$enable]) + +=item $enabled = $cbor->get_text_keys + +If C<$enabled> is true (or missing), then C will encode all +perl hash keys as CBOR text strings/UTF-8 string, upgrading them as needed. + +If C<$enable> is false (the default), then C will encode hash keys +normally - upgraded perl strings (strings internally encoded as UTF-8) as +CBOR text strings, and downgraded perl strings as CBOR byte strings. + +This option does not affect C in any way. + +This option is useful for interoperability with CBOR decoders that don't +treat byte strings as a form of text. It is especially useful as Perl +gives very little control over hash keys. + +Enabling this option can be slow, as all downgraded hash keys that are +encoded need to be scanned and converted to UTF-8. + +=item $cbor = $cbor->text_strings ([$enable]) + +=item $enabled = $cbor->get_text_strings + +This option works similar to C, above, but works on all strings +(including hash keys), so C has no further effect after +enabling C. + +If C<$enabled> is true (or missing), then C will encode all perl +strings as CBOR text strings/UTF-8 strings, upgrading them as needed. + +If C<$enable> is false (the default), then C will encode strings +normally (but see C) - upgraded perl strings (strings +internally encoded as UTF-8) as CBOR text strings, and downgraded perl +strings as CBOR byte strings. + +This option does not affect C in any way. + +This option has similar advantages and disadvantages as C. In +addition, this option effectively removes the ability to encode byte +strings, which might break some C and C methods that rely +on this, such as bignum encoding, so this option is mainly useful for very +simple data. + =item $cbor = $cbor->validate_utf8 ([$enable]) =item $enabled = $cbor->get_validate_utf8 @@ -263,7 +307,7 @@ If C<$enable> is false (the default), then C will blindly accept UTF-8 data, marking them as valid UTF-8 in the resulting data structure -regardless of whether thats true or not. +regardless of whether that's true or not. Perl isn't too happy about corrupted UTF-8 in strings, but should generally not crash or do similarly evil things. Extensions might be not @@ -483,7 +527,7 @@ Perl hash references become CBOR maps. As there is no inherent ordering in hash keys (or CBOR maps), they will usually be encoded in a pseudo-random -order. This order can be different each time a hahs is encoded. +order. This order can be different each time a hash is encoded. Currently, tied hashes will use the indefinite-length format, while normal hashes will use the fixed-length format. @@ -546,15 +590,16 @@ $x .= ""; # another, more awkward way to stringify print $x; # perl does it for you, too, quite often -You can force whether a string ie encoded as byte or text string by using -C and C): +You can force whether a string is encoded as byte or text string by using +C and C (if C is disabled): utf8::upgrade $x; # encode $x as text string utf8::downgrade $x; # encode $x as byte string Perl doesn't define what operations up- and downgrade strings, so if the difference between byte and text is important, you should up- or downgrade -your string as late as possible before encoding. +your string as late as possible before encoding. You can also force the +use of CBOR text strings by using C or C. You can force the type to be a CBOR number by numifying it: @@ -665,7 +710,6 @@ sub URI::THAW { my ($class, $serialiser, $uri) = @_; - $class->new ($uri) } @@ -806,7 +850,7 @@ =head2 ENFORCED TAGS These tags are always handled when decoding, and their handling cannot be -overriden by the user. +overridden by the user. =over 4 @@ -844,7 +888,7 @@ =item 22098 (indirection, L) This tag is automatically generated when a reference are encountered (with -the exception of hash and array refernces). It is converted to a reference +the exception of hash and array references). It is converted to a reference when decoding. =item 55799 (self-describe CBOR, RFC 7049) @@ -857,7 +901,7 @@ =head2 NON-ENFORCED TAGS These tags have default filters provided when decoding. Their handling can -be overriden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by +be overridden by changing the C<%CBOR::XS::FILTER> entry for the tag, or by providing a custom C callback when decoding. When they result in decoding into a specific Perl class, the module @@ -910,48 +954,6 @@ =cut -our %FILTER = ( - # 0 # rfc4287 datetime, utf-8 - # 1 # unix timestamp, any - - 2 => sub { # pos bigint - require Math::BigInt; - Math::BigInt->new ("0x" . unpack "H*", pop) - }, - - 3 => sub { # neg bigint - require Math::BigInt; - -Math::BigInt->new ("0x" . unpack "H*", pop) - }, - - 4 => sub { # decimal fraction, array - require Math::BigFloat; - Math::BigFloat->new ($_[1][1] . "E" . $_[1][0]) - }, - - 5 => sub { # bigfloat, array - require Math::BigFloat; - scalar Math::BigFloat->new ($_[1][1])->blsft ($_[1][0], 2) - }, - - 21 => sub { pop }, # expected conversion to base64url encoding - 22 => sub { pop }, # expected conversion to base64 encoding - 23 => sub { pop }, # expected conversion to base16 encoding - - # 24 # embedded cbor, byte string - - 32 => sub { - require URI; - URI->new (pop) - }, - - # 33 # base64url rfc4648, utf-8 - # 34 # base64 rfc46484, utf-8 - # 35 # regex pcre/ecma262, utf-8 - # 36 # mime message rfc2045, utf-8 -); - - =head1 CBOR and JSON CBOR is supposed to implement a superset of the JSON data model, and is, @@ -1098,7 +1100,7 @@ 5 => sub { # bigfloat, array require Math::BigFloat; - scalar Math::BigFloat->new ($_[1][1])->blsft ($_[1][0], 2) + scalar Math::BigFloat->new ($_[1][1]) * Math::BigFloat->new (2)->bpow ($_[1][0]) }, 21 => sub { pop }, # expected conversion to base64url encoding