line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Role::REST::Client::Serializer; |
2
|
|
|
|
|
|
|
$Role::REST::Client::Serializer::VERSION = '0.21'; |
3
|
4
|
|
|
4
|
|
1340
|
use Try::Tiny; |
|
4
|
|
|
|
|
1519
|
|
|
4
|
|
|
|
|
188
|
|
4
|
4
|
|
|
4
|
|
411
|
use Moo; |
|
4
|
|
|
|
|
8659
|
|
|
4
|
|
|
|
|
27
|
|
5
|
4
|
|
|
4
|
|
5308
|
use Types::Standard qw(Enum InstanceOf); |
|
4
|
|
|
|
|
54483
|
|
|
4
|
|
|
|
|
37
|
|
6
|
4
|
|
|
4
|
|
4312
|
use Data::Serializer::Raw; |
|
4
|
|
|
|
|
2429
|
|
|
4
|
|
|
|
|
1238
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has 'type' => ( |
9
|
|
|
|
|
|
|
isa => Enum[qw{application/json application/xml application/yaml application/x-www-form-urlencoded text/javascript}], |
10
|
|
|
|
|
|
|
is => 'rw', |
11
|
|
|
|
|
|
|
default => sub { 'application/json' }, |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has 'serializer' => ( |
15
|
|
|
|
|
|
|
isa => InstanceOf['Data::Serializer::Raw'], |
16
|
|
|
|
|
|
|
is => 'ro', |
17
|
|
|
|
|
|
|
default => \&_set_serializer, |
18
|
|
|
|
|
|
|
lazy => 1, |
19
|
|
|
|
|
|
|
); |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our %modules = ( |
22
|
|
|
|
|
|
|
'application/json' => { |
23
|
|
|
|
|
|
|
module => 'JSON', |
24
|
|
|
|
|
|
|
}, |
25
|
|
|
|
|
|
|
'application/xml' => { |
26
|
|
|
|
|
|
|
module => 'XML::Simple', |
27
|
|
|
|
|
|
|
}, |
28
|
|
|
|
|
|
|
'application/yaml' => { |
29
|
|
|
|
|
|
|
module => 'YAML', |
30
|
|
|
|
|
|
|
}, |
31
|
|
|
|
|
|
|
'application/x-www-form-urlencoded' => { |
32
|
|
|
|
|
|
|
module => 'FORM', |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
'text/javascript' => { |
35
|
|
|
|
|
|
|
module => 'JSON', |
36
|
|
|
|
|
|
|
}, |
37
|
|
|
|
|
|
|
); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
sub _set_serializer { |
40
|
5
|
|
|
5
|
|
55
|
my $self = shift; |
41
|
5
|
50
|
|
|
|
75
|
return unless $modules{$self->type}; |
42
|
|
|
|
|
|
|
|
43
|
5
|
|
|
|
|
102
|
my $module = $modules{$self->type}{module}; |
44
|
5
|
50
|
|
|
|
43
|
return $module if $module eq 'FORM'; |
45
|
|
|
|
|
|
|
|
46
|
5
|
|
|
|
|
47
|
return Data::Serializer::Raw->new( |
47
|
|
|
|
|
|
|
serializer => $module, |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
sub content_type { |
52
|
4
|
|
|
4
|
0
|
380156
|
my ($self) = @_; |
53
|
4
|
|
|
|
|
90
|
return $self->type; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub serialize { |
57
|
7
|
|
|
7
|
0
|
2133
|
my ($self, $data) = @_; |
58
|
7
|
50
|
|
|
|
148
|
return unless $self->serializer; |
59
|
|
|
|
|
|
|
|
60
|
7
|
|
|
|
|
15840
|
my $result; |
61
|
|
|
|
|
|
|
try { |
62
|
7
|
|
|
7
|
|
443
|
$result = $self->serializer->serialize($data) |
63
|
|
|
|
|
|
|
} catch { |
64
|
0
|
|
|
0
|
|
0
|
warn "Couldn't serialize data with " . $self->type . ": $_"; |
65
|
7
|
|
|
|
|
60
|
}; |
66
|
|
|
|
|
|
|
|
67
|
7
|
|
|
|
|
11605
|
return $result; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub deserialize { |
71
|
10
|
|
|
10
|
0
|
3332
|
my ($self, $data) = @_; |
72
|
10
|
50
|
|
|
|
201
|
return unless $self->serializer; |
73
|
|
|
|
|
|
|
|
74
|
10
|
|
|
|
|
1129
|
my $result; |
75
|
|
|
|
|
|
|
try { |
76
|
10
|
|
|
10
|
|
627
|
$result = $self->serializer->deserialize($data); |
77
|
|
|
|
|
|
|
} catch { |
78
|
0
|
|
|
0
|
|
0
|
warn "Couldn't deserialize data with " . $self->type . ": $_"; |
79
|
10
|
|
|
|
|
77
|
}; |
80
|
|
|
|
|
|
|
|
81
|
10
|
|
|
|
|
71458
|
return $result; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
1; |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
__END__ |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=pod |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=encoding UTF-8 |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
=head1 NAME |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Role::REST::Client::Serializer |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=head1 VERSION |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
version 0.21 |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 AUTHOR |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Kaare Rasmussen <kaare at cpan dot org> |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Kaare Rasmussen. |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
109
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=cut |