| 1 | # Copyright Infomation | ||
| 2 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 3 | # Author : Dr. Ahmed Amin Elsheshtawy, Ph.D. | ||
| 4 | # Website: https://github.com/mewsoft/Nile, http://www.mewsoft.com | ||
| 5 | # Email : mewsoft@cpan.org, support@mewsoft.com | ||
| 6 | # Copyrights (c) 2014-2015 Mewsoft Corp. All rights reserved. | ||
| 7 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 8 | package Nile::Deserializer; | ||
| 9 | |||
| 10 | our $VERSION = '0.55'; | ||
| 11 | our $AUTHORITY = 'cpan:MEWSOFT'; | ||
| 12 | |||
| 13 | =pod | ||
| 14 | |||
| 15 | =encoding utf8 | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | Nile::Deserializer - Data structures deserializer | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | $data = $app->thaw->json($encoded); | ||
| 24 | $data = $app->thaw->yaml($encoded); | ||
| 25 | $data = $app->thaw->storable($encoded); | ||
| 26 | $data = $app->thaw->dumper($encoded); | ||
| 27 | $data = $app->thaw->xml($encoded); | ||
| 28 | |||
| 29 | # also deserialize method is an alias for freeze | ||
| 30 | $data = $app->deserialize->json($encoded); | ||
| 31 | |||
| 32 | =head1 DESCRIPTION | ||
| 33 | |||
| 34 | Nile::Deserializer - Data structures deserializer | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | use Nile::Base; | ||
| 39 | use Module::Load; | ||
| 40 | extends qw(Nile::Serialization); | ||
| 41 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 42 | =head2 json() | ||
| 43 | |||
| 44 | $encoded = qq!{"lname":"elsheshtawy","fname":"ahmed","phone":{"home":"02222444","mobile":"012222333"}}!; | ||
| 45 | |||
| 46 | $data = $app->thaw->json($encoded); | ||
| 47 | |||
| 48 | # returns: | ||
| 49 | # $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 50 | |||
| 51 | Deserialize a JSON structure to a data structure. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub json { | ||
| 56 | return shift->Json->utf8->decode(@_); | ||
| 57 | } | ||
| 58 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 59 | =head2 yaml() | ||
| 60 | |||
| 61 | $data = $app->thaw->yaml($encoded); | ||
| 62 | |||
| 63 | # returns: | ||
| 64 | # $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 65 | |||
| 66 | Deserialize a YAML structure to a data structure | ||
| 67 | |||
| 68 | =cut | ||
| 69 | |||
| 70 | sub yaml { | ||
| 71 | shift->Yaml; | ||
| 72 | return YAML::thaw(@_); | ||
| 73 | } | ||
| 74 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 75 | =head2 storable() | ||
| 76 | |||
| 77 | $data = $app->thaw->storable($encoded); | ||
| 78 | |||
| 79 | # returns: | ||
| 80 | # $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 81 | |||
| 82 | Deserialize a Storable structure to a data structure. | ||
| 83 | |||
| 84 | =cut | ||
| 85 | |||
| 86 | sub storable { | ||
| 87 | shift->Storable; | ||
| 88 | return Storable::thaw(@_); | ||
| 89 | } | ||
| 90 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 91 | =head2 dumper() | ||
| 92 | |||
| 93 | $data = $app->thaw->dumper($encoded); | ||
| 94 | |||
| 95 | # returns: | ||
| 96 | # $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 97 | |||
| 98 | Deserialize a Data::Dumper structure to a data structure. | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | sub dumper { | ||
| 103 | my ($self, $data) = @_; | ||
| 104 | return undef unless defined $data; | ||
| 105 | my $M = ""; | ||
| 106 | # clearify hashref's as perl may treat it as a block | ||
| 107 | $data = '+'.$data if ($data =~ /^\{/); | ||
| 108 | my $res = eval($data); | ||
| 109 | if ($@) { | ||
| 110 | $self->me->abort("Unable to deserialize : $@. $data"); | ||
| 111 | } | ||
| 112 | return $M ? $M : $res; | ||
| 113 | } | ||
| 114 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 115 | =head2 xml() | ||
| 116 | |||
| 117 | $data = $app->thaw->xml($encoded); | ||
| 118 | |||
| 119 | # returns: | ||
| 120 | # $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 121 | |||
| 122 | Deserialize a XML structure to a data structure | ||
| 123 | |||
| 124 | =cut | ||
| 125 | |||
| 126 | sub xml { | ||
| 127 | my $xml = shift->Xml->parse(@_); | ||
| 128 | return $xml; | ||
| 129 | } | ||
| 130 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 131 | has 'SerealDecoder' => ( | ||
| 132 | is => 'rw', | ||
| 133 | isa => 'Sereal::Decoder', | ||
| 134 | lazy => 1, | ||
| 135 | default => sub { | ||
| 136 | load Sereal::Decoder; | ||
| 137 | Sereal::Decoder->new; | ||
| 138 | } | ||
| 139 | ); | ||
| 140 | |||
| 141 | =head2 sereal() | ||
| 142 | |||
| 143 | # $encoded: binary data | ||
| 144 | |||
| 145 | $data = $app->thaw->sereal($encoded); | ||
| 146 | |||
| 147 | # returns: | ||
| 148 | # $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 149 | |||
| 150 | |||
| 151 | Deserialize binary data to data structure to using L<Sereal::Decoder>. | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | sub sereal { | ||
| 156 | my ($self) = shift; | ||
| 157 | return $self->SerealDecoder->decode(@_); | ||
| 158 | } | ||
| 159 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 160 | |||
| 161 | =pod | ||
| 162 | |||
| 163 | =head1 Bugs | ||
| 164 | |||
| 165 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 166 | |||
| 167 | =head1 HOMEPAGE | ||
| 168 | |||
| 169 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 170 | |||
| 171 | =head1 SOURCE | ||
| 172 | |||
| 173 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 174 | |||
| 175 | =head1 SEE ALSO | ||
| 176 | |||
| 177 | See L<Nile> for details about the complete framework. | ||
| 178 | |||
| 179 | =head1 AUTHOR | ||
| 180 | |||
| 181 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 182 | Website: http://www.mewsoft.com | ||
| 183 | |||
| 184 | =head1 COPYRIGHT AND LICENSE | ||
| 185 | |||
| 186 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 187 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 188 | |||
| 189 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | 1; |