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   251151 use Moo::Role;
  56         253  
  56         409  
27             requires 'JSON';
28              
29 56     56   26432 use OpenSearch::Client::Util qw(throw);
  56         136  
  56         455  
30 56     56   16115 use Try::Tiny;
  56         91  
  56         3918  
31 56     56   298 use Encode qw(encode_utf8 decode_utf8 is_utf8);
  56         104  
  56         4459  
32 56     56   318 use namespace::clean;
  56         86  
  56         397  
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 3375 my ( $self, $var ) = @_;
42 90 100       317 unless ( ref $var ) {
43 42 100       226 return is_utf8($var)
44             ? encode_utf8($var)
45             : $var;
46             }
47 48     48   2751 return try { $self->JSON->encode($var) }
48 48     6   288 catch { throw( "Serializer", $_, { var => $var } ) };
  6         1287  
49             }
50              
51             #===================================
52             sub encode_bulk {
53             #===================================
54 34     34 1 5429 my ( $self, $var ) = @_;
55 34 100       107 unless ( ref $var ) {
56 18 100       114 return is_utf8($var)
57             ? encode_utf8($var)
58             : $var;
59             }
60              
61 16         24 my $json = '';
62 16 100       190 throw( "Param", "Var must be an array ref" )
63             unless ref $var eq 'ARRAY';
64             return try {
65 10     10   618 for (@$var) {
66 16 100       202 $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n";
67             }
68 7         89 return $json;
69             }
70 10     3   77 catch { throw( "Serializer", $_, { var => $var } ) };
  3         615  
71             }
72              
73             #===================================
74             sub encode_pretty {
75             #===================================
76 43     43 1 246 my ( $self, $var ) = @_;
77 43         218 $self->JSON->pretty(1);
78              
79 43         523 my $json;
80             try {
81 43     43   3688 $json = $self->encode($var);
82             }
83             catch {
84 4     4   179 die "$_";
85             }
86             finally {
87 43     43   989 $self->JSON->pretty(0);
88 43         376 };
89              
90 39         786 return $json;
91             }
92              
93             #===================================
94             sub decode {
95             #===================================
96 218     218 1 34529 my ( $self, $json ) = @_;
97              
98 218 100       620 return unless defined $json;
99              
100 156 100       2025 return is_utf8($json) ? $json : decode_utf8($json)
    100          
101             unless substr( $json, 0, 1 ) =~ /^[\[{]/;
102              
103             return try {
104 34     34   2034 $self->JSON->decode($json);
105             }
106             catch {
107 2     2   613 throw( "Serializer", $_, { json => $json } );
108 34         294 };
109             }
110              
111             #===================================
112             sub _set_canonical {
113             #===================================
114 0     0     shift()->JSON->canonical(1);
115             }
116              
117             1;
118              
119             __END__