diff options
author | franck cuny <franck@lumberjaph.net> | 2009-12-08 16:13:42 +0100 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2009-12-08 16:13:42 +0100 |
commit | 263e87473b4f1195adc93dd4b69fa6557c8d9be8 (patch) | |
tree | adf9c598f1aab0852fadccefd6e11277e26b70aa /lib | |
parent | move serialize into a role (diff) | |
download | moosex-net-api-263e87473b4f1195adc93dd4b69fa6557c8d9be8.tar.gz |
method to deserialize
Diffstat (limited to '')
-rw-r--r-- | lib/MooseX/Net/API/Role/Deserialize.pm | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/lib/MooseX/Net/API/Role/Deserialize.pm b/lib/MooseX/Net/API/Role/Deserialize.pm index b83c58a..cf69087 100644 --- a/lib/MooseX/Net/API/Role/Deserialize.pm +++ b/lib/MooseX/Net/API/Role/Deserialize.pm @@ -4,13 +4,21 @@ use Moose::Role; use JSON::XS; use YAML::Syck; use XML::Simple; +use Try::Tiny; + +my $reverse_content_type = { + 'application/json' => 'json', + 'application/x-yaml' => 'yaml', + 'text/xml' => 'xml', + 'application/xml' => 'xml', +}; sub _from_json { return decode_json( $_[1] ); } sub _from_yaml { - return Dump $_[1]; + return Load $_[1]; } sub _from_xml { @@ -18,4 +26,24 @@ sub _from_xml { $xml->XMLin( $_[1] ); } +sub _do_deserialization { + my ( $caller, $raw_content, @content_types ) = @_; + + my $content; + foreach my $deserializer (@content_types) { + my $method; + if ( $reverse_content_type->{$deserializer} ) { + $method = '_from_' . $reverse_content_type->{$deserializer}; + } + else { + $method = '_from_' . $deserializer; + } + next if ( !$caller->meta->find_method_by_name($method) ); + try { + $content = $caller->$method($raw_content); + }; + return $content if $content; + } +} + 1; |