Debian Codegraph @

lib/Test2/Harness/Util/JSON.pm

raw
1 package Test2::Harness::Util::JSON;
2 use strict;
3 use warnings;
4
5 use Carp qw/croak/;
6
7 our $VERSION = '1.000179';
8
9 BEGIN {
10 local $@ = undef;
11 my $ok = eval {
12 require JSON::MaybeXS;
13 JSON::MaybeXS->import('JSON');
14 1;
15
16 if (JSON() eq 'JSON::PP') {
17 *JSON_IS_PP = sub() { 1 };
18 *JSON_IS_XS = sub() { 0 };
19 *JSON_IS_CPANEL = sub() { 0 };
20 *JSON_IS_CPANEL_OR_XS = sub() { 0 };
21 }
22 elsif (JSON() eq 'JSON::XS') {
23 *JSON_IS_PP = sub() { 0 };
24 *JSON_IS_XS = sub() { 1 };
25 *JSON_IS_CPANEL = sub() { 0 };
26 *JSON_IS_CPANEL_OR_XS = sub() { 1 };
27 }
28 elsif (JSON() eq 'Cpanel::JSON::XS') {
29 *JSON_IS_PP = sub() { 0 };
30 *JSON_IS_XS = sub() { 0 };
31 *JSON_IS_CPANEL = sub() { 1 };
32 *JSON_IS_CPANEL_OR_XS = sub() { 1 };
33 }
34 };
35
36 unless ($ok) {
37 require JSON::PP;
38 *JSON = sub() { 'JSON::PP' };
39
40 *JSON_IS_PP = sub() { 1 };
41 *JSON_IS_XS = sub() { 0 };
42 *JSON_IS_CPANEL = sub() { 0 };
43 *JSON_IS_CPANEL_OR_XS = sub() { 0 };
44 }
45
46 }
47
48 our @EXPORT = qw{JSON encode_json decode_json encode_pretty_json encode_canon_json stream_json_l stream_json_l_file stream_json_l_url};
49 our @EXPORT_OK = qw{JSON_IS_PP JSON_IS_XS JSON_IS_CPANEL JSON_IS_CPANEL_OR_XS};
50
51 BEGIN { require Exporter; our @ISA = qw(Exporter) }
52
53 my $json = JSON->new->utf8(1)->convert_blessed(1)->allow_nonref(1);
54 my $json_non_utf8 = JSON->new->utf8(0)->convert_blessed(1)->allow_nonref(1);
55 my $canon = JSON->new->utf8(1)->canonical(1)->convert_blessed(1)->allow_nonref(1);
56 my $pretty = JSON->new->utf8(1)->pretty(1)->canonical(1)->convert_blessed(1)->allow_nonref(1);
57
58 sub encode_json { $json->encode(@_) }
59 sub encode_canon_json { $canon->encode(@_) }
60 sub encode_pretty_json { $pretty->encode(@_) }
61
62 sub decode_json {
63 my ($input) = @_;
64 my $data;
65
66 local $@;
67 my $error;
68
69 # Try to decode the JSON stream as utf8. In malformed tests or tests which are intentionally
70 # testing bytes behavior we need to accept the bytes from the JSON file instead.
71 my $ok = eval { $data = $json->decode($input); 1 } || do {
72 $error = $@;
73 eval { $data = $json_non_utf8->decode($input); 1 };
74 };
75 $error ||= $@;
76 return $data if $ok;
77 my $mess = Carp::longmess("JSON decode error: $error");
78 die "$mess\n=======\n$input\n=======\n";
79 }
80
81 sub stream_json_l {
82 my ($path, $handler, %params) = @_;
83
84 croak "No path provided" unless $path;
85
86 return stream_json_l_file($path, $handler) if -f $path;
87 return stream_json_l_url($path, $handler, %params) if $path =~ m{^https?://};
88
89 croak "'$path' is not a valid path (file does not exist, or is not an http(s) url)";
90 }
91
92 sub stream_json_l_file {
93 my ($path, $handler) = @_;
94
95 croak "Invalid file '$path'" unless -f $path;
96
97 croak "Path must have a .json or .jsonl extension with optional .gz or .bz2 postfix."
98 unless $path =~ m/\.(json(?:l)?)(?:.(?:bz2|gz))?$/;
99
100 if ($1 eq 'json') {
101 require Test2::Harness::Util::File::JSON;
102 my $json = Test2::Harness::Util::File::JSON->new(name => $path);
103 $handler->($json->read);
104 }
105 else {
106 require Test2::Harness::Util::File::JSONL;
107 my $jsonl = Test2::Harness::Util::File::JSONL->new(name => $path);
108 while (my ($item) = $jsonl->poll(max => 1)) {
109 $handler->($item);
110 }
111 }
112
113 return 1;
114 }
115
116 sub stream_json_l_url {
117 my ($path, $handler, %params) = @_;
118 my $meth = $params{http_method} // 'get';
119 my $args = $params{http_args} // [];
120
121 require HTTP::Tiny;
122 my $ht = HTTP::Tiny->new();
123
124 my $buffer = '';
125 my $iterate = sub {
126 my ($res) = @_;
127
128 my @parts = split /(\n)/, $buffer;
129
130 while (@parts > 1) {
131 my $line = shift @parts;
132 my $nl = shift @parts;
133 my $data;
134 unless (eval { $data = decode_json($line); 1 }) {
135 warn "Unable to decode json for chunk when parsing json/l chunk:\n----\n$line\n----\n$@\n----\n";
136 next;
137 }
138
139 $handler->($data, $res);
140 }
141
142 $buffer = shift @parts // '';
143 };
144
145 my $res = $ht->$meth(
146 $path,
147 {
148 @$args,
149 data_callback => sub {
150 my ($chunk, $res) = @_;
151 $buffer .= $chunk;
152 $iterate->($res);
153 },
154 }
155 );
156
157 if (length($buffer)) {
158 $buffer .= "\n" unless $buffer =~ m/\n$/;
159 $iterate->($res);
160 }
161
162 return $res;
163 }
164
165 1;
166
167 __END__
168
169 =pod
170
171 =encoding UTF-8
172
173 =head1 NAME
174
175 Test2::Harness::Util::JSON - Utility class to help Test2::Harness pick the best
176 JSON implementation.
177
178 =head1 DESCRIPTION
179
180 This package provides functions for encoding/decoding json, and uses the best
181 json tools available.
182
183 =head1 SYNOPSIS
184
185 use Test2::Harness::Util::JSON qw/encode_json decode_json/;
186
187 my $data = { foo => 1 };
188 my $json = encode_json($data);
189 my $copy = decode_json($json);
190
191 =head1 EXPORTS
192
193 =over 4
194
195 =item $package = JSON()
196
197 This returns the JSON package being used by yath.
198
199 =item $bool = JSON_IS_PP()
200
201 True if yath is using L<JSON::PP>.
202
203 =item $bool = JSON_IS_XS()
204
205 True if yath is using L<JSON::XS>.
206
207 =item $bool = JSON_IS_CPANEL()
208
209 True if yath is using L<Cpanel::JSON::XS>.
210
211 =item $bool = JSON_IS_CPANEL_OR_XS()
212
213 True if either L<JSON::XS> or L<Cpanel::JSON::XS> are being used.
214
215 =item $string = encode_json($data)
216
217 Encode data into json. String will be 1-line.
218
219 =item $data = decode_json($string)
220
221 Decode json data from the string.
222
223 =item $string = encode_pretty_json($data)
224
225 Encode into human-friendly json.
226
227 =item $string = encode_canon_json($data)
228
229 Encode into canon-json.
230
231 =back
232
233 =head1 SOURCE
234
235 The source code repository for Test2-Harness can be found at
236 F<http://github.com/Test-More/Test2-Harness/>.
237
238 =head1 MAINTAINERS
239
240 =over 4
241
242 =item Chad Granum E<lt>[email protected]<gt>
243
244 =back
245
246 =head1 AUTHORS
247
248 =over 4
249
250 =item Chad Granum E<lt>[email protected]<gt>
251
252 =back
253
254 =head1 COPYRIGHT
255
256 Copyright 2020 Chad Granum E<lt>[email protected]<gt>.
257
258 This program is free software; you can redistribute it and/or
259 modify it under the same terms as Perl itself.
260
261 See F<http://dev.perl.org/licenses/>
262
263 =cut