line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer::Serializer::JSON; |
2
|
|
|
|
|
|
|
our $AUTHORITY = 'cpan:SUKRIA'; |
3
|
|
|
|
|
|
|
#ABSTRACT: serializer for handling JSON data |
4
|
|
|
|
|
|
|
$Dancer::Serializer::JSON::VERSION = '1.3521'; |
5
|
165
|
|
|
165
|
|
1151
|
use strict; |
|
165
|
|
|
|
|
441
|
|
|
165
|
|
|
|
|
4805
|
|
6
|
165
|
|
|
165
|
|
946
|
use warnings; |
|
165
|
|
|
|
|
422
|
|
|
165
|
|
|
|
|
3954
|
|
7
|
165
|
|
|
165
|
|
891
|
use Carp; |
|
165
|
|
|
|
|
479
|
|
|
165
|
|
|
|
|
9353
|
|
8
|
165
|
|
|
165
|
|
1218
|
use Dancer::ModuleLoader; |
|
165
|
|
|
|
|
517
|
|
|
165
|
|
|
|
|
5484
|
|
9
|
165
|
|
|
165
|
|
1022
|
use Dancer::Deprecation; |
|
165
|
|
|
|
|
511
|
|
|
165
|
|
|
|
|
4880
|
|
10
|
165
|
|
|
165
|
|
1128
|
use Dancer::Config 'setting'; |
|
165
|
|
|
|
|
442
|
|
|
165
|
|
|
|
|
8675
|
|
11
|
165
|
|
|
165
|
|
1149
|
use Dancer::Exception qw(:all); |
|
165
|
|
|
|
|
500
|
|
|
165
|
|
|
|
|
19567
|
|
12
|
165
|
|
|
165
|
|
1296
|
use base 'Dancer::Serializer::Abstract'; |
|
165
|
|
|
|
|
412
|
|
|
165
|
|
|
|
|
75615
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# helpers |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub from_json { |
18
|
13
|
|
|
13
|
0
|
152
|
my $s = Dancer::Serializer::JSON->new; |
19
|
13
|
|
|
|
|
69
|
$s->deserialize(@_); |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub to_json { |
23
|
8
|
|
|
8
|
0
|
67
|
my $s = Dancer::Serializer::JSON->new; |
24
|
8
|
|
|
|
|
32
|
$s->serialize(@_); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# class definition |
28
|
|
|
|
|
|
|
|
29
|
38
|
|
|
38
|
0
|
202
|
sub loaded { Dancer::ModuleLoader->load_with_params('JSON', '-support_by_pp') } |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub init { |
32
|
38
|
|
|
38
|
1
|
108
|
my ($self) = @_; |
33
|
38
|
50
|
|
|
|
131
|
raise core_serializer => 'JSON is needed and is not installed' |
34
|
|
|
|
|
|
|
unless $self->loaded; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub serialize { |
38
|
32
|
|
|
32
|
1
|
1997
|
my $self = shift; |
39
|
32
|
|
|
|
|
56
|
my $entity = shift; |
40
|
|
|
|
|
|
|
|
41
|
32
|
|
100
|
|
|
111
|
my $options = $self->_serialize_options_as_hashref(@_) || {}; |
42
|
|
|
|
|
|
|
|
43
|
32
|
|
50
|
|
|
127
|
my $config = setting('engines') || {}; |
44
|
32
|
|
100
|
|
|
163
|
$config = $config->{JSON} || {}; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
# straight pass through of config options to JSON |
47
|
32
|
|
|
|
|
98
|
map { $options->{$_} = $config->{$_} } keys %$config; |
|
22
|
|
|
|
|
57
|
|
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
# pull in config from serializer init as well (and possibly override settings from the conf file) |
50
|
32
|
|
|
|
|
393
|
map { $options->{$_} = $self->config->{$_} } keys %{$self->config}; |
|
4
|
|
|
|
|
10
|
|
|
32
|
|
|
|
|
169
|
|
51
|
|
|
|
|
|
|
|
52
|
32
|
100
|
100
|
|
|
102
|
if (setting('environment') eq 'development' and not defined $options->{pretty}) { |
53
|
26
|
|
|
|
|
62
|
$options->{pretty} = 1; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
32
|
|
|
|
|
273
|
JSON::to_json( $entity, $options ); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub deserialize { |
60
|
22
|
|
|
22
|
1
|
820
|
my $self = shift; |
61
|
22
|
|
|
|
|
46
|
my $entity = shift; |
62
|
|
|
|
|
|
|
|
63
|
22
|
|
|
|
|
64
|
my $options = $self->_deserialize_options_as_hashref(@_); |
64
|
22
|
|
|
|
|
93
|
JSON::from_json( $entity, $options ); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Standard JSON behaviour is fine when serializing; we'll end up |
68
|
|
|
|
|
|
|
# encoding as UTF8 later on. |
69
|
|
|
|
|
|
|
sub _serialize_options_as_hashref { |
70
|
32
|
|
|
32
|
|
102
|
return shift->_options_as_hashref(@_); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
# JSON should be UTF8 by default, so explicitly decode it as such |
74
|
|
|
|
|
|
|
# on its way in. |
75
|
|
|
|
|
|
|
sub _deserialize_options_as_hashref { |
76
|
22
|
|
|
22
|
|
49
|
my $self = shift; |
77
|
22
|
|
50
|
|
|
74
|
my $options = $self->_options_as_hashref(@_) || {}; |
78
|
22
|
50
|
|
|
|
80
|
$options->{utf8} = 1 if !exists $options->{utf8}; |
79
|
22
|
|
|
|
|
47
|
return $options; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub _options_as_hashref { |
83
|
54
|
|
|
54
|
|
83
|
my $self = shift; |
84
|
|
|
|
|
|
|
|
85
|
54
|
100
|
|
|
|
412
|
return if scalar @_ == 0; |
86
|
|
|
|
|
|
|
|
87
|
2
|
50
|
|
|
|
9
|
if ( scalar @_ == 1 ) { |
|
|
0
|
|
|
|
|
|
88
|
2
|
|
|
|
|
8
|
return shift; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
elsif ( scalar @_ % 2 ) { |
91
|
0
|
|
|
|
|
0
|
carp "options for to_json/from_json must be key value pairs (as a hashref)"; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
0
|
|
|
|
|
0
|
Dancer::Deprecation->deprecated( |
95
|
|
|
|
|
|
|
version => '1.3002', |
96
|
|
|
|
|
|
|
fatal => 1, |
97
|
|
|
|
|
|
|
message => 'options as hash for to_json/from_json is DEPRECATED. please pass a hashref', |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
27
|
|
|
27
|
1
|
1145
|
sub content_type {'application/json'} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
1; |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
__END__ |