lib/Module/Generic/JSON.pm

filter: Statementstmt 85.7% 108 / 126 Branchbran 46.5% 27 / 58 Conditioncond 44.1% 15 / 34 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 90.9% 20 / 22 Podpod 100% 6 / 6 totaltotal 66.9% 178 / 266 CC 44 SCAR 30.7
CC
44
Cov
69%
AUTOLOAD
51.5
init
14.6
to_json
11.6
CRAP 21.5
« lib/Module/Generic/Exception.pm ↑ Summary lib/Module/Generic/Iterator.pm »
1 ##----------------------------------------------------------------------------
2 ## Module Generic - ~/lib/Module/Generic/JSON.pm
3 ## Version v0.2.2
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2025/03/24
7 ## Modified 2025/04/23
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Module::Generic::JSON;
14 BEGIN
15 2 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use v5.16.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 warnings::register_categories( 'Module::Generic' );
20 2 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶ use vars qw( @EXPORT @EXPORT_OK $AUTOLOAD $DEBUG $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶ use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶ use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 our @ISA = qw( Module::Generic );
25 2 our @EXPORT = qw( from_json to_json encode_json decode_json );
26 2 our @EXPORT_OK = qw( new_json );
27 2 our %EXPORT_TAGS = ();
28 2 our $VERSION = 'v0.2.2';
29 };
30  
31 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 sub import
35 2 ▶{
Subroutine import: called CC=1 SCAR=0.0
36 2 my $this = shift( @_ );
37 2 $this->export_to_level( 1, undef, ( @_, @EXPORT ) );
38 }
39  
40 sub init
41 54 ▶{
Subroutine init: called CC=11 SCAR=26.8
Pod documented
42 54 my $self = shift( @_ );
43 54 $self->{_init_strict_use_sub} = 1;
44 54 my $opts = $self->_get_args_as_hash( @_ );
45 # try-catch
46 54 local $@;
47 54 my $j = eval{ JSON->new };
48 54 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 54 54
49 {
50 0 return( $self->error( "Error instantiating a JSON object: $@" ) );
51 }
52 54 my $equi =
53 {
54 order => 'canonical',
55 ordered => 'canonical',
56 sorted => 'canonical',
57 sort => 'canonical',
58 };
59  
60 # We remove it to prevent it from interfering with out checks
61 54 ▶ my $debug = ( CORE::exists( $opts->{debug} ) ? CORE::delete( $opts->{debug} ) : undef );
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'debug'} ? : 0 54 54
62 54 foreach my $opt ( keys( %$opts ) )
63 {
64 6 ▶ my $meth = exists( $equi->{ $opt } ) ? $equi->{ $opt } : $opt;
Branch: 1 branch
BranchTrueFalseTotal
exists $equi->{$opt} ? : 0 6 6
65 $j->can( $meth ) || do
66 6 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$j->can($meth) or do { warn "Unknown JSON option '${opt}'\n" if $self->_warnings_is_enabled("Module::Generic"); next } 5 1 6
67 1 ▶ warn( "Unknown JSON option '${opt}'\n" ) if( $self->_warnings_is_enabled( 'Module::Generic' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled("Module::Generic") 0 1 1
68 1 next;
69 };
70  
71 eval
72 5 {
73 5 $j->$meth( $opts->{ $opt } );
74 };
75 5 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
76 {
77 0 ▶ if( $@ =~ /perl[[:blank:]\h]+structure[[:blank:]\h]+exceeds[[:blank:]\h]+maximum[[:blank:]\h]+nesting[[:blank:]\h]+level/i )
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /perl[[:blank:]\h]+structure[[:blank:]\h]+exceeds[[:blank:]\h]+maximum[[:blank:]\h]+nesting[[:blank:]\h]+level/i) { } 0 0 0
78 {
79 0 my $max = $j->get_max_depth;
80 0 return( $self->error( "Unable to set json option ${opt}: $@ (max_depth value is ${max})" ) );
81 }
82 else
83 {
84 0 return( $self->error( "Unable to set json option ${opt}: $@" ) );
85 }
86 }
87 5 delete( $opts->{ $opt } );
88 }
89 54 $self->{_json} = $j;
90 54 ▶ $opts->{debug} = $debug if( defined( $debug ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $debug 0 54 54
91 # Pass the rest to our parent init for properties unique to our module.
92 54 ▶ $self->SUPER::init( %$opts ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(%$opts) or return $self->pass_error 54 0 54
93 54 return( $self );
94 }
95  
96 sub decode_json($)
97 6 ▶{
Subroutine decode_json: called CC=2 SCAR=7.2
Pod documented
98 6 my $json = __PACKAGE__->new;
99 my $rv = eval
100 6 {
101 6 $json->utf8->decode( @_ );
102 };
103 6 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
104 {
105 0 return( $json->error( $@ ) );
106 }
107 6 return( $rv );
108 }
109  
110 sub encode_json($)
111 5 ▶{
Subroutine encode_json: called CC=2 SCAR=7.2
Pod documented
112 5 my $json = __PACKAGE__->new;
113 my $rv = eval
114 5 {
115 5 $json->utf8->encode( @_ );
116 };
117 5 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
118 {
119 0 return( $json->error( $@ ) );
120 }
121 5 return( $rv );
122 }
123  
124 sub to_json($@)
125 1 ▶{
Subroutine to_json: called CC=7 SCAR=24.5
Pod documented
126 1 ▶ if( ref($_[0]) eq __PACKAGE__ or
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq __PACKAGE__ or @_ > 2 and $_[0] eq __PACKAGE__) 0 1 1
Condition33%2/6
ref $_[0] eq __PACKAGE__ or @_ > 2 and $_[0] eq __PACKAGE__
l!l&&r!l&&!r
001
@_ > 2 and $_[0] eq __PACKAGE__
!ll&&!rl&&r
100
MC/DC0%0/3
ref $_[0] eq __PACKAGE__ or @_ > 2 and $_[0] eq __PACKAGE__
ref $_[0] eq __PACKAGE__ @_ > 2 $_[0] eq __PACKAGE__
Truth table25%1/4
A or B and C
A= ref $_[0] eq __PACKAGE__B= @_ > 2C= $_[0] eq __PACKAGE__
ABCresult
1XX1
0111
00X0
0100
127 ( @_ > 2 and $_[0] eq __PACKAGE__ ) )
128 {
129 0 return( __PACKAGE__->error( "to_json should not be called as a method." ) );
130 }
131  
132 1 my $opts = {};
133 1 ▶ if( @_ == 2 and ref($_[1]) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and ref $_[1] eq "HASH") 1 0 1
Condition33%1/3
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
134 {
135 1 $opts = $_[1];
136 }
137 1 ▶ my $json = __PACKAGE__->new( %$opts ) ||
Condition50%1/2
__PACKAGE__->new(%$opts) || (return __PACKAGE__->pass_error)
l!l
10
MC/DC0%0/1
__PACKAGE__->new(%$opts) || (return __PACKAGE__->pass_error)
__PACKAGE__->new(%$opts)
Truth table50%1/2
A
A= __PACKAGE__->new(%$opts)
Aresult
11
00
138 return( __PACKAGE__->pass_error );
139 1 return( $json->encode( $_[0] ) );
140 }
141  
142  
143 sub from_json($@)
144 1 ▶{
Subroutine from_json: called CC=6 SCAR=21.6
Pod documented
145 1 ▶ if( ref( $_[0] ) eq __PACKAGE__ or $_[0] eq __PACKAGE__ )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__) 0 1 1
Condition33%1/3
ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__
l!l&&r!l&&!r
001
MC/DC0%0/2
ref $_[0] eq __PACKAGE__ or $_[0] eq __PACKAGE__
ref $_[0] eq __PACKAGE__ $_[0] eq __PACKAGE__
Truth table33%1/3
A or B
A= ref $_[0] eq __PACKAGE__B= $_[0] eq __PACKAGE__
ABresult
1X1
011
000
146 {
147 0 return( __PACKAGE__->error( "from_json should not be called as a method." ) );
148 }
149  
150 1 my $opts = {};
151 1 ▶ if( @_ == 2 and ref($_[1]) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and ref $_[1] eq "HASH") 1 0 1
Condition33%1/3
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
152 {
153 1 $opts = $_[1];
154 }
155 1 ▶ my $json = __PACKAGE__->new( %$opts ) ||
Condition50%1/2
__PACKAGE__->new(%$opts) || (return __PACKAGE__->pass_error)
l!l
10
MC/DC0%0/1
__PACKAGE__->new(%$opts) || (return __PACKAGE__->pass_error)
__PACKAGE__->new(%$opts)
Truth table50%1/2
A
A= __PACKAGE__->new(%$opts)
Aresult
11
00
156 return( __PACKAGE__->pass_error );
157 1 return( $json->decode( $_[0] ) );
158 }
159  
160 sub new_json
161 41 ▶{
Subroutine new_json: called CC=1 SCAR=0.0
Pod documented
162 41 my $self = __PACKAGE__->new( @_ );
163 41 $self->debug( $DEBUG );
164 41 return( $self );
165 }
166  
167 sub AUTOLOAD
168 126 ▶{
Subroutine AUTOLOAD: called CC=21 SCAR=39.4
169 126 my $self;
170 126 ▶ $self = shift( @_ ) if( Scalar::Util::blessed( $_[0] ) && $_[0]->isa( 'Module::Generic::JSON' ) );
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($_[0]) and $_[0]->isa("Module::Generic::JSON") 126 0 126
Condition33%1/3
builtin::blessed($_[0]) and $_[0]->isa("Module::Generic::JSON")
!ll&&!rl&&r
00126
MC/DC0%0/2
builtin::blessed($_[0]) and $_[0]->isa("Module::Generic::JSON")
builtin::blessed($_[0]) $_[0]->isa("Module::Generic::JSON")
Truth table33%1/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa("Module::Generic::JSON")
ABresult
0X0
100
111
171 126 my @args = @_;
172 126 my( $class, $meth, $code );
173 126 ▶ $class = ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
12600
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
174 126 $meth = $AUTOLOAD;
175 126 ▶ if( CORE::index( $meth, '::' ) != -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($meth, "::") != -1) 126 0 126
176 {
177 126 my $idx = rindex( $meth, '::' );
178 126 $class = substr( $meth, 0, $idx );
179 126 $meth = substr( $meth, $idx + 2 );
180 }
181  
182  
183 126 ▶ if( $self )
Branch: 2 branches
BranchTrueFalseTotal
if ($self) { } 126 0 126
elsif ($code = 'JSON'->can($meth)) { } 0 0 0
184 {
185 126 ▶ my $j = $self->{_json} || return( $self->error( "No JSON object could be found! This should not happen." ) );
Condition50%1/2
$self->{'_json'} || (return $self->error('No JSON object could be found! This should not happen.'))
l!l
1260
MC/DC0%0/1
$self->{'_json'} || (return $self->error('No JSON object could be found! This should not happen.'))
$self->{'_json'}
Truth table50%1/2
A
A= $self->{'_json'}
Aresult
11
00
186 126 ▶ if( $code = $j->can( $meth ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($code = $j->can($meth)) { } 126 0 126
187 {
188 126 local $@;
189 126 my $wantlist = wantarray();
190 my @rv = eval
191 126 {
192 126 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: called CC=1 SCAR=0.0
193 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
194 126 ▶ ( $wantlist // '' ) ? ( $code->( $j, scalar( @args ) ? @args : () ) ) : scalar( $code->( $j, scalar( @args ) ? @args : () ) )
Branch: 3 branches
BranchTrueFalseTotal
$wantlist // '' ? : 0 126 126
scalar @args ? : 0 0 0
scalar @args ? : 106 20 126
Condition100%2/2
$wantlist // ""
l!l
8739
MC/DC100%1/1
$wantlist // ""
$wantlist
Truth table100%2/2
A
A= $wantlist
Aresult
11
00
195 };
196 126 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 41 85 126
197 {
198 41 return( $self->error( $@ ) );
199 }
200 85 ▶ if( Scalar::Util::blessed( $rv[0] ) && $rv[0]->isa( 'JSON' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($rv[0]) and $rv[0]->isa('JSON')) { } 51 34 85
Condition66%2/3
builtin::blessed($rv[0]) and $rv[0]->isa("JSON")
!ll&&!rl&&r
34051
MC/DC50%1/2
builtin::blessed($rv[0]) and $rv[0]->isa("JSON")
builtin::blessed($rv[0]) $rv[0]->isa("JSON")
Truth table66%2/3
A and B
A= builtin::blessed($rv[0])B= $rv[0]->isa("JSON")
ABresult
0X0
100
111
201 {
202 51 return( $self );
203 }
204 else
205 {
206 34 ▶ return( ( $wantlist // '' ) ? @rv : $rv[0] );
Branch: 1 branch
BranchTrueFalseTotal
$wantlist // '' ? : 0 34 34
Condition50%1/2
$wantlist // ""
l!l
340
MC/DC0%0/1
$wantlist // ""
$wantlist
Truth table50%1/2
A
A= $wantlist
Aresult
11
00
207 }
208 }
209 else
210 {
211 0 return( $self->error( "Unknown JSON method '${meth}'" ) );
212 }
213 }
214 elsif( $code = JSON->can( $meth ) )
215 {
216 0 local $@;
217 my @rv = eval
218 0 {
219 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
220 0 ▶ $code->( scalar( @args ) ? @args : () );
Branch: 1 branch
BranchTrueFalseTotal
scalar @args ? : 0 0 0
221 };
222 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
223 {
224 0 return( __PACKAGE__->error( $@ ) );
225 }
226 0 ▶ return( wantarray() ? @rv : $rv[0] );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
227 }
228 else
229 {
230 0 die( "Unknown class function '${meth}' in JSON" );
231 }
232 }
233  
234 sub DESTROY
235 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=0.0
236 # DESTROY exists to avoid being caught by AUTOLOAD
237 };
238  
239 2 1;
240 # NOTE: POD
241 __END__
« lib/Module/Generic/Exception.pm ↑ Summary lib/Module/Generic/Iterator.pm »