blib/lib/Venus/Json.pm

filter: Statementstmt 80.0% 88 / 110 Branchbran 75.0% 18 / 24 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 73.9% 17 / 23 Podpod 16.6% 2 / 12 totaltotal 73.1% 131 / 179 CC 20 SCAR 14.8
CC
20
Cov
79%
config
12.3
FROM_BOOL
5.0
TO_BOOL
5.0
CRAP 4.4
« blib/lib/Venus/Hash.pm ↑ Summary blib/lib/Venus/Match.pm »
1 package Venus::Json;
2  
3 5 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 5 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # INHERITS
13  
14 5 base 'Venus::Kind::Utility';
15  
16 # INTEGRATES
17  
18 5 with 'Venus::Role::Valuable';
19 5 with 'Venus::Role::Buildable';
20 5 with 'Venus::Role::Accessible';
21 5 with 'Venus::Role::Explainable';
22  
23 # OVERLOADS
24  
25 5 ▶use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 '""' => 'explain',
27 '~~' => 'explain',
28 fallback => 1,
29 5 );
30  
31 # ATTRIBUTES
32  
33 5 attr 'decoder';
34 5 attr 'encoder';
35  
36 # BUILDERS
37  
38 4 ▶sub build_arg {
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
39 4 my ($self, $data) = @_;
40  
41 return {
42 4 value => $data
43 };
44 }
45  
46 29 ▶sub build_args {
Subroutine build_args: called CC=3 SCAR=11.0
Pod undocumented
47 29 my ($self, $data) = @_;
48  
49 29 ▶ if (keys %$data == 1 && exists $data->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$data == 1 and exists $data->{"value"}) 9 20 29
Condition100%3/3
keys %$data == 1 and exists $data->{"value"}
!ll&&!rl&&r
1559
MC/DC100%2/2
keys %$data == 1 and exists $data->{"value"}
keys %$data == 1 exists $data->{"value"}
Truth table100%3/3
A and B
A= keys %$data == 1B= exists $data->{"value"}
ABresult
0X0
100
111
50 9 return $data;
51 }
52 return {
53 20 value => $data
54 };
55 }
56  
57 0 ▶sub build_nil {
Subroutine build_nil: not called CC=1 SCAR=6.9
Pod undocumented
58 0 my ($self, $data) = @_;
59  
60 return {
61 0 value => $data
62 };
63 }
64  
65 29 ▶sub build_self {
Subroutine build_self: called CC=1 SCAR=0.0
Pod undocumented
66 29 my ($self, $data) = @_;
67  
68 29 return $self->config;
69 }
70  
71 # METHODS
72  
73 29 ▶sub config {
Subroutine config: called CC=7 SCAR=25.1
Pod undocumented
74 29 my ($self, $package) = @_;
75  
76 29 ▶ $package ||= $self->package
Branch: 1 branch
BranchTrueFalseTotal
$package ||= $self->package or $self->error_on_config->capture($package)->throw 29 0 29
Condition33%1/3
$package ||= $self->package
l!l&&r!l&&!r
0290
MC/DC0%0/2
$package ||= $self->package
$package $self->package
Truth table33%1/3
A ||= B
A= $packageB= $self->package
ABresult
1X1
011
000
77 or $self->error_on_config->capture($package)->throw;
78  
79 29 $package = $package->new
80 ->canonical
81 ->allow_nonref
82 ->allow_unknown
83 ->allow_blessed
84 ->convert_blessed
85 ->pretty;
86  
87 29 ▶ if ($package->can('escape_slash')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->can("escape_slash")) 29 0 29
88 29 $package->escape_slash;
89 }
90  
91 # Cpanel::JSON::XS
92 29 ▶ if ($package->isa('Cpanel::JSON::XS')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->isa("Cpanel::JSON::XS")) 29 0 29
93 10 ▶ $self->decoder(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
94 10 my ($text) = @_;
95 10 $package->decode($text);
96 29 });
97 20 ▶ $self->encoder(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
98 20 my ($data) = @_;
99 20 $package->encode($data);
100 29 });
101 }
102  
103 # JSON::XS
104 29 ▶ if ($package->isa('JSON::XS')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->isa("JSON::XS")) 0 29 29
105 0 ▶ $self->decoder(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 my ($text) = @_;
107 0 $package->decode($text);
108 0 });
109 0 ▶ $self->encoder(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 my ($data) = @_;
111 0 $package->encode($data);
112 0 });
113 }
114  
115 # JSON::PP
116 29 ▶ if ($package->isa('JSON::PP')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->isa("JSON::PP")) 0 29 29
117 0 ▶ $self->decoder(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 my ($text) = @_;
119 0 $package->decode($text);
120 0 });
121 0 ▶ $self->encoder(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 my ($data) = @_;
123 0 $package->encode($data);
124 0 });
125 }
126  
127 29 return $self;
128 }
129  
130 10 ▶sub decode {
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
131 10 my ($self, $data) = @_;
132  
133 # double-traversing the data structure due to lack of serialization hooks
134 10 return $self->set(FROM_BOOL($self->decoder->($data)));
135 }
136  
137 20 ▶sub encode {
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
138 20 my ($self) = @_;
139  
140 # double-traversing the data structure due to lack of serialization hooks
141 20 return $self->encoder->(TO_BOOL($self->get));
142 }
143  
144 7 ▶sub explain {
Subroutine explain: called CC=1 SCAR=0.0
Pod undocumented
145 7 my ($self) = @_;
146  
147 7 return $self->encode;
148 }
149  
150 50 ▶sub package {
Subroutine package: called CC=4 SCAR=13.9
Pod undocumented
151 50 my ($self) = @_;
152  
153 50 state $engine;
154  
155 50 ▶ return $engine if defined $engine;
Branch: 1 branch
BranchTrueFalseTotal
if defined $engine 45 5 50
156  
157 5 my %packages = (
158 'JSON::XS' => '3.0',
159 'JSON::PP' => '2.27105',
160 'Cpanel::JSON::XS' => '4.09',
161 );
162 5 for my $package (
163 grep defined,
164 $ENV{VENUS_JSON_PACKAGE},
165 qw(Cpanel::JSON::XS JSON::XS JSON::PP)
166 )
167 {
168 5 my $criteria = "require $package; $package->VERSION($packages{$package})";
169 5 ▶ if (do {local $@; eval "$criteria"; $@}) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { local $@; eval "$criteria"; $@ }) { } 0 5 5
170 0 next;
171 }
172 else {
173 5 $engine = $package;
174 5 last;
175 }
176 }
177  
178 5 return $engine;
179 }
180  
181 235 ▶sub FROM_BOOL {
Subroutine FROM_BOOL: called CC=5 SCAR=16.1
Pod undocumented
182 235 my ($value) = @_;
183  
184 235 require Venus::Boolean;
185  
186 235 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 85 150 235
187 85 for my $key (keys %$value) {
188 197 $value->{$key} = FROM_BOOL($value->{$key});
189 }
190 85 return $value;
191 }
192  
193 150 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 14 136 150
194 14 for my $key (keys @$value) {
195 28 $value->[$key] = FROM_BOOL($value->[$key]);
196 }
197 14 return $value;
198 }
199  
200 136 return Venus::Boolean::TO_BOOL(Venus::Boolean::FROM_BOOL($value));
201 }
202  
203 128 ▶sub TO_BOOL {
Subroutine TO_BOOL: called CC=5 SCAR=16.1
Pod undocumented
204 128 my ($value) = @_;
205  
206 128 require Venus::Boolean;
207  
208 128 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 36 92 128
209 36 $value = {
210 %$value
211 };
212 36 for my $key (keys %$value) {
213 88 $value->{$key} = TO_BOOL($value->{$key});
214 }
215 36 return $value;
216 }
217  
218 92 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 10 82 92
219 10 $value = [
220 @$value
221 ];
222 10 for my $key (keys @$value) {
223 20 $value->[$key] = TO_BOOL($value->[$key]);
224 }
225 10 return $value;
226 }
227  
228 82 return Venus::Boolean::TO_BOOL_JPO($value);
229 }
230  
231 # ERRORS
232  
233 0 ▶sub error_on_config {
Subroutine error_on_config: not called CC=1 SCAR=6.9
Pod undocumented
234 0 my ($self) = @_;
235  
236 0 my $error = $self->error->sysinfo;
237  
238 0 $error->name('on.config');
239 0 $error->message('No suitable JSON package');
240 0 $error->offset(1);
241 0 $error->reset;
242  
243 0 return $error;
244 }
245  
246 5 1;
247  
248  
249  
250 =head1 NAME
251  
252 Venus::Json - Json Class
253  
254 =cut
255  
256 =head1 ABSTRACT
257  
258 Json Class for Perl 5
259  
260 =cut
261  
262 =head1 SYNOPSIS
263  
264 package main;
265  
266 use Venus::Json;
267  
268 my $json = Venus::Json->new(
269 value => { name => ['Ready', 'Robot'], version => 0.12, stable => !!1, }
270 );
271  
272 # $json->encode;
273  
274 =cut
275  
276 =head1 DESCRIPTION
277  
278 This package provides methods for reading and writing L<JSON|https://json.org>
279 data. B<Note:> This package requires that a suitable JSON library is installed,
280 currently either C<JSON::XS> C<3.0+>, C<JSON::PP> C<2.27105+>, or
281 C<Cpanel::JSON::XS> C<4.09+>. You can use the C<VENUS_JSON_PACKAGE> environment
282 variable to include or prioritize your preferred JSON library.
283  
284 =cut
285  
286 =head1 ATTRIBUTES
287  
288 This package has the following attributes:
289  
290 =cut
291  
292 =head2 decoder
293  
294 decoder(CodeRef)
295  
296 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
297  
298 =cut
299  
300 =head2 encoder
301  
302 encoder(CodeRef)
303  
304 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
305  
306 =cut
307  
308 =head1 INHERITS
309  
310 This package inherits behaviors from:
311  
312 L<Venus::Kind::Utility>
313  
314 =cut
315  
316 =head1 INTEGRATES
317  
318 This package integrates behaviors from:
319  
320 L<Venus::Role::Accessible>
321  
322 L<Venus::Role::Buildable>
323  
324 L<Venus::Role::Explainable>
325  
326 L<Venus::Role::Valuable>
327  
328 =cut
329  
330 =head1 METHODS
331  
332 This package provides the following methods:
333  
334 =cut
335  
336 =head2 decode
337  
338 decode(string $json) (any)
339  
340 The decode method decodes the JSON string, sets the object value, and returns
341 the decoded value.
342  
343 I<Since C<0.01>>
344  
345 =over 4
346  
347 =item decode example 1
348  
349 # given: synopsis;
350  
351 my $decode = $json->decode('{"codename":["Ready","Robot"],"stable":true}');
352  
353 # { codename => ["Ready", "Robot"], stable => 1 }
354  
355 =back
356  
357 =cut
358  
359 =head2 encode
360  
361 encode() (string)
362  
363 The encode method encodes the objects value as a JSON string and returns the
364 encoded string.
365  
366 I<Since C<0.01>>
367  
368 =over 4
369  
370 =item encode example 1
371  
372 # given: synopsis;
373  
374 my $encode = $json->encode;
375  
376 # '{ "name": ["Ready", "Robot"], "stable": true, "version": 0.12 }'
377  
378 =back
379  
380 =cut
381  
382 =head2 new
383  
384 new(any @args) (Venus::Json)
385  
386 The new method constructs an instance of the package.
387  
388 I<Since C<4.15>>
389  
390 =over 4
391  
392 =item new example 1
393  
394 package main;
395  
396 use Venus::Json;
397  
398 my $new = Venus::Json->new;
399  
400 # bless(..., "Venus::Json")
401  
402 =back
403  
404 =over 4
405  
406 =item new example 2
407  
408 package main;
409  
410 use Venus::Json;
411  
412 my $new = Venus::Json->new({password => 'secret'});
413  
414 # bless(..., "Venus::Json")
415  
416 =back
417  
418 =over 4
419  
420 =item new example 3
421  
422 package main;
423  
424 use Venus::Json;
425  
426 my $new = Venus::Json->new(value => {password => 'secret'});
427  
428 # bless(..., "Venus::Json")
429  
430 =back
431  
432 =over 4
433  
434 =item B<may raise> L<Venus::Json::Error> C<on.config>
435  
436 package main;
437  
438 use Venus::Json;
439  
440 local $ENV{VENUS_JSON_PACKAGE} = 'Fake::Json';
441  
442 my $new = Venus::Json->new;
443  
444 # Error! (on.config)
445  
446 =back
447  
448 =cut
449  
450 =head1 AUTHORS
451  
452 Awncorp, C<awncorp@cpan.org>
453  
454 =cut
455  
456 =head1 LICENSE
457  
458 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
459  
460 This program is free software, you can redistribute it and/or modify it under
461 the terms of the Apache license version 2.0.
462  
463 =cut
« blib/lib/Venus/Hash.pm ↑ Summary blib/lib/Venus/Match.pm »