File Coverage

blib/lib/OpenSearch/Client/Role/Serializer/JSON.pm
Criterion Covered Total %
statement 45 46 97.8
branch 18 18 100.0
condition n/a
subroutine 18 19 94.7
pod 4 4 100.0
total 85 87 97.7


line stmt bran cond sub pod time code
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             $OpenSearch::Client::Role::Serializer::JSON::VERSION = '3.071';
26 56     56   245240 use Moo::Role;
  56         192  
  56         369  
27             requires 'JSON';
28              
29 56     56   26428 use OpenSearch::Client::Util qw(throw);
  56         131  
  56         501  
30 56     56   15649 use Try::Tiny;
  56         130  
  56         4055  
31 56     56   271 use Encode qw(encode_utf8 decode_utf8 is_utf8);
  56         113  
  56         4356  
32 56     56   247 use namespace::clean;
  56         90  
  56         406  
33              
34             has 'mime_type' => ( is => 'ro', default => 'application/json' );
35              
36             with 'OpenSearch::Client::Role::Serializer';
37              
38             #===================================
39             sub encode {
40             #===================================
41 90     90 1 4140 my ( $self, $var ) = @_;
42 90 100       267 unless ( ref $var ) {
43 42 100       281 return is_utf8($var)
44             ? encode_utf8($var)
45             : $var;
46             }
47 48     48   3056 return try { $self->JSON->encode($var) }
48 48     6   348 catch { throw( "Serializer", $_, { var => $var } ) };
  6         1470  
49             }
50              
51             #===================================
52             sub encode_bulk {
53             #===================================
54 34     34 1 5406 my ( $self, $var ) = @_;
55 34 100       98 unless ( ref $var ) {
56 18 100       102 return is_utf8($var)
57             ? encode_utf8($var)
58             : $var;
59             }
60              
61 16         27 my $json = '';
62 16 100       61 throw( "Param", "Var must be an array ref" )
63             unless ref $var eq 'ARRAY';
64             return try {
65 10     10   607 for (@$var) {
66 16 100       214 $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n";
67             }
68 7         65 return $json;
69             }
70 10     3   77 catch { throw( "Serializer", $_, { var => $var } ) };
  3         629  
71             }
72              
73             #===================================
74             sub encode_pretty {
75             #===================================
76 43     43 1 256 my ( $self, $var ) = @_;
77 43         221 $self->JSON->pretty(1);
78              
79 43         526 my $json;
80             try {
81 43     43   4128 $json = $self->encode($var);
82             }
83             catch {
84 4     4   179 die "$_";
85             }
86             finally {
87 43     43   1130 $self->JSON->pretty(0);
88 43         425 };
89              
90 39         838 return $json;
91             }
92              
93             #===================================
94             sub decode {
95             #===================================
96 218     218 1 36455 my ( $self, $json ) = @_;
97              
98 218 100       607 return unless defined $json;
99              
100 156 100       1803 return is_utf8($json) ? $json : decode_utf8($json)
    100          
101             unless substr( $json, 0, 1 ) =~ /^[\[{]/;
102              
103             return try {
104 34     34   2312 $self->JSON->decode($json);
105             }
106             catch {
107 2     2   637 throw( "Serializer", $_, { json => $json } );
108 34         229 };
109             }
110              
111             #===================================
112             sub _set_canonical {
113             #===================================
114 0     0     shift()->JSON->canonical(1);
115             }
116              
117             1;
118              
119             __END__