File Coverage

blib/lib/Search/Elasticsearch/Role/Serializer/JSON.pm
Criterion Covered Total %
statement 43 46 93.4
branch 13 18 72.2
condition n/a
subroutine 17 19 89.4
pod 4 4 100.0
total 77 87 88.5


line stmt bran cond sub pod time code
1             # Licensed to Elasticsearch B.V. under one or more contributor
2             # license agreements. See the NOTICE file distributed with
3             # this work for additional information regarding copyright
4             # ownership. Elasticsearch B.V. licenses this file to you under
5             # the Apache License, Version 2.0 (the "License"); you may
6             # not use this file except in compliance with the License.
7             # You may obtain a copy of the License at
8             #
9             # http://www.apache.org/licenses/LICENSE-2.0
10             #
11             # Unless required by applicable law or agreed to in writing,
12             # software distributed under the License is distributed on an
13             # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14             # KIND, either express or implied. See the License for the
15             # specific language governing permissions and limitations
16             # under the License.
17              
18             package Search::Elasticsearch::Role::Serializer::JSON;
19             $Search::Elasticsearch::Role::Serializer::JSON::VERSION = '8.00';
20 55     55   246050 use Moo::Role;
  55         143  
  55         324  
21             requires 'JSON';
22              
23 55     55   19927 use Search::Elasticsearch::Util qw(throw);
  55         141  
  55         455  
24 55     55   17622 use Try::Tiny;
  55         124  
  55         3179  
25 55     55   24676 use Encode qw(encode_utf8 decode_utf8 is_utf8);
  55         683586  
  55         6832  
26 55     55   1274 use namespace::clean;
  55         118  
  55         2679  
27              
28             has 'mime_type' => ( is => 'ro', default => 'application/json' );
29              
30             with 'Search::Elasticsearch::Role::Serializer';
31              
32             #===================================
33             sub encode {
34             #===================================
35 117     117 1 8693 my ( $self, $var ) = @_;
36 117 100       361 unless ( ref $var ) {
37 60 100       440 return is_utf8($var)
38             ? encode_utf8($var)
39             : $var;
40             }
41 57     57   4467 return try { $self->JSON->encode($var) }
42 57     9   430 catch { throw( "Serializer", $_, { var => $var } ) };
  9         2409  
43             }
44              
45             #===================================
46             sub encode_bulk {
47             #===================================
48 1     1 1 4 my ( $self, $var ) = @_;
49 1 50       5 unless ( ref $var ) {
50 0 0       0 return is_utf8($var)
51             ? encode_utf8($var)
52             : $var;
53             }
54              
55 1         4 my $json = '';
56 1 50       5 throw( "Param", "Var must be an array ref" )
57             unless ref $var eq 'ARRAY';
58             return try {
59 1     1   51 for (@$var) {
60 1 50       11 $json .= ( ref($_) ? $self->JSON->encode($_) : $_ ) . "\n";
61             }
62 1         4 return $json;
63             }
64 1     0   10 catch { throw( "Serializer", $_, { var => $var } ) };
  0         0  
65             }
66              
67             #===================================
68             sub encode_pretty {
69             #===================================
70 34     34 1 198 my ( $self, $var ) = @_;
71 34         163 $self->JSON->pretty(1);
72              
73 34         700 my $json;
74             try {
75 34     34   3481 $json = $self->encode($var);
76             }
77             catch {
78 3     3   188 die "$_";
79             }
80             finally {
81 34     34   1056 $self->JSON->pretty(0);
82 34         272 };
83              
84 31         855 return $json;
85             }
86              
87             #===================================
88             sub decode {
89             #===================================
90 254     254 1 104680 my ( $self, $json ) = @_;
91              
92 254 100       746 return unless defined $json;
93              
94 184 100       2212 return is_utf8($json) ? $json : decode_utf8($json)
    100          
95             unless substr( $json, 0, 1 ) =~ /^[\[{]/;
96              
97             return try {
98 46     46   3717 $self->JSON->decode($json);
99             }
100             catch {
101 6     6   1448 throw( "Serializer", $_, { json => $json } );
102 46         338 };
103             }
104              
105             #===================================
106             sub _set_canonical {
107             #===================================
108 0     0     shift()->JSON->canonical(1);
109             }
110              
111             1;
112              
113             =pod
114              
115             =encoding UTF-8
116              
117             =head1 NAME
118              
119             Search::Elasticsearch::Role::Serializer::JSON - A Serializer role for JSON modules
120              
121             =head1 VERSION
122              
123             version 8.00
124              
125             =head1 DESCRIPTION
126              
127             This role encodes Perl data structures into JSON strings, and
128             decodes JSON strings into Perl data structures.
129              
130             =head1 METHODS
131              
132             =head2 C
133              
134             $bytes = $serializer->encode($ref);
135             $bytes = $serializer->encode($str);
136              
137             The L method converts array and hash refs into their JSON
138             equivalents. If a string is passed in, it is returned as the UTF8 encoded
139             version of itself. The empty string and C are returned as is.
140              
141             =head2 C
142              
143             $bytes = $serializer->encode_pretty($ref);
144             $bytes = $serializer->encode_pretty($str);
145              
146             Works exactly as L but the JSON output is pretty-printed.
147              
148             =head2 C
149              
150             $bytes = $serializer->encode_bulk([\%hash,\%hash,...]);
151             $bytes = $serializer->encode_bulk([$str,$str,...]);
152              
153             The L method expects an array ref of hashes or strings.
154             Each hash or string is processed by L then joined together
155             by newline characters, with a final newline character appended to the end.
156             This is the special JSON format used for bulk requests.
157              
158             =head2 C
159              
160             $var = $serializer->decode($json_bytes);
161             $str = $serializer->decode($bytes);
162              
163             If the passed in value looks like JSON (ie starts with a C<{> or C<[>
164             character), then it is decoded from JSON, otherwise it is returned as
165             the UTF8 decoded version of itself. The empty string and C are
166             returned as is.
167              
168             =head1 AUTHOR
169              
170             Enrico Zimuel
171              
172             =head1 COPYRIGHT AND LICENSE
173              
174             This software is Copyright (c) 2022 by Elasticsearch BV.
175              
176             This is free software, licensed under:
177              
178             The Apache License, Version 2.0, January 2004
179              
180             =cut
181              
182             __END__