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