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             $OpenSearch::Client::Role::Serializer::JSON::VERSION = '3.070';
25 55     55   234231 use Moo::Role;
  55         101  
  55         351  
26             requires 'JSON';
27              
28 55     55   25051 use OpenSearch::Client::Util qw(throw);
  55         277  
  55         501  
29 55     55   15839 use Try::Tiny;
  55         137  
  55         3964  
30 55     55   263 use Encode qw(encode_utf8 decode_utf8 is_utf8);
  55         129  
  55         4510  
31 55     55   302 use namespace::clean;
  55         110  
  55         402  
32              
33             has 'mime_type' => ( is => 'ro', default => 'application/json' );
34              
35             with 'OpenSearch::Client::Role::Serializer';
36              
37             #===================================
38             sub encode {
39             #===================================
40 90     90 1 3426 my ( $self, $var ) = @_;
41 90 100       224 unless ( ref $var ) {
42 42 100       225 return is_utf8($var)
43             ? encode_utf8($var)
44             : $var;
45             }
46 48     48   2704 return try { $self->JSON->encode($var) }
47 48     6   308 catch { throw( "Serializer", $_, { var => $var } ) };
  6         1221  
48             }
49              
50             #===================================
51             sub encode_bulk {
52             #===================================
53 34     34 1 5552 my ( $self, $var ) = @_;
54 34 100       97 unless ( ref $var ) {
55 18 100       114 return is_utf8($var)
56             ? encode_utf8($var)
57             : $var;
58             }
59              
60 16         24 my $json = '';
61 16 100       64 throw( "Param", "Var must be an array ref" )
62             unless ref $var eq 'ARRAY';
63             return try {
64 10     10   594 for (@$var) {
65 16 100       260 $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n";
66             }
67 7         69 return $json;
68             }
69 10     3   79 catch { throw( "Serializer", $_, { var => $var } ) };
  3         590  
70             }
71              
72             #===================================
73             sub encode_pretty {
74             #===================================
75 43     43 1 223 my ( $self, $var ) = @_;
76 43         206 $self->JSON->pretty(1);
77              
78 43         531 my $json;
79             try {
80 43     43   3379 $json = $self->encode($var);
81             }
82             catch {
83 4     4   182 die "$_";
84             }
85             finally {
86 43     43   1143 $self->JSON->pretty(0);
87 43         381 };
88              
89 39         816 return $json;
90             }
91              
92             #===================================
93             sub decode {
94             #===================================
95 218     218 1 36017 my ( $self, $json ) = @_;
96              
97 218 100       574 return unless defined $json;
98              
99 156 100       1585 return is_utf8($json) ? $json : decode_utf8($json)
    100          
100             unless substr( $json, 0, 1 ) =~ /^[\[{]/;
101              
102             return try {
103 34     34   2279 $self->JSON->decode($json);
104             }
105             catch {
106 2     2   621 throw( "Serializer", $_, { json => $json } );
107 34         413 };
108             }
109              
110             #===================================
111             sub _set_canonical {
112             #===================================
113 0     0     shift()->JSON->canonical(1);
114             }
115              
116             1;
117              
118             __END__