Venus/Json.pm

filter: Statementstmt 86.0% 80 / 93 Branchbran 72.7% 16 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 78.9% 15 / 19 Podpod 25.0% 2 / 8 totaltotal 77.5% 114 / 147 CC 18 SCAR 15.6
CC
18
Cov
82%
config
12.3
FROM_BOOL
5.0
TO_BOOL
5.0
CRAP 4.7
« Venus/Template.pm ↑ Summary Venus/Try.pm »
1 package Venus::Json;
2  
3 2 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 use overload (
9 2 '""' => 'explain',
10 '~~' => 'explain',
11 fallback => 1,
12 2 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Venus::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 base 'Venus::Kind::Utility';
17  
18 2 with 'Venus::Role::Accessible';
19 2 with 'Venus::Role::Explainable';
20  
21 # ATTRIBUTES
22  
23 2 attr 'decoder';
24 2 attr 'encoder';
25  
26 # BUILDERS
27  
28 sub build_self {
29 4 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=1 SCAR=0.0
Pod undocumented
30  
31 4 return $self->config;
32 }
33  
34 # METHODS
35  
36 sub config {
37 4 ▶ my ($self, $package) = @_;
Subroutine config: called CC=7 SCAR=25.1
Pod undocumented
38  
39 4 ▶ $package ||= $self->package or $self->throw->error({
Branch: 1 branch
BranchTrueFalseTotal
$package ||= $self->package or $self->throw->error({"message", "No suitable JSON package"}) 0 4 4
Condition33%1/3
$package ||= $self->package
l!l&&r!l&&!r
040
MC/DC0%0/2
$package ||= $self->package
$package $self->package
Truth table33%1/3
A ||= B
A= $packageB= $self->package
ABresult
1X1
011
000
40 message => 'No suitable JSON package',
41 });
42  
43 4 $package = $package->new
44 ->canonical
45 ->allow_nonref
46 ->allow_unknown
47 ->allow_blessed
48 ->convert_blessed
49 ->pretty;
50  
51 4 ▶ if ($package->can('escape_slash')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->can("escape_slash")) 4 0 4
52 4 $package->escape_slash;
53 }
54  
55 # Cpanel::JSON::XS
56 4 ▶ if ($package->isa('Cpanel::JSON::XS')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->isa("Cpanel::JSON::XS")) 4 0 4
57 $self->decoder(sub {
58 1 ▶ my ($text) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 $package->decode($text);
60 4 });
61 $self->encoder(sub {
62 5 ▶ my ($data) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
63 5 $package->encode($data);
64 4 });
65 }
66  
67 # JSON::XS
68 4 ▶ if ($package->isa('JSON::XS')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->isa("JSON::XS")) 0 4 4
69 $self->decoder(sub {
70 0 ▶ my ($text) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
71 0 $package->decode($text);
72 0 });
73 $self->encoder(sub {
74 0 ▶ my ($data) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 $package->encode($data);
76 0 });
77 }
78  
79 # JSON::PP
80 4 ▶ if ($package->isa('JSON::PP')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($package->isa("JSON::PP")) 0 4 4
81 $self->decoder(sub {
82 0 ▶ my ($text) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 0 $package->decode($text);
84 0 });
85 $self->encoder(sub {
86 0 ▶ my ($data) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
87 0 $package->encode($data);
88 0 });
89 }
90  
91 4 return $self;
92 }
93  
94 sub decode {
95 1 ▶ my ($self, $data) = @_;
Subroutine decode: called CC=1 SCAR=0.0
Pod documented
96  
97 # double-traversing the data structure due to lack of serialization hooks
98 1 return $self->set(FROM_BOOL($self->decoder->($data)));
99 }
100  
101 sub encode {
102 5 ▶ my ($self) = @_;
Subroutine encode: called CC=1 SCAR=0.0
Pod documented
103  
104 # double-traversing the data structure due to lack of serialization hooks
105 5 return $self->encoder->(TO_BOOL($self->get));
106 }
107  
108 sub explain {
109 4 ▶ my ($self) = @_;
Subroutine explain: called CC=1 SCAR=0.0
Pod undocumented
110  
111 4 return $self->encode;
112 }
113  
114 sub package {
115 5 ▶ my ($self) = @_;
Subroutine package: called CC=4 SCAR=13.9
Pod undocumented
116  
117 5 state $engine;
118  
119 5 ▶ return $engine if defined $engine;
Branch: 1 branch
BranchTrueFalseTotal
if defined $engine 3 2 5
120  
121 2 my %packages = (
122 'JSON::XS' => '3.0',
123 'JSON::PP' => '2.27105',
124 'Cpanel::JSON::XS' => '4.09',
125 );
126 2 for my $package (qw(Cpanel::JSON::XS JSON::XS JSON::PP)) {
127 2 my $criteria = "require $package; $package->VERSION($packages{$package})";
128 2 ▶ if (do {local $@; eval "$criteria"; $@}) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { local $@; eval "$criteria"; $@ }) { } 0 2 2
129 0 next;
130 }
131 else {
132 2 $engine = $package;
133 2 last;
134 }
135 }
136  
137 2 return $engine;
138 }
139  
140 sub FROM_BOOL {
141 5 ▶ my ($value) = @_;
Subroutine FROM_BOOL: called CC=5 SCAR=16.1
Pod undocumented
142  
143 5 require Venus::Boolean;
144  
145 5 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 1 4 5
146 1 for my $key (keys %$value) {
147 2 $value->{$key} = FROM_BOOL($value->{$key});
148 }
149 1 return $value;
150 }
151  
152 4 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 1 3 4
153 1 for my $key (keys @$value) {
154 2 $value->[$key] = FROM_BOOL($value->[$key]);
155 }
156 1 return $value;
157 }
158  
159 3 return Venus::Boolean::TO_BOOL(Venus::Boolean::FROM_BOOL($value));
160 }
161  
162 sub TO_BOOL {
163 20 ▶ my ($value) = @_;
Subroutine TO_BOOL: called CC=5 SCAR=16.1
Pod undocumented
164  
165 20 require Venus::Boolean;
166  
167 20 ▶ if (ref($value) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 3 17 20
168 3 $value = {
169 %$value
170 };
171 3 for my $key (keys %$value) {
172 9 $value->{$key} = TO_BOOL($value->{$key});
173 }
174 3 return $value;
175 }
176  
177 17 ▶ if (ref($value) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 3 14 17
178 3 $value = [
179 @$value
180 ];
181 3 for my $key (keys @$value) {
182 6 $value->[$key] = TO_BOOL($value->[$key]);
183 }
184 3 return $value;
185 }
186  
187 14 return Venus::Boolean::TO_BOOL_OBJ($value);
188 }
189  
190 2 1;
191  
192  
193  
194 =head1 NAME
195  
196 Venus::Json - Json Class
197  
198 =cut
199  
200 =head1 ABSTRACT
201  
202 Json Class for Perl 5
203  
204 =cut
205  
206 =head1 SYNOPSIS
207  
208 package main;
209  
210 use Venus::Json;
211  
212 my $json = Venus::Json->new(
213 value => { name => ['Ready', 'Robot'], version => 0.12, stable => !!1, }
214 );
215  
216 # $json->encode;
217  
218 =cut
219  
220 =head1 DESCRIPTION
221  
222 This package provides methods for reading and writing L<JSON|https://json.org>
223 data. B<Note:> This package requires that a suitable JSON is installed,
224 currently either C<JSON::XS> C<3.0+>, C<JSON::PP> C<2.27105+>, or
225 C<Cpanel::JSON::XS> C<4.09+>.
226  
227 =cut
228  
229 =head1 ATTRIBUTES
230  
231 This package has the following attributes:
232  
233 =cut
234  
235 =head2 decoder
236  
237 decoder(CodeRef)
238  
239 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
240  
241 =cut
242  
243 =head2 encoder
244  
245 encoder(CodeRef)
246  
247 This attribute is read-write, accepts C<(CodeRef)> values, and is optional.
248  
249 =cut
250  
251 =head1 INHERITS
252  
253 This package inherits behaviors from:
254  
255 L<Venus::Kind::Utility>
256  
257 =cut
258  
259 =head1 INTEGRATES
260  
261 This package integrates behaviors from:
262  
263 L<Venus::Role::Accessible>
264  
265 L<Venus::Role::Explainable>
266  
267 =cut
268  
269 =head1 METHODS
270  
271 This package provides the following methods:
272  
273 =cut
274  
275 =head2 decode
276  
277 decode(Str $json) (Any)
278  
279 The decode method decodes the JSON string, sets the object value, and returns
280 the decoded value.
281  
282 I<Since C<0.01>>
283  
284 =over 4
285  
286 =item decode example 1
287  
288 # given: synopsis;
289  
290 my $decode = $json->decode('{"codename":["Ready","Robot"],"stable":true}');
291  
292 # { codename => ["Ready", "Robot"], stable => 1 }
293  
294 =back
295  
296 =cut
297  
298 =head2 encode
299  
300 encode() (Str)
301  
302 The encode method encodes the objects value as a JSON string and returns the
303 encoded string.
304  
305 I<Since C<0.01>>
306  
307 =over 4
308  
309 =item encode example 1
310  
311 # given: synopsis;
312  
313 my $encode = $json->encode;
314  
315 # '{ "name": ["Ready", "Robot"], "stable": true, "version": 0.12 }'
316  
317 =back
318  
319 =cut
« Venus/Template.pm ↑ Summary Venus/Try.pm »