| 1 | package CBOR::Free::Decoder; | ||
| 2 | |||
| 3 | =encoding utf8 | ||
| 4 | |||
| 5 | =head1 NAME | ||
| 6 | |||
| 7 | CBOR::Free::Decoder | ||
| 8 | |||
| 9 | =head1 SYNOPSIS | ||
| 10 | |||
| 11 | my $decoder = CBOR::Free::Decoder->new()->set_tag_handlers( | ||
| 12 | 2 => sub { DateTime->from_epoch( epoch => shift() ) }, | ||
| 13 | ); | ||
| 14 | |||
| 15 | # Enable shared/circular references: | ||
| 16 | $decoder->preserve_references(); | ||
| 17 | |||
| 18 | # Reject maps that would overwrite a decoded Perl hash key: | ||
| 19 | $decoder->reject_duplicate_keys(); | ||
| 20 | |||
| 21 | =head1 DESCRIPTION | ||
| 22 | |||
| 23 | This class provides an object-oriented interface to L<CBOR::Free>’s | ||
| 24 | decoder. This interface allows interpretation of tagged values. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | #---------------------------------------------------------------------- | ||
| 29 | |||
| 30 | 7 | ▶ | use parent qw( CBOR::Free::Decoder::Base ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | |||
| 32 | 7 | ▶ | use CBOR::Free (); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | |||
| 34 | #---------------------------------------------------------------------- | ||
| 35 | |||
| 36 | =head1 METHODS | ||
| 37 | |||
| 38 | =head2 $obj = I<CLASS>->new() | ||
| 39 | |||
| 40 | Creates a new CBOR decoder object. | ||
| 41 | |||
| 42 | =cut | ||
| 43 | |||
| 44 | #---------------------------------------------------------------------- | ||
| 45 | |||
| 46 | =head2 $data = I<OBJ>->decode( $CBOR ) | ||
| 47 | |||
| 48 | Same as L<CBOR::Free>’s static function of the same name but applies | ||
| 49 | any tag handlers configured in C<set_tag_handlers()>. | ||
| 50 | |||
| 51 | As in L<CBOR::Free>, any unrecognized tags prompt a warning but are | ||
| 52 | otherwise ignored. | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | #---------------------------------------------------------------------- | ||
| 57 | |||
| 58 | =head2 $enabled_yn = I<OBJ>->preserve_references( [$ENABLE] ) | ||
| 59 | |||
| 60 | Enables/disables recognition of CBOR’s shared references. (If no | ||
| 61 | argument is given, shared references will be enabled.) | ||
| 62 | |||
| 63 | B<HANDLE WITH CARE.> This option can cause CBOR::Free to create circular | ||
| 64 | references, which can cause memory leaks if not handled properly. | ||
| 65 | |||
| 66 | =cut | ||
| 67 | |||
| 68 | #---------------------------------------------------------------------- | ||
| 69 | |||
| 70 | =head2 $enabled_yn = I<OBJ>->naive_utf8( [$ENABLE] ) | ||
| 71 | |||
| 72 | Same interface as C<preserve_references()>, but this option tells I<OBJ> | ||
| 73 | to forgo UTF-8 validation of CBOR text strings when enabled. This speeds up | ||
| 74 | decoding of text strings but may confuse Perl if invalid UTF-8 is given in | ||
| 75 | a CBOR text string. That may or may not break your application. | ||
| 76 | |||
| 77 | This I<should> be safe in contexts—such as IPC—where you control the CBOR | ||
| 78 | serialization and can thus ensure validity of the encoded text. | ||
| 79 | |||
| 80 | If in doubt, leave this off. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | #---------------------------------------------------------------------- | ||
| 85 | |||
| 86 | =head2 $enabled_yn = I<OBJ>->reject_duplicate_keys( [$ENABLE] ) | ||
| 87 | |||
| 88 | Enables or disables rejection of duplicate map keys. It is disabled by | ||
| 89 | default. If enabled, decoding dies with a | ||
| 90 | L<CBOR::Free::X::DuplicateMapKey> exception when two CBOR keys would occupy | ||
| 91 | the same key in the decoded Perl hash. | ||
| 92 | |||
| 93 | Calling this method without C<$ENABLE> enables rejection. Passing a false | ||
| 94 | value disables it. The return value indicates whether rejection is enabled. | ||
| 95 | |||
| 96 | =cut | ||
| 97 | |||
| 98 | #---------------------------------------------------------------------- | ||
| 99 | |||
| 100 | =head2 $obj = I<OBJ>->string_decode_cbor(); | ||
| 101 | |||
| 102 | This causes I<OBJ> to decode strings according to their CBOR type: | ||
| 103 | text strings are UTF8-decoded; binary strings are left as-is. This is | ||
| 104 | the default configuration, à la C<CBOR::Free::decode()>. | ||
| 105 | |||
| 106 | =head2 $obj = I<OBJ>->string_decode_never(); | ||
| 107 | |||
| 108 | This causes I<OBJ> to leave all strings undecoded. This is useful for | ||
| 109 | applications that treat all strings as octet sequences. Note that CBOR | ||
| 110 | text strings will still be validated as UTF-8 unless C<naive_utf8()> is | ||
| 111 | enabled. | ||
| 112 | |||
| 113 | =head2 $obj = I<OBJ>->string_decode_always(); | ||
| 114 | |||
| 115 | This causes I<OBJ> to decode all CBOR strings (including binary strings) | ||
| 116 | as UTF-8, applying appropriate pre-validation unless C<naive_utf8()> is | ||
| 117 | enabled. This is useful if you expect all strings (including binary) to be | ||
| 118 | UTF-8 and want to handle them in Perl as character strings instead of | ||
| 119 | byte strings. | ||
| 120 | |||
| 121 | =head2 I<OBJ>->set_tag_handlers( %TAG_CALLBACK ) | ||
| 122 | |||
| 123 | Takes a list of key/value pairs where each key is a tag (i.e., number) | ||
| 124 | and each value is a coderef that CBOR::Free will run when that tag is | ||
| 125 | seen during a decode operation. The coderef will receive the tagged value, | ||
| 126 | and its (scalar) return will be inserted into the decoded data structure. | ||
| 127 | |||
| 128 | To unset a tag handler, assign undef to it. | ||
| 129 | |||
| 130 | This returns the I<OBJ>. | ||
| 131 | |||
| 132 | B<NOTE:> Handlers assigned here will only fire if CBOR::Free itself | ||
| 133 | doesn’t decode the tag. For example, a handler for the “indirection” tag | ||
| 134 | here will be ignored. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | 7 | 1; | |