| 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::Serializer; | ||
| 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::Serializer - Data structures Serializer | ||
| 20 | |||
| 21 | =head1 SYNOPSIS | ||
| 22 | |||
| 23 | $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 24 | |||
| 25 | $encoded = $app->freeze->json($data); | ||
| 26 | $encoded = $app->freeze->yaml($data); | ||
| 27 | $encoded = $app->freeze->storable($data); | ||
| 28 | $encoded = $app->freeze->dumper($data); | ||
| 29 | $encoded = $app->freeze->xml($data); | ||
| 30 | |||
| 31 | # also serialize method is an alias for freeze | ||
| 32 | $encoded = $app->serialize->json($data); | ||
| 33 | |||
| 34 | =head1 DESCRIPTION | ||
| 35 | |||
| 36 | Nile::Serializer - Data structures Serializer | ||
| 37 | |||
| 38 | =cut | ||
| 39 | |||
| 40 | #use Nile::Base; | ||
| 41 | use Moose; | ||
| 42 | use Module::Load; | ||
| 43 | extends qw(Nile::Serialization); | ||
| 44 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 45 | =head2 json() | ||
| 46 | |||
| 47 | $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 48 | |||
| 49 | $encoded = $app->freeze->json($data); | ||
| 50 | |||
| 51 | # returns: | ||
| 52 | |||
| 53 | # {"lname":"elsheshtawy","fname":"ahmed","phone":{"home":"02222444","mobile":"012222333"}} | ||
| 54 | |||
| 55 | Serialize a data structure to a JSON structure. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub json { | ||
| 60 | return shift->Json->utf8->encode(@_); | ||
| 61 | } | ||
| 62 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 63 | =head2 yaml() | ||
| 64 | |||
| 65 | $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 66 | |||
| 67 | $encoded = $app->freeze->yaml($data); | ||
| 68 | |||
| 69 | # returns: | ||
| 70 | |||
| 71 | # --- | ||
| 72 | # fname: ahmed | ||
| 73 | # lname: elsheshtawy | ||
| 74 | # phone: | ||
| 75 | # home: 02222444 | ||
| 76 | # mobile: 012222333 | ||
| 77 | |||
| 78 | Serialize a data structure to a YAML structure. | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | sub yaml { | ||
| 83 | shift->Yaml; | ||
| 84 | return YAML::freeze(@_); | ||
| 85 | } | ||
| 86 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 87 | =head2 storable() | ||
| 88 | |||
| 89 | $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 90 | |||
| 91 | $encoded = $app->freeze->storable($data); | ||
| 92 | |||
| 93 | # returns: binary data | ||
| 94 | |||
| 95 | Serialize a data structure to a Storable structure. | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | sub storable { | ||
| 100 | shift->Storable; | ||
| 101 | # using network byte order makes sense to always do, under all circumstances to make it platform neutral | ||
| 102 | return Storable::nfreeze(@_); | ||
| 103 | #return Storable::freeze(@_); | ||
| 104 | } | ||
| 105 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 106 | =head2 dumper() | ||
| 107 | |||
| 108 | $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 109 | |||
| 110 | $encoded = $app->freeze->dumper($data); | ||
| 111 | |||
| 112 | # returns: | ||
| 113 | |||
| 114 | # $M = {'lname' => 'elsheshtawy','fname' => 'ahmed','phone' => {'home' => '02222444','mobile' => '012222333'}}; | ||
| 115 | |||
| 116 | Serialize a data structure to a Data::Dumper structure. | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub dumper { | ||
| 121 | |||
| 122 | my ($self, $data) = @_; | ||
| 123 | |||
| 124 | my $d = Data::Dumper->new([$data], ["M"]); | ||
| 125 | |||
| 126 | $d->Purity(1); | ||
| 127 | $d->Quotekeys(1); | ||
| 128 | $d->Deepcopy(0); | ||
| 129 | $d->Indent(0); | ||
| 130 | $d->Terse(0); | ||
| 131 | $d->Useqq(0); | ||
| 132 | |||
| 133 | return $d->Dump; | ||
| 134 | } | ||
| 135 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 136 | =head2 xml() | ||
| 137 | |||
| 138 | $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 139 | |||
| 140 | $encoded = $app->freeze->xml($data); | ||
| 141 | |||
| 142 | # returns: | ||
| 143 | |||
| 144 | # <?xml version="1.0" encoding="UTF-8" ?> | ||
| 145 | # <fname>ahmed</fname> | ||
| 146 | # <lname>elsheshtawy</lname> | ||
| 147 | # <phone> | ||
| 148 | # <home>02222444</home> | ||
| 149 | # <mobile>012222333</mobile> | ||
| 150 | # </phone> | ||
| 151 | |||
| 152 | |||
| 153 | Serialize a data structure to a XML structure. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | sub xml { | ||
| 158 | my $xml = shift->Xml; | ||
| 159 | $xml->set(output_encoding => 'UTF-8'); | ||
| 160 | $xml->set(indent => 0); | ||
| 161 | return $xml->write(@_); | ||
| 162 | } | ||
| 163 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 164 | has 'SerealEncoder' => ( | ||
| 165 | is => 'rw', | ||
| 166 | isa => 'Sereal::Encoder', | ||
| 167 | lazy => 1, | ||
| 168 | default => sub { | ||
| 169 | load Sereal::Encoder; | ||
| 170 | Sereal::Encoder->new; | ||
| 171 | } | ||
| 172 | ); | ||
| 173 | |||
| 174 | =head2 sereal() | ||
| 175 | |||
| 176 | $data = {fname=>"ahmed", lname=>"elsheshtawy", phone=>{mobile=>"012222333", home=>"02222444"}}; | ||
| 177 | |||
| 178 | $encoded = $app->freeze->sereal($data); | ||
| 179 | |||
| 180 | # returns: binary data | ||
| 181 | |||
| 182 | Serialize a data structure to a binary data using L<Sereal::Encoder>. | ||
| 183 | |||
| 184 | =cut | ||
| 185 | |||
| 186 | sub sereal { | ||
| 187 | my ($self) = shift; | ||
| 188 | return $self->SerealEncoder->encode(@_); | ||
| 189 | } | ||
| 190 | #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
| 191 | |||
| 192 | =pod | ||
| 193 | |||
| 194 | =head1 Bugs | ||
| 195 | |||
| 196 | This project is available on github at L<https://github.com/mewsoft/Nile>. | ||
| 197 | |||
| 198 | =head1 HOMEPAGE | ||
| 199 | |||
| 200 | Please visit the project's homepage at L<https://metacpan.org/release/Nile>. | ||
| 201 | |||
| 202 | =head1 SOURCE | ||
| 203 | |||
| 204 | Source repository is at L<https://github.com/mewsoft/Nile>. | ||
| 205 | |||
| 206 | =head1 SEE ALSO | ||
| 207 | |||
| 208 | See L<Nile> for details about the complete framework. | ||
| 209 | |||
| 210 | =head1 AUTHOR | ||
| 211 | |||
| 212 | Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> | ||
| 213 | Website: http://www.mewsoft.com | ||
| 214 | |||
| 215 | =head1 COPYRIGHT AND LICENSE | ||
| 216 | |||
| 217 | Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, | ||
| 218 | L<https://github.com/mewsoft/Nile>, L<http://www.mewsoft.com> | ||
| 219 | |||
| 220 | This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 221 | |||
| 222 | =cut | ||
| 223 | |||
| 224 | 1; |