line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Elasticsearch::Serializer::JSON; |
2
|
|
|
|
|
|
|
$Elasticsearch::Serializer::JSON::VERSION = '1.05'; |
3
|
42
|
|
|
42
|
|
32361
|
use Moo; |
|
42
|
|
|
|
|
94
|
|
|
42
|
|
|
|
|
430
|
|
4
|
|
|
|
|
|
|
|
5
|
42
|
|
|
42
|
|
16265
|
use Elasticsearch::Util qw(throw); |
|
42
|
|
|
|
|
87
|
|
|
42
|
|
|
|
|
572
|
|
6
|
42
|
|
|
42
|
|
13877
|
use JSON(); |
|
42
|
|
|
|
|
89
|
|
|
42
|
|
|
|
|
770
|
|
7
|
42
|
|
|
42
|
|
263
|
use Try::Tiny; |
|
42
|
|
|
|
|
80
|
|
|
42
|
|
|
|
|
3159
|
|
8
|
42
|
|
|
42
|
|
63875
|
use Encode qw(encode_utf8 decode_utf8 is_utf8); |
|
42
|
|
|
|
|
540231
|
|
|
42
|
|
|
|
|
12240
|
|
9
|
42
|
|
|
42
|
|
813
|
use namespace::clean; |
|
42
|
|
|
|
|
99
|
|
|
42
|
|
|
|
|
475
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $JSON = JSON->new->utf8; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has 'mime_type' => ( is => 'ro', default => 'application/json' ); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
with 'Elasticsearch::Role::Serializer'; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
#=================================== |
18
|
|
|
|
|
|
|
sub encode { |
19
|
|
|
|
|
|
|
#=================================== |
20
|
54
|
|
|
54
|
1
|
2159
|
my ( $self, $var ) = @_; |
21
|
54
|
100
|
|
|
|
211
|
unless ( ref $var ) { |
22
|
18
|
100
|
|
|
|
180
|
return is_utf8($var) |
23
|
|
|
|
|
|
|
? encode_utf8($var) |
24
|
|
|
|
|
|
|
: $var; |
25
|
|
|
|
|
|
|
} |
26
|
36
|
|
|
36
|
|
2893
|
return try { $JSON->encode($var) } |
27
|
36
|
|
|
2
|
|
377
|
catch { throw( "Serializer", $_, { var => $var } ) }; |
|
2
|
|
|
|
|
41
|
|
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
#=================================== |
31
|
|
|
|
|
|
|
sub encode_bulk { |
32
|
|
|
|
|
|
|
#=================================== |
33
|
12
|
|
|
12
|
1
|
3244
|
my ( $self, $var ) = @_; |
34
|
12
|
100
|
|
|
|
41
|
unless ( ref $var ) { |
35
|
6
|
100
|
|
|
|
55
|
return is_utf8($var) |
36
|
|
|
|
|
|
|
? encode_utf8($var) |
37
|
|
|
|
|
|
|
: $var; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
6
|
|
|
|
|
13
|
my $json = ''; |
41
|
6
|
100
|
|
|
|
32
|
throw( "Param", "Var must be an array ref" ) |
42
|
|
|
|
|
|
|
unless ref $var eq 'ARRAY'; |
43
|
|
|
|
|
|
|
return try { |
44
|
4
|
|
|
4
|
|
265
|
for (@$var) { |
45
|
6
|
100
|
|
|
|
106
|
$json .= ( ref($_) ? $JSON->encode($_) : $_ ) . "\n"; |
46
|
|
|
|
|
|
|
} |
47
|
3
|
|
|
|
|
12
|
return $json; |
48
|
|
|
|
|
|
|
} |
49
|
4
|
|
|
1
|
|
48
|
catch { throw( "Serializer", $_, { var => $var } ) }; |
|
1
|
|
|
|
|
15
|
|
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
#=================================== |
53
|
|
|
|
|
|
|
sub encode_pretty { |
54
|
|
|
|
|
|
|
#=================================== |
55
|
16
|
|
|
16
|
1
|
80
|
my ( $self, $var ) = @_; |
56
|
16
|
|
|
|
|
137
|
$JSON->pretty(1); |
57
|
|
|
|
|
|
|
|
58
|
16
|
|
|
|
|
21
|
my $json; |
59
|
|
|
|
|
|
|
try { |
60
|
16
|
|
|
16
|
|
1886
|
$json = $self->encode($var); |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
catch { |
63
|
1
|
|
|
1
|
|
167
|
die "$_"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
finally { |
66
|
16
|
|
|
16
|
|
495
|
$JSON->pretty(0); |
67
|
16
|
|
|
|
|
212
|
}; |
68
|
|
|
|
|
|
|
|
69
|
15
|
|
|
|
|
282
|
return $json; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
#=================================== |
73
|
|
|
|
|
|
|
sub decode { |
74
|
|
|
|
|
|
|
#=================================== |
75
|
211
|
|
|
211
|
1
|
30457
|
my $json = $_[1]; |
76
|
|
|
|
|
|
|
|
77
|
211
|
100
|
|
|
|
887
|
return unless defined $json; |
78
|
|
|
|
|
|
|
|
79
|
151
|
100
|
|
|
|
1522
|
return is_utf8($json) ? $json : decode_utf8($json) |
|
|
100
|
|
|
|
|
|
80
|
|
|
|
|
|
|
unless substr( $json, 0, 1 ) =~ /^[\[{]/; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
return try { |
83
|
33
|
|
|
33
|
|
2538
|
$JSON->decode($json); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
catch { |
86
|
1
|
|
|
1
|
|
18
|
throw( "Serializer", $_, { json => $json } ); |
87
|
33
|
|
|
|
|
291
|
}; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
#=================================== |
91
|
|
|
|
|
|
|
sub _set_canonical { |
92
|
|
|
|
|
|
|
#=================================== |
93
|
0
|
|
|
0
|
|
|
$JSON->canonical(1); |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
1; |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=pod |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=encoding UTF-8 |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 NAME |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
Elasticsearch::Serializer::JSON - A Serializer for JSON |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 VERSION |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
version 1.05 |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 DESCRIPTION |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
This module uses L<JSON> to Perl data structures into JSON strings, and |
113
|
|
|
|
|
|
|
to decode JSON strings into Perl data structures. |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 METHODS |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head2 C<encode()> |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
$bytes = $serializer->encode($ref); |
120
|
|
|
|
|
|
|
$bytes = $serializer->encode($str); |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
The L</encode()> method converts array and hash refs into their JSON |
123
|
|
|
|
|
|
|
equivalents. If a string is passed in, it is returned as the UTF8 encoded |
124
|
|
|
|
|
|
|
version of itself. The empty string and C<undef> are returned as is. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head2 C<encode_pretty()> |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
$bytes = $serializer->encode_pretty($ref); |
129
|
|
|
|
|
|
|
$bytes = $serializer->encode_pretty($str); |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Works exactly as L</encode()> but the JSON output is pretty-printed. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head2 C<encode_bulk()> |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
$bytes = $serializer->encode_bulk([\%hash,\%hash,...]); |
136
|
|
|
|
|
|
|
$bytes = $serializer->encode_bulk([$str,$str,...]); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
The L</encode_bulk()> method expects an array ref of hashes or strings. |
139
|
|
|
|
|
|
|
Each hash or string is processed by L</encode()> then joined together |
140
|
|
|
|
|
|
|
by newline characters, with a final newline character appended to the end. |
141
|
|
|
|
|
|
|
This is the special JSON format used for bulk requests. |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head2 C<decode()> |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
$var = $serializer->decode($json_bytes); |
146
|
|
|
|
|
|
|
$str = $serializer->decode($bytes); |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
If the passed in value looks like JSON (ie starts with a C<{> or C<[> |
149
|
|
|
|
|
|
|
character), then it is decoded from JSON, otherwise it is returned as |
150
|
|
|
|
|
|
|
the UTF8 decoded version of itself. The empty string and C<undef> are |
151
|
|
|
|
|
|
|
returned as is. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head1 AUTHOR |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Clinton Gormley <drtech@cpan.org> |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This software is Copyright (c) 2014 by Elasticsearch BV. |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This is free software, licensed under: |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
The Apache License, Version 2.0, January 2004 |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=cut |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
__END__ |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
# ABSTRACT: A Serializer for JSON |
170
|
|
|
|
|
|
|
|