Elasticsearch/Role/Serializer/JSON.pm

filter: Statementstmt 94.2% 49 / 52 Branchbran 72.2% 13 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 89.4% 17 / 19 Podpod 100% 4 / 4 totaltotal 89.2% 83 / 93 CC 11 SCAR 8.8
CC
11
Cov
89%
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 agreements.
2 # Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
3 # See the LICENSE file in the project root for more information
4  
5 package Search::Elasticsearch::Role::Serializer::JSON;
6 55 $Search::Elasticsearch::Role::Serializer::JSON::VERSION = '7.30_1'; # TRIAL
7  
8 55 $Search::Elasticsearch::Role::Serializer::JSON::VERSION = '7.301';
9 55 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 55 requires 'JSON';
11  
12 55 ▶use Search::Elasticsearch::Util qw(throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 55 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 55 ▶use Encode qw(encode_utf8 decode_utf8 is_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 55 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 55 has 'mime_type' => ( is => 'ro', default => 'application/json' );
18  
19 55 with 'Search::Elasticsearch::Role::Serializer';
20  
21 #===================================
22 sub encode {
23 #===================================
24 117 ▶ my ( $self, $var ) = @_;
Subroutine encode: called CC=3 SCAR=11.0
Pod documented
25 117 ▶ unless ( ref $var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $var) 60 57 117
26 60 ▶ return is_utf8($var)
Branch: 1 branch
BranchTrueFalseTotal
&is_utf8($var) ? : 9 51 60
27 ? encode_utf8($var)
28 : $var;
29 }
30 57 ▶ return try { $self->JSON->encode($var) }
Subroutine __ANON__: called CC=1 SCAR=0.0
31 57 ▶ catch { throw( "Serializer", $_, { var => $var } ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
32 }
33  
34 #===================================
35 sub encode_bulk {
36 #===================================
37 1 ▶ my ( $self, $var ) = @_;
Subroutine encode_bulk: called CC=4 SCAR=16.0
Pod documented
38 1 ▶ unless ( ref $var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $var) 0 1 1
39 0 ▶ return is_utf8($var)
Branch: 1 branch
BranchTrueFalseTotal
&is_utf8($var) ? : 0 0 0
40 ? encode_utf8($var)
41 : $var;
42 }
43  
44 1 my $json = '';
45 1 ▶ throw( "Param", "Var must be an array ref" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $var eq "ARRAY" 0 1 1
46 unless ref $var eq 'ARRAY';
47 return try {
48 1 ▶ for (@$var) {
Subroutine __ANON__: called CC=3 SCAR=11.2
49 1 ▶ $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1 0 1
50 }
51 1 return $json;
52 }
53 1 ▶ catch { throw( "Serializer", $_, { var => $var } ) };
Subroutine __ANON__: not called CC=1 SCAR=1.2
54 }
55  
56 #===================================
57 sub encode_pretty {
58 #===================================
59 34 ▶ my ( $self, $var ) = @_;
Subroutine encode_pretty: called CC=1 SCAR=0.0
Pod documented
60 34 $self->JSON->pretty(1);
61  
62 34 my $json;
63 try {
64 34 ▶ $json = $self->encode($var);
Subroutine __ANON__: called CC=1 SCAR=0.0
65 }
66 catch {
67 3 ▶ die "$_";
Subroutine __ANON__: called CC=1 SCAR=0.0
68 }
69 finally {
70 34 ▶ $self->JSON->pretty(0);
Subroutine __ANON__: called CC=1 SCAR=0.0
71 34 };
72  
73 31 return $json;
74 }
75  
76 #===================================
77 sub decode {
78 #===================================
79 254 ▶ my ( $self, $json ) = @_;
Subroutine decode: called CC=4 SCAR=13.9
Pod documented
80  
81 254 ▶ return unless defined $json;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json 70 184 254
82  
83 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
84 unless substr( $json, 0, 1 ) =~ /^[\[{]/;
85  
86 return try {
87 46 ▶ $self->JSON->decode($json);
Subroutine __ANON__: called CC=1 SCAR=0.0
88 }
89 catch {
90 6 ▶ throw( "Serializer", $_, { json => $json } );
Subroutine __ANON__: called CC=1 SCAR=0.0
91 46 };
92 }
93  
94 #===================================
95 sub _set_canonical {
96 #===================================
97 0 ▶ shift()->JSON->canonical(1);
Subroutine _set_canonical: not called CC=1 SCAR=6.9
98 }
99  
100 55 1;
101  
102 =pod
103  
104 =encoding UTF-8
105  
106 =head1 NAME
107  
108 Search::Elasticsearch::Role::Serializer::JSON - A Serializer role for JSON modules
109  
110 =head1 VERSION
111  
112 version 7.30_1
113  
114 =head1 DESCRIPTION
115  
116 This role encodes Perl data structures into JSON strings, and
117 decodes JSON strings into Perl data structures.
118  
119 =head1 METHODS
120  
121 =head2 C<encode()>
122  
123 $bytes = $serializer->encode($ref);
124 $bytes = $serializer->encode($str);
125  
126 The L</encode()> method converts array and hash refs into their JSON
127 equivalents. If a string is passed in, it is returned as the UTF8 encoded
128 version of itself. The empty string and C<undef> are returned as is.
129  
130 =head2 C<encode_pretty()>
131  
132 $bytes = $serializer->encode_pretty($ref);
133 $bytes = $serializer->encode_pretty($str);
134  
135 Works exactly as L</encode()> but the JSON output is pretty-printed.
136  
137 =head2 C<encode_bulk()>
138  
139 $bytes = $serializer->encode_bulk([\%hash,\%hash,...]);
140 $bytes = $serializer->encode_bulk([$str,$str,...]);
141  
142 The L</encode_bulk()> method expects an array ref of hashes or strings.
143 Each hash or string is processed by L</encode()> then joined together
144 by newline characters, with a final newline character appended to the end.
145 This is the special JSON format used for bulk requests.
146  
147 =head2 C<decode()>
148  
149 $var = $serializer->decode($json_bytes);
150 $str = $serializer->decode($bytes);
151  
152 If the passed in value looks like JSON (ie starts with a C<{> or C<[>
153 character), then it is decoded from JSON, otherwise it is returned as
154 the UTF8 decoded version of itself. The empty string and C<undef> are
155 returned as is.
156  
157 =head1 AUTHOR
158  
159 Enrico Zimuel <enrico.zimuel@elastic.co>
160  
161 =head1 COPYRIGHT AND LICENSE
162  
163 This software is Copyright (c) 2020 by Elasticsearch BV.
164  
165 This is free software, licensed under:
166  
167 The Apache License, Version 2.0, January 2004
168  
169 =cut
170  
171 __END__
« Elasticsearch/Role/CxnPool.pm ↑ Summary Elasticsearch/Role/Logger.pm »