line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Dancer2::Serializer::JSON; |
2
|
|
|
|
|
|
|
# ABSTRACT: Serializer for handling JSON data |
3
|
|
|
|
|
|
|
$Dancer2::Serializer::JSON::VERSION = '1.0.0'; |
4
|
21
|
|
|
21
|
|
110044
|
use Moo; |
|
21
|
|
|
|
|
7245
|
|
|
21
|
|
|
|
|
220
|
|
5
|
21
|
|
|
21
|
|
10522
|
use JSON::MaybeXS (); |
|
21
|
|
|
|
|
83
|
|
|
21
|
|
|
|
|
463
|
|
6
|
21
|
|
|
21
|
|
140
|
use Scalar::Util 'blessed'; |
|
21
|
|
|
|
|
63
|
|
|
21
|
|
|
|
|
8508
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
with 'Dancer2::Core::Role::Serializer'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has '+content_type' => ( default => sub {'application/json'} ); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
# helpers |
13
|
6
|
|
|
6
|
1
|
148
|
sub from_json { __PACKAGE__->deserialize(@_) } |
14
|
|
|
|
|
|
|
|
15
|
24
|
|
|
24
|
1
|
3332
|
sub to_json { __PACKAGE__->serialize(@_) } |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub decode_json { |
18
|
2
|
|
|
2
|
0
|
4707
|
my ( $entity ) = @_; |
19
|
|
|
|
|
|
|
|
20
|
2
|
|
|
|
|
26
|
JSON::MaybeXS::decode_json($entity); |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub encode_json { |
24
|
2
|
|
|
2
|
0
|
5
|
my ( $entity ) = @_; |
25
|
|
|
|
|
|
|
|
26
|
2
|
|
|
|
|
21
|
JSON::MaybeXS::encode_json($entity); |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
# class definition |
30
|
|
|
|
|
|
|
sub serialize { |
31
|
|
|
|
|
|
|
my ( $self, $entity, $options ) = @_; |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $config = blessed $self ? $self->config : {}; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
foreach (keys %$config) { |
36
|
|
|
|
|
|
|
$options->{$_} = $config->{$_} unless exists $options->{$_}; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
$options->{utf8} = 1 if !defined $options->{utf8}; |
40
|
|
|
|
|
|
|
JSON::MaybeXS->new($options)->encode($entity); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub deserialize { |
44
|
|
|
|
|
|
|
my ( $self, $entity, $options ) = @_; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
$options->{utf8} = 1 if !defined $options->{utf8}; |
47
|
|
|
|
|
|
|
JSON::MaybeXS->new($options)->decode($entity); |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
1; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
__END__ |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=pod |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=encoding UTF-8 |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 NAME |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Dancer2::Serializer::JSON - Serializer for handling JSON data |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=head1 VERSION |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
version 1.0.0 |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
=head1 DESCRIPTION |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
This is a serializer engine that allows you to turn Perl data structures into |
69
|
|
|
|
|
|
|
JSON output and vice-versa. |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head2 content_type |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Returns 'application/json' |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=head1 METHODS |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head2 serialize($content) |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Serializes a Perl data structure into a JSON string. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
=head2 deserialize($content) |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
Deserializes a JSON string into a Perl data structure. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
=head1 FUNCTIONS |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=head2 from_json($content, \%options) |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
This is an helper available to transform a JSON data structure to a Perl data structures. |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head2 to_json($content, \%options) |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
This is an helper available to transform a Perl data structure to JSON. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Calling this function will B<not> trigger the serialization's hooks. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=head2 Configuring the JSON Serializer using C<set engines> |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
The JSON serializer options can be configured via C<set engines>. The most |
102
|
|
|
|
|
|
|
common settings are: |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=over 4 |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=item allow_nonref |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
Ignore non-ref scalars returned from handlers. With this set the "Hello, World!" |
109
|
|
|
|
|
|
|
handler returning a string will be dealt with properly. |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=back |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Set engines should be called prior to setting JSON as the serializer: |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
set engines => |
116
|
|
|
|
|
|
|
{ |
117
|
|
|
|
|
|
|
serializer => |
118
|
|
|
|
|
|
|
{ |
119
|
|
|
|
|
|
|
JSON => |
120
|
|
|
|
|
|
|
{ |
121
|
|
|
|
|
|
|
allow_nonref => 1 |
122
|
|
|
|
|
|
|
}, |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
}; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
set serializer => 'JSON'; |
127
|
|
|
|
|
|
|
set content_type => 'application/json'; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 Returning non-JSON data. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Handlers can return non-JSON via C<send_as>, which overrides the default serializer: |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
get '/' => |
134
|
|
|
|
|
|
|
sub |
135
|
|
|
|
|
|
|
{ |
136
|
|
|
|
|
|
|
send_as html => |
137
|
|
|
|
|
|
|
q{Welcome to the root of all evil...<br>step into my office.} |
138
|
|
|
|
|
|
|
}; |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Any other non-JSON returned format supported by 'send_as' can be used. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 AUTHOR |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
Dancer Core Developers |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
This software is copyright (c) 2023 by Alexis Sukrieh. |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
151
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |