Elasticsearch/Role/Serializer/JSON.pm

filter: Statementstmt 94.1% 48 / 51 Branchbran 72.2% 13 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 89.4% 17 / 19 Podpod 100% 4 / 4 totaltotal 89.1% 82 / 92 CC 11 SCAR 8.8
CC
11
Cov
88%
encode_bulk
4.9
decode
4.0
__ANON__
3.1
CRAP 2.4
« Elasticsearch/Role/CxnPool.pm ↑ Summary Elasticsearch/Role/Logger.pm »
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 55 $Search::Elasticsearch::Role::Serializer::JSON::VERSION = '8.12';
20 55 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 55 requires 'JSON';
22  
23 55 ▶use Search::Elasticsearch::Util qw(throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 55 ▶use Encode qw(encode_utf8 decode_utf8 is_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 55 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 55 has 'mime_type' => ( is => 'ro', default => 'application/json' );
29  
30 55 with 'Search::Elasticsearch::Role::Serializer';
31  
32 #===================================
33 117 ▶sub encode {
Subroutine encode: called CC=3 SCAR=11.0
Pod documented
34 #===================================
35 117 my ( $self, $var ) = @_;
36 117 ▶ unless ( ref $var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $var) 60 57 117
37 60 ▶ return is_utf8($var)
Branch: 1 branch
BranchTrueFalseTotal
&is_utf8($var) ? : 9 51 60
38 ? encode_utf8($var)
39 : $var;
40 }
41 57 ▶ return try { $self->JSON->encode($var) }
Subroutine __ANON__: called CC=1 SCAR=0.0
42 57 ▶ catch { throw( "Serializer", $_, { var => $var } ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
43 }
44  
45 #===================================
46 1 ▶sub encode_bulk {
Subroutine encode_bulk: called CC=4 SCAR=16.0
Pod documented
47 #===================================
48 1 my ( $self, $var ) = @_;
49 1 ▶ unless ( ref $var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $var) 0 1 1
50 0 ▶ return is_utf8($var)
Branch: 1 branch
BranchTrueFalseTotal
&is_utf8($var) ? : 0 0 0
51 ? encode_utf8($var)
52 : $var;
53 }
54  
55 1 my $json = '';
56 1 ▶ throw( "Param", "Var must be an array ref" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $var eq "ARRAY" 0 1 1
57 unless ref $var eq 'ARRAY';
58 1 ▶ return try {
Subroutine __ANON__: called CC=3 SCAR=11.2
59 1 for (@$var) {
60 1 ▶ $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 0 1
61 }
62 1 return $json;
63 }
64 0 ▶ catch { throw( "Serializer", $_, { var => $var } ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
65 }
66  
67 #===================================
68 34 ▶sub encode_pretty {
Subroutine encode_pretty: called CC=1 SCAR=0.0
Pod documented
69 #===================================
70 34 my ( $self, $var ) = @_;
71 34 $self->JSON->pretty(1);
72  
73 34 my $json;
74 34 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
75 34 $json = $self->encode($var);
76 }
77 3 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
78 3 die "$_";
79 }
80 34 ▶ finally {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 34 $self->JSON->pretty(0);
82 34 };
83  
84 31 return $json;
85 }
86  
87 #===================================
88 254 ▶sub decode {
Subroutine decode: called CC=4 SCAR=13.9
Pod documented
89 #===================================
90 254 my ( $self, $json ) = @_;
91  
92 254 ▶ return unless defined $json;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json 70 184 254
93  
94 184 ▶ return is_utf8($json) ? $json : decode_utf8($json)
Branch: 2 branches
BranchTrueFalseTotal
unless substr($json, 0, 1) =~ /^[\[{]/ 138 46 184
&is_utf8($json) ? : 6 132 138
95 unless substr( $json, 0, 1 ) =~ /^[\[{]/;
96  
97 46 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
98 46 $self->JSON->decode($json);
99 }
100 6 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
101 6 throw( "Serializer", $_, { json => $json } );
102 46 };
103 }
104  
105 #===================================
106 0 ▶sub _set_canonical {
Subroutine _set_canonical: not called CC=1 SCAR=6.9
107 #===================================
108 0 shift()->JSON->canonical(1);
109 }
110  
111 55 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 8.12
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<encode()>
133  
134 $bytes = $serializer->encode($ref);
135 $bytes = $serializer->encode($str);
136  
137 The L</encode()> 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<undef> are returned as is.
140  
141 =head2 C<encode_pretty()>
142  
143 $bytes = $serializer->encode_pretty($ref);
144 $bytes = $serializer->encode_pretty($str);
145  
146 Works exactly as L</encode()> but the JSON output is pretty-printed.
147  
148 =head2 C<encode_bulk()>
149  
150 $bytes = $serializer->encode_bulk([\%hash,\%hash,...]);
151 $bytes = $serializer->encode_bulk([$str,$str,...]);
152  
153 The L</encode_bulk()> method expects an array ref of hashes or strings.
154 Each hash or string is processed by L</encode()> 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<decode()>
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<undef> are
166 returned as is.
167  
168 =head1 AUTHOR
169  
170 Enrico Zimuel <enrico.zimuel@elastic.co>
171  
172 =head1 COPYRIGHT AND LICENSE
173  
174 This software is Copyright (c) 2024 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__
« Elasticsearch/Role/CxnPool.pm ↑ Summary Elasticsearch/Role/Logger.pm »