Client/Role/Serializer/JSON.pm

filter: Statementstmt 98.0% 50 / 51 Branchbran 100% 18 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.7% 18 / 19 Podpod 100% 4 / 4 totaltotal 97.8% 90 / 92 CC 11 SCAR 8.2
CC
11
Cov
99%
encode_bulk
4.0
decode
4.0
encode
3.0
CRAP 2.3
« Client/Role/Logger.pm ↑ Summary Client/Core/3_0.pm »
1 # OpenSearch::Client is an unofficial client for OpenSearch.
2 # It is derived from Search::Elasticsearch version 7.714
3 # License details from the original work are contained in the
4 # NOTICE file distributed with this work.
5 #
6 #-----------------------------------------------------------------------
7 # OpenSearch::Client
8 #-----------------------------------------------------------------------
9 # Copyright 2026 Mark Dootson
10 #
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14 #
15 # http://www.apache.org/licenses/LICENSE-2.0
16 #
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22  
23 package OpenSearch::Client::Role::Serializer::JSON;
24 ## Historical versioning
25 56 $OpenSearch::Client::Role::Serializer::JSON::VERSION = '3.071';
26 56 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 56 requires 'JSON';
28  
29 56 ▶use OpenSearch::Client::Util qw(throw);
Subroutine BEGIN: called CC=1 SCAR=0.0
30 56 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 56 ▶use Encode qw(encode_utf8 decode_utf8 is_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
32 56 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 56 has 'mime_type' => ( is => 'ro', default => 'application/json' );
35  
36 56 with 'OpenSearch::Client::Role::Serializer';
37  
38 #===================================
39 90 ▶sub encode {
Subroutine encode: called CC=3 SCAR=11.0
Pod documented
40 #===================================
41 90 my ( $self, $var ) = @_;
42 90 ▶ unless ( ref $var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $var) 42 48 90
43 42 ▶ return is_utf8($var)
Branch: 1 branch
BranchTrueFalseTotal
&is_utf8($var) ? : 6 36 42
44 ? encode_utf8($var)
45 : $var;
46 }
47 48 ▶ return try { $self->JSON->encode($var) }
Subroutine __ANON__: called CC=1 SCAR=0.0
48 48 ▶ catch { throw( "Serializer", $_, { var => $var } ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
49 }
50  
51 #===================================
52 34 ▶sub encode_bulk {
Subroutine encode_bulk: called CC=4 SCAR=13.9
Pod documented
53 #===================================
54 34 my ( $self, $var ) = @_;
55 34 ▶ unless ( ref $var ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $var) 18 16 34
56 18 ▶ return is_utf8($var)
Branch: 1 branch
BranchTrueFalseTotal
&is_utf8($var) ? : 3 15 18
57 ? encode_utf8($var)
58 : $var;
59 }
60  
61 16 my $json = '';
62 16 ▶ throw( "Param", "Var must be an array ref" )
Branch: 1 branch
BranchTrueFalseTotal
unless ref $var eq "ARRAY" 6 10 16
63 unless ref $var eq 'ARRAY';
64 10 ▶ return try {
Subroutine __ANON__: called CC=3 SCAR=11.0
65 10 for (@$var) {
66 16 ▶ $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 10 6 16
67 }
68 7 return $json;
69 }
70 10 ▶ catch { throw( "Serializer", $_, { var => $var } ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
71 }
72  
73 #===================================
74 43 ▶sub encode_pretty {
Subroutine encode_pretty: called CC=1 SCAR=0.0
Pod documented
75 #===================================
76 43 my ( $self, $var ) = @_;
77 43 $self->JSON->pretty(1);
78  
79 43 my $json;
80 43 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
81 43 $json = $self->encode($var);
82 }
83 4 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
84 4 die "$_";
85 }
86 43 ▶ finally {
Subroutine __ANON__: called CC=1 SCAR=0.0
87 43 $self->JSON->pretty(0);
88 43 };
89  
90 39 return $json;
91 }
92  
93 #===================================
94 218 ▶sub decode {
Subroutine decode: called CC=4 SCAR=13.9
Pod documented
95 #===================================
96 218 my ( $self, $json ) = @_;
97  
98 218 ▶ return unless defined $json;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $json 62 156 218
99  
100 156 ▶ return is_utf8($json) ? $json : decode_utf8($json)
Branch: 2 branches
BranchTrueFalseTotal
unless substr($json, 0, 1) =~ /^[\[{]/ 122 34 156
&is_utf8($json) ? : 2 120 122
101 unless substr( $json, 0, 1 ) =~ /^[\[{]/;
102  
103 34 ▶ return try {
Subroutine __ANON__: called CC=1 SCAR=0.0
104 34 $self->JSON->decode($json);
105 }
106 2 ▶ catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
107 2 throw( "Serializer", $_, { json => $json } );
108 34 };
109 }
110  
111 #===================================
112 0 ▶sub _set_canonical {
Subroutine _set_canonical: not called CC=1 SCAR=6.9
113 #===================================
114 0 shift()->JSON->canonical(1);
115 }
116  
117 56 1;
118  
119 __END__
« Client/Role/Logger.pm ↑ Summary Client/Core/3_0.pm »