blob: cf69087984cd648bff391ff45319bf7668659d95 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
package MooseX::Net::API::Role::Deserialize;
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 Load $_[1];
}
sub _from_xml {
my $xml = XML::Simple->new( ForceArray => 0 );
$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;
|