| 1 | package Data::Structure::Deserialize::Auto 1.01; | ||
| 2 | 9 | ▶ | use v5.22; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 9 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | # ABSTRACT: Deserializes data structures from perl, JSON, YAML, or TOML data, from strings or files | ||
| 6 | |||
| 7 | =encoding UTF-8 | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Data::Structure::Deserialize::Auto - deserializes data structures from perl, JSON, YAML, or TOML data, from strings or files | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Data::Structure::Deserialize::Auto qw(deserialize); | ||
| 16 | |||
| 17 | my $str = '{"db": {"host": "localhost"}}'; | ||
| 18 | my $ds = deserialize($str); #autodetects JSON | ||
| 19 | say $ds->{db}->{host}; # localhost | ||
| 20 | |||
| 21 | # OR | ||
| 22 | |||
| 23 | $str = <<'END'; | ||
| 24 | options: | ||
| 25 | autosave: 1 | ||
| 26 | END | ||
| 27 | $ds = deserialize($str); #autodetects YAML | ||
| 28 | say $ds->{options}->{autosave}; # 1 | ||
| 29 | |||
| 30 | # OR | ||
| 31 | |||
| 32 | use Data::Dumper; | ||
| 33 | $Data::Dumper::Terse = 1; | ||
| 34 | my $filename = ...; | ||
| 35 | open(my $FH, '>', $filename); | ||
| 36 | my $data = { | ||
| 37 | a => 1, | ||
| 38 | b => 2, | ||
| 39 | c => 3 | ||
| 40 | }; | ||
| 41 | print $FH Dumper($data); | ||
| 42 | close($FH); | ||
| 43 | $ds = deserialize($filename); #autodetects perl in referenced file | ||
| 44 | say $ds->{b}; # 2 | ||
| 45 | |||
| 46 | =head1 DESCRIPTION | ||
| 47 | |||
| 48 | L<Data::Structure::Deserialize::Auto> is a module for converting a string in an | ||
| 49 | arbitrary format (one of perl/JSON/YAML/TOML) into a perl data structure, without | ||
| 50 | needing to worry about what format it was in. | ||
| 51 | |||
| 52 | If the string argument given to it is a valid local filename, it is treated as | ||
| 53 | such, and that file's contents are processed instead. | ||
| 54 | |||
| 55 | =head1 FUNCTIONS | ||
| 56 | |||
| 57 | =head2 deserialize( $str[, $hint] ) | ||
| 58 | |||
| 59 | Given a string as its first argument, returns a perl data structure by decoding | ||
| 60 | the perl (L<Data::Dumper>), JSON, YAML, or TOML string. Or, if the string is a valid | ||
| 61 | filename, by decoding the contents of that file. | ||
| 62 | |||
| 63 | If a hint is given as the second argument, where its value is one of C<yaml>, | ||
| 64 | C<json>, C<toml> or C<perl>, then this type of deserialization is tried first. | ||
| 65 | This may be necessary in certain rare edge cases where the input value's format | ||
| 66 | is ambiguous. | ||
| 67 | |||
| 68 | This function can be exported | ||
| 69 | |||
| 70 | =cut | ||
| 71 | |||
| 72 | 9 | ▶ | use base qw(Exporter); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 73 | |||
| 74 | 9 | ▶ | use File::Basename; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 75 | 0 | ▶ | use IO::All; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 76 | use JSON qw(decode_json); | ||
| 77 | use Readonly; | ||
| 78 | use Syntax::Keyword::Try; | ||
| 79 | use TOML qw(from_toml); | ||
| 80 | use YAML::XS; | ||
| 81 | |||
| 82 | use experimental qw(signatures); | ||
| 83 | |||
| 84 | Readonly::Hash my %FILE_TYPES => ( | ||
| 85 | yml => 'yaml', | ||
| 86 | yaml => 'yaml', | ||
| 87 | toml => 'toml', | ||
| 88 | json => 'json', | ||
| 89 | ); | ||
| 90 | Readonly::Array my @DECODER_PRIORITY => qw(perl yaml json toml); | ||
| 91 | |||
| 92 | our @EXPORT_OK = qw( | ||
| 93 | deserialize | ||
| 94 | ); | ||
| 95 | |||
| 96 | sub _decoders() { | ||
| 97 | return ( | ||
| 98 | yaml => sub($v) { | ||
| 99 | Load($v); | ||
| 100 | }, | ||
| 101 | json => sub($v) { | ||
| 102 | decode_json($v); | ||
| 103 | }, | ||
| 104 | toml => sub($v) { | ||
| 105 | from_toml($v); | ||
| 106 | }, | ||
| 107 | perl => sub($v) { | ||
| 108 | no warnings 'syntax'; | ||
| 109 | eval($v); ## no critic (ProhibitStringyEval) | ||
| 110 | }, | ||
| 111 | ); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub _is_filename($str) { | ||
| 115 | return 0 if ($str =~ /\n/); | ||
| 116 | return (-f $str); | ||
| 117 | } | ||
| 118 | |||
| 119 | sub deserialize($v, $hint = undef) { | ||
| 120 | return $v if (ref($v) eq 'HASH'); | ||
| 121 | |||
| 122 | my @decoders = @DECODER_PRIORITY; | ||
| 123 | my %decoders = _decoders(); | ||
| 124 | if (_is_filename($v)) { | ||
| 125 | my ($fn, $dirs, $suffix) = fileparse($v, keys(%FILE_TYPES)); | ||
| 126 | unshift(@decoders, $FILE_TYPES{$suffix}) if (defined($suffix) && defined($FILE_TYPES{$suffix})); | ||
| 127 | $v = io->file($v)->slurp; | ||
| 128 | } | ||
| 129 | unshift(@decoders, $hint) if (defined($hint)); | ||
| 130 | my $n; | ||
| 131 | do { | ||
| 132 | $n = shift(@decoders); | ||
| 133 | my $decoder = $decoders{$n}; | ||
| 134 | try { | ||
| 135 | my $structure = $decoder->($v); | ||
| 136 | if (ref($structure) eq 'HASH' || ref($structure) eq 'ARRAY') { | ||
| 137 | # warn("decoded using '$n'"); | ||
| 138 | return $decoder->($v); | ||
| 139 | } | ||
| 140 | } catch { | ||
| 141 | # ignore any errors and try the next decoder, or die out at the bottom | ||
| 142 | }; | ||
| 143 | } while (@decoders); | ||
| 144 | die("Data::Structure::Deserialize::Auto was unable to process the input"); | ||
| 145 | } | ||
| 146 | |||
| 147 | =pod | ||
| 148 | |||
| 149 | =head1 AUTHOR | ||
| 150 | |||
| 151 | Mark Tyrrell C<< <mark@tyrrminal.dev> >> | ||
| 152 | |||
| 153 | =head1 LICENSE | ||
| 154 | |||
| 155 | Copyright (c) 2024 Mark Tyrrell | ||
| 156 | |||
| 157 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| 158 | of this software and associated documentation files (the "Software"), to deal | ||
| 159 | in the Software without restriction, including without limitation the rights | ||
| 160 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| 161 | copies of the Software, and to permit persons to whom the Software is | ||
| 162 | furnished to do so, subject to the following conditions: | ||
| 163 | |||
| 164 | The above copyright notice and this permission notice shall be included in all | ||
| 165 | copies or substantial portions of the Software. | ||
| 166 | |||
| 167 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| 168 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| 169 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| 170 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| 171 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| 172 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| 173 | SOFTWARE. | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | 1; | ||
| 178 | |||
| 179 | __END__ | ||