line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# Licensed to Elasticsearch B.V. under one or more contributor |
2
|
|
|
|
|
|
|
# license agreements. See the NOTICE file distributed with |
3
|
|
|
|
|
|
|
# this work for additional information regarding copyright |
4
|
|
|
|
|
|
|
# ownership. Elasticsearch B.V. licenses this file to you under |
5
|
|
|
|
|
|
|
# the Apache License, Version 2.0 (the "License"); you may |
6
|
|
|
|
|
|
|
# not use this file except in compliance with the License. |
7
|
|
|
|
|
|
|
# You may obtain a copy of the License at |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
10
|
|
|
|
|
|
|
# |
11
|
|
|
|
|
|
|
# Unless required by applicable law or agreed to in writing, |
12
|
|
|
|
|
|
|
# software distributed under the License is distributed on an |
13
|
|
|
|
|
|
|
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14
|
|
|
|
|
|
|
# KIND, either express or implied. See the License for the |
15
|
|
|
|
|
|
|
# specific language governing permissions and limitations |
16
|
|
|
|
|
|
|
# under the License. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
package Search::Elasticsearch::Role::Serializer::JSON; |
19
|
|
|
|
|
|
|
$Search::Elasticsearch::Role::Serializer::JSON::VERSION = '7.717'; |
20
|
55
|
|
|
55
|
|
210998
|
use Moo::Role; |
|
55
|
|
|
|
|
117
|
|
|
55
|
|
|
|
|
305
|
|
21
|
|
|
|
|
|
|
requires 'JSON'; |
22
|
|
|
|
|
|
|
|
23
|
55
|
|
|
55
|
|
16919
|
use Search::Elasticsearch::Util qw(throw); |
|
55
|
|
|
|
|
151
|
|
|
55
|
|
|
|
|
406
|
|
24
|
55
|
|
|
55
|
|
15254
|
use Try::Tiny; |
|
55
|
|
|
|
|
102
|
|
|
55
|
|
|
|
|
2851
|
|
25
|
55
|
|
|
55
|
|
20588
|
use Encode qw(encode_utf8 decode_utf8 is_utf8); |
|
55
|
|
|
|
|
562147
|
|
|
55
|
|
|
|
|
5662
|
|
26
|
55
|
|
|
55
|
|
1825
|
use namespace::clean; |
|
55
|
|
|
|
|
825
|
|
|
55
|
|
|
|
|
1270
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has 'mime_type' => ( is => 'ro', default => 'application/json' ); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
with 'Search::Elasticsearch::Role::Serializer'; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
#=================================== |
33
|
|
|
|
|
|
|
sub encode { |
34
|
|
|
|
|
|
|
#=================================== |
35
|
117
|
|
|
117
|
1
|
7503
|
my ( $self, $var ) = @_; |
36
|
117
|
100
|
|
|
|
299
|
unless ( ref $var ) { |
37
|
60
|
100
|
|
|
|
335
|
return is_utf8($var) |
38
|
|
|
|
|
|
|
? encode_utf8($var) |
39
|
|
|
|
|
|
|
: $var; |
40
|
|
|
|
|
|
|
} |
41
|
57
|
|
|
57
|
|
3802
|
return try { $self->JSON->encode($var) } |
42
|
57
|
|
|
9
|
|
436
|
catch { throw( "Serializer", $_, { var => $var } ) }; |
|
9
|
|
|
|
|
2140
|
|
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
#=================================== |
46
|
|
|
|
|
|
|
sub encode_bulk { |
47
|
|
|
|
|
|
|
#=================================== |
48
|
1
|
|
|
1
|
1
|
3
|
my ( $self, $var ) = @_; |
49
|
1
|
50
|
|
|
|
4
|
unless ( ref $var ) { |
50
|
0
|
0
|
|
|
|
0
|
return is_utf8($var) |
51
|
|
|
|
|
|
|
? encode_utf8($var) |
52
|
|
|
|
|
|
|
: $var; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
3
|
my $json = ''; |
56
|
1
|
50
|
|
|
|
5
|
throw( "Param", "Var must be an array ref" ) |
57
|
|
|
|
|
|
|
unless ref $var eq 'ARRAY'; |
58
|
|
|
|
|
|
|
return try { |
59
|
1
|
|
|
1
|
|
56
|
for (@$var) { |
60
|
1
|
50
|
|
|
|
20
|
$json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n"; |
61
|
|
|
|
|
|
|
} |
62
|
1
|
|
|
|
|
3
|
return $json; |
63
|
|
|
|
|
|
|
} |
64
|
1
|
|
|
0
|
|
14
|
catch { throw( "Serializer", $_, { var => $var } ) }; |
|
0
|
|
|
|
|
0
|
|
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
#=================================== |
68
|
|
|
|
|
|
|
sub encode_pretty { |
69
|
|
|
|
|
|
|
#=================================== |
70
|
34
|
|
|
34
|
1
|
167
|
my ( $self, $var ) = @_; |
71
|
34
|
|
|
|
|
145
|
$self->JSON->pretty(1); |
72
|
|
|
|
|
|
|
|
73
|
34
|
|
|
|
|
525
|
my $json; |
74
|
|
|
|
|
|
|
try { |
75
|
34
|
|
|
34
|
|
2823
|
$json = $self->encode($var); |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
catch { |
78
|
3
|
|
|
3
|
|
143
|
die "$_"; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
finally { |
81
|
34
|
|
|
34
|
|
850
|
$self->JSON->pretty(0); |
82
|
34
|
|
|
|
|
221
|
}; |
83
|
|
|
|
|
|
|
|
84
|
31
|
|
|
|
|
652
|
return $json; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
#=================================== |
88
|
|
|
|
|
|
|
sub decode { |
89
|
|
|
|
|
|
|
#=================================== |
90
|
254
|
|
|
254
|
1
|
89360
|
my ( $self, $json ) = @_; |
91
|
|
|
|
|
|
|
|
92
|
254
|
100
|
|
|
|
595
|
return unless defined $json; |
93
|
|
|
|
|
|
|
|
94
|
184
|
100
|
|
|
|
1981
|
return is_utf8($json) ? $json : decode_utf8($json) |
|
|
100
|
|
|
|
|
|
95
|
|
|
|
|
|
|
unless substr( $json, 0, 1 ) =~ /^[\[{]/; |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
return try { |
98
|
46
|
|
|
46
|
|
3280
|
$self->JSON->decode($json); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
catch { |
101
|
6
|
|
|
6
|
|
1410
|
throw( "Serializer", $_, { json => $json } ); |
102
|
46
|
|
|
|
|
298
|
}; |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
#=================================== |
106
|
|
|
|
|
|
|
sub _set_canonical { |
107
|
|
|
|
|
|
|
#=================================== |
108
|
0
|
|
|
0
|
|
|
shift()->JSON->canonical(1); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
1; |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=pod |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=encoding UTF-8 |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 NAME |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Search::Elasticsearch::Role::Serializer::JSON - A Serializer role for JSON modules |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 VERSION |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
version 7.717 |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 DESCRIPTION |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
This role encodes Perl data structures into JSON strings, and |
128
|
|
|
|
|
|
|
decodes JSON strings into Perl data structures. |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 METHODS |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 C |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
$bytes = $serializer->encode($ref); |
135
|
|
|
|
|
|
|
$bytes = $serializer->encode($str); |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
The L method converts array and hash refs into their JSON |
138
|
|
|
|
|
|
|
equivalents. If a string is passed in, it is returned as the UTF8 encoded |
139
|
|
|
|
|
|
|
version of itself. The empty string and C are returned as is. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=head2 C |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
$bytes = $serializer->encode_pretty($ref); |
144
|
|
|
|
|
|
|
$bytes = $serializer->encode_pretty($str); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Works exactly as L but the JSON output is pretty-printed. |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 C |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
$bytes = $serializer->encode_bulk([\%hash,\%hash,...]); |
151
|
|
|
|
|
|
|
$bytes = $serializer->encode_bulk([$str,$str,...]); |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
The L method expects an array ref of hashes or strings. |
154
|
|
|
|
|
|
|
Each hash or string is processed by L then joined together |
155
|
|
|
|
|
|
|
by newline characters, with a final newline character appended to the end. |
156
|
|
|
|
|
|
|
This is the special JSON format used for bulk requests. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 C |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
$var = $serializer->decode($json_bytes); |
161
|
|
|
|
|
|
|
$str = $serializer->decode($bytes); |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
If the passed in value looks like JSON (ie starts with a C<{> or C<[> |
164
|
|
|
|
|
|
|
character), then it is decoded from JSON, otherwise it is returned as |
165
|
|
|
|
|
|
|
the UTF8 decoded version of itself. The empty string and C are |
166
|
|
|
|
|
|
|
returned as is. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 AUTHOR |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
Enrico Zimuel |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
This software is Copyright (c) 2022 by Elasticsearch BV. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
This is free software, licensed under: |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
The Apache License, Version 2.0, January 2004 |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=cut |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
__END__ |