blib/lib/JSON/YY.pm

filter: Statementstmt 97.0% 131 / 135 Branchbran 82.5% 33 / 40 Conditioncond 45.0% 18 / 40 MC/DCmcdc 25.0% 5 / 20 Subroutinesub 94.1% 32 / 34 Podpod 100% 12 / 12 totaltotal 82.2% 231 / 281 CC 43 SCAR 19.3
CC
43
Cov
85%
import
16.5
is_bool
13.7
canonical
6.0
CRAP 6.9
↑ Summary yyjson.c »
1 package JSON::YY;
2  
3 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 30 our $VERSION = '0.09';
9  
10 30 require XSLoader;
11 30 XSLoader::load('JSON::YY', $VERSION);
12  
13 30 our @EXPORT_OK = qw(encode_json decode_json decode_json_ro is_bool true false);
14  
15 # JSON::PP assigns $JSON::PP::true/false when it loads, so load it before
16 # handing them out: loaded later, it would replace the objects already given.
17 3 ▶sub true { require JSON::PP; $JSON::PP::true }
Subroutine true: called CC=1 SCAR=0.0
Pod documented
18 2 ▶sub false { require JSON::PP; $JSON::PP::false }
Subroutine false: called CC=1 SCAR=0.0
Pod documented
19  
20 6 ▶sub is_bool {
Subroutine is_bool: called CC=10 SCAR=26.2
Pod documented
21 6 my ($val) = @_;
22 6 ▶ return 0 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 5 6
23 5 ▶ if (Scalar::Util::blessed($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($val)) 2 3 5
24 2 ▶ return 1 if eval {
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $val->isa("JSON::PP::Boolean") or $val->isa("boolean") or $val->isa("Types::Serialiser::BooleanBase") or $val->isa("Types::Serialiser::Boolean") or $val->isa("JSON::XS::Boolean") } } 2 0 2
25 2 ▶ $val->isa('JSON::PP::Boolean')
Condition33%4/12
$val->isa('JSON::PP::Boolean') || $val->isa('boolean') || $val->isa('Types::Serialiser::BooleanBase') || $val->isa('Types::Serialiser::Boolean') || $val->isa('JSON::XS::Boolean')
l!l&&r!l&&!r
200
$val->isa("JSON::PP::Boolean") or $val->isa("boolean") or $val->isa("Types::Serialiser::BooleanBase") or $val->isa("Types::Serialiser::Boolean")
l!l&&r!l&&!r
200
$val->isa("JSON::PP::Boolean") or $val->isa("boolean") or $val->isa("Types::Serialiser::BooleanBase")
l!l&&r!l&&!r
200
$val->isa("JSON::PP::Boolean") or $val->isa("boolean")
l!l&&r!l&&!r
200
MC/DC0%0/5
$val->isa('JSON::PP::Boolean') || $val->isa('boolean') || $val->isa('Types::Serialiser::BooleanBase') || $val->isa('Types::Serialiser::Boolean') || $val->isa('JSON::XS::Boolean')
$val->isa("JSON::PP::Boolean") $val->isa("boolean") $val->isa("Types::Serialiser::BooleanBase") $val->isa("Types::Serialiser::Boolean") $val->isa('JSON::XS::Boolean')
Truth table16%1/6
A or B or C or D || E
A= $val->isa("JSON::PP::Boolean")B= $val->isa("boolean")C= $val->isa("Types::Serialiser::BooleanBase")D= $val->isa("Types::Serialiser::Boolean")E= $val->isa('JSON::XS::Boolean')
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
26 || $val->isa('boolean')
27 || $val->isa('Types::Serialiser::BooleanBase')
28 || $val->isa('Types::Serialiser::Boolean')
29 || $val->isa('JSON::XS::Boolean')
30 };
31 }
32 3 ▶ if (defined &builtin::is_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &builtin::is_bool) 3 0 3
33 30 ▶ no if $] >= 5.036, warnings => 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
34 3 ▶ return 1 if builtin::is_bool($val);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::is_bool($val) 0 3 3
35 }
36 3 return 0;
37 }
38  
39 30 my @DOC_KEYWORDS = qw(jdoc jget jgetp jset jdel jhas jclone jencode
40 jstr jnum jbool jnull jarr jobj jtype jlen jkeys jdecode
41 jiter jnext jkey jpatch jmerge jfrom jvals jeq
42 jpp jraw jread jwrite jpaths jfind
43 jis_obj jis_arr jis_str jis_num jis_int jis_real jis_bool jis_null);
44  
45 # functional API — fast XS path, always utf8
46 30 *encode_json = \&_xs_encode_json;
47 30 *decode_json = \&_xs_decode_json;
48 30 *decode_json_ro = \&_xs_decode_json_ro;
49  
50 # max_depth reaches XS as a U32: -1 would wrap to 4294967295, which is not
51 # "unlimited" but "deep enough to blow the C stack".
52 21 ▶sub _checked_max_depth {
Subroutine _checked_max_depth: called CC=4 SCAR=13.9
53 21 my ($self, $val) = @_;
54 21 ▶ $val = 512 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 20 21
55 21 ▶ Carp::croak("max_depth must be a non-negative integer (got '$val')")
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /\A[0-9]+\z/ and $val <= 4294967295 10 11 21
Condition100%3/3
$val =~ /\A[0-9]+\z/ and $val <= 4294967295
!ll&&!rl&&r
8211
MC/DC100%2/2
$val =~ /\A[0-9]+\z/ and $val <= 4294967295
$val =~ /\A[0-9]+\z/ $val <= 4294967295
Truth table100%3/3
A and B
A= $val =~ /\A[0-9]+\z/B= $val <= 4294967295
ABresult
0X0
100
111
56 unless $val =~ /\A[0-9]+\z/ && $val <= 0xFFFFFFFF;
57 11 $self->_set_max_depth($val);
58 }
59  
60 13 ▶sub _checked_bool {
Subroutine _checked_bool: called CC=2 SCAR=6.9
61 13 my ($self, $val) = @_;
62 13 ▶ require JSON::PP if $val;
Branch: 1 branch
BranchTrueFalseTotal
if $val 12 1 13
63 13 $self->_set_bool($val);
64 }
65  
66 30 my %SETTERS = (
67 utf8 => \&_set_utf8,
68 pretty => \&_set_pretty,
69 canonical => \&_set_canonical,
70 allow_nonref => \&_set_allow_nonref,
71 allow_unknown => \&_set_allow_unknown,
72 allow_blessed => \&_set_allow_blessed,
73 convert_blessed => \&_set_convert_blessed,
74 max_depth => \&_checked_max_depth,
75 bool => \&_checked_bool,
76 boolean => \&_checked_bool,
77 boolean_object => \&_checked_bool,
78 );
79  
80 51 ▶sub import {
Subroutine import: called CC=16 SCAR=28.0
81 51 my $class = shift;
82 51 my @exports;
83 my @flags;
84 51 my $want_doc = 0;
85 51 for my $arg (@_) {
86 75 ▶ if ($arg eq ':doc') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg eq ':doc') { } 21 54 75
elsif ($arg =~ /^-(.+)/) { } 8 46 54
87 21 $want_doc = 1;
88 } elsif ($arg =~ /^-(.+)/) {
89 8 push @flags, $1;
90 } else {
91 46 push @exports, $arg;
92 }
93 }
94 51 my $caller = caller;
95  
96 # activate keywords via XS::Parse::Keyword hint keys
97 51 ▶ if ($want_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_doc) 21 30 51
98 21 $^H{"JSON::YY/$_"} = 1 for @DOC_KEYWORDS;
99 }
100 51 ▶ if (@flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flags) 6 45 51
101 6 my $coder = $class->new;
102 6 for my $f (@flags) {
103 8 ▶ my $setter = $SETTERS{$f}
Branch: 1 branch
BranchTrueFalseTotal
my $setter = $SETTERS{$f} or Carp::croak("unknown flag: -$f") 8 0 8
104 or Carp::croak("unknown flag: -$f");
105 8 $coder->$setter(1);
106 }
107 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 30 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
109 # a qw() import in the same scope leaves the keyword hint set, and the
110 # compiler would then bypass these closures entirely -- silently
111 # dropping the flags the caller just asked for
112 6 delete $^H{"JSON::YY/$_"} for qw(encode_json decode_json);
113 6 ▶ *{"${caller}::encode_json"} = sub { $coder->encode($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
114 6 ▶ *{"${caller}::decode_json"} = sub { $coder->decode($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
115 }
116 51 ▶ if (@exports) {
Branch: 1 branch
BranchTrueFalseTotal
if (@exports) 24 27 51
117 30 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
118 24 for my $e (@exports) {
119 Carp::croak("'$e' is not exported by JSON::YY")
120 46 ▶ unless grep { $_ eq $e } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $e;} @JSON::YY::EXPORT_OK 0 46 46
121 46 ▶ next if @flags && ($e eq 'encode_json' || $e eq 'decode_json');
Branch: 1 branch
BranchTrueFalseTotal
if @flags and $e eq "encode_json" || $e eq "decode_json" 0 46 46
Condition16%1/6
@flags and $e eq "encode_json" || $e eq "decode_json"
!ll&&!rl&&r
4600
$e eq "encode_json" or $e eq "decode_json"
l!l&&r!l&&!r
000
MC/DC0%0/3
@flags and $e eq "encode_json" || $e eq "decode_json"
@flags $e eq "encode_json" $e eq "decode_json"
Truth table25%1/4
A and B or C
A= @flagsB= $e eq "encode_json"C= $e eq "decode_json"
ABCresult
0XX0
1000
11X1
1011
122 46 *{"${caller}::$e"} = \&{$e};
123 # also enable keyword for exported functions
124 46 $^H{"JSON::YY/$e"} = 1;
125 }
126 }
127 }
128  
129 # chaining setters
130 10 ▶sub utf8 { $_[0]->_set_utf8($_[1] // 1); $_[0] }
Subroutine utf8: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
$_[1] // 1
l!l
010
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
131 3 ▶sub pretty { $_[0]->_set_pretty($_[1] // 1); $_[0] }
Subroutine pretty: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
$_[1] // 1
l!l
03
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
132 0 ▶sub canonical { $_[0]->_set_canonical($_[1] // 1); $_[0] }
Subroutine canonical: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[1] // 1
l!l
00
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
133 3 ▶sub allow_nonref { $_[0]->_set_allow_nonref($_[1] // 1); $_[0] }
Subroutine allow_nonref: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[1] // 1
l!l
21
MC/DC100%1/1
$_[1] // 1
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
134 0 ▶sub allow_unknown { $_[0]->_set_allow_unknown($_[1] // 1); $_[0] }
Subroutine allow_unknown: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
$_[1] // 1
l!l
00
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
135 1 ▶sub allow_blessed { $_[0]->_set_allow_blessed($_[1] // 1); $_[0] }
Subroutine allow_blessed: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
$_[1] // 1
l!l
01
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
136 2 ▶sub convert_blessed { $_[0]->_set_convert_blessed($_[1] // 1); $_[0] }
Subroutine convert_blessed: called CC=2 SCAR=7.2
Pod documented
Condition50%1/2
$_[1] // 1
l!l
02
MC/DC0%0/1
$_[1] // 1
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
137 4 ▶sub max_depth { $_[0]->_checked_max_depth($_[1]); $_[0] }
Subroutine max_depth: called CC=1 SCAR=0.0
Pod documented
138 3 ▶sub bool { $_[0]->_checked_bool($_[1] // 1); $_[0] }
Subroutine bool: called CC=2 SCAR=6.9
Pod documented
Condition100%2/2
$_[1] // 1
l!l
21
MC/DC100%1/1
$_[1] // 1
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
139 30 *boolean = \&bool;
140 30 *boolean_object = \&bool;
141  
142 # wrap XS new to accept keyword args
143 {
144 30 my $orig_new = JSON::YY->can('new');
145 30 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
146 101 ▶ *new = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
147 101 my ($class, %args) = @_;
148 101 my $self = $orig_new->($class);
149 101 for my $k (keys %args) {
150 111 ▶ my $setter = $SETTERS{$k}
Branch: 1 branch
BranchTrueFalseTotal
my $setter = $SETTERS{$k} or Carp::croak("unknown option: $k") 111 0 111
151 or Carp::croak("unknown option: $k");
152 111 $self->$setter($args{$k});
153 }
154 92 $self;
155 30 };
156 }
157  
158 # Doc overloading: stringify to JSON, boolean always true, eq/ne deep compare
159 package JSON::YY::Doc;
160 30 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
161  
162 # _doc_eq only understands Docs, so compare the JSON text when the other side
163 # is not one -- otherwise $doc eq '{"a":1}' is false while "$doc" eq it is true.
164 17 ▶sub _eq {
Subroutine _eq: called CC=5 SCAR=16.1
165 17 my ($self, $other) = @_;
166 17 ▶ return JSON::YY::_doc_eq($self, $other)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($other) and $other->isa("JSON::YY::Doc") 5 12 17
Condition66%2/3
builtin::blessed($other) and $other->isa("JSON::YY::Doc")
!ll&&!rl&&r
1205
MC/DC50%1/2
builtin::blessed($other) and $other->isa("JSON::YY::Doc")
builtin::blessed($other) $other->isa("JSON::YY::Doc")
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa("JSON::YY::Doc")
ABresult
0X0
100
111
167 if Scalar::Util::blessed($other) && $other->isa('JSON::YY::Doc');
168 # _doc_stringify returns UTF-8 bytes. The UTF8 flag cannot tell a character
169 # string from a byte string (chars < 256 may be stored either way), so
170 # accept the operand under either reading.
171 12 ▶ my $str = defined $other ? "$other" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $other ? : 11 1 12
172 12 my $json = JSON::YY::_doc_stringify($self);
173 12 ▶ return 1 if $json eq $str;
Branch: 1 branch
BranchTrueFalseTotal
if $json eq $str 5 7 12
174 7 my $enc = $str;
175 7 utf8::encode($enc);
176 7 return $json eq $enc;
177 }
178  
179 use overload
180 53 ▶ '""' => sub { JSON::YY::_doc_stringify($_[0]) },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
181 2 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
182 # without this `fallback` numifies via the string and every pair of Docs
183 # compares equal; identity is the only sensible number for a handle
184 6 ▶ '0+' => sub { Scalar::Util::refaddr($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
185 'eq' => \&_eq,
186 6 ▶ 'ne' => sub { !_eq(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
187 30 fallback => 1;
188  
189 package JSON::YY;
190 30 1;
191  
192 __END__
↑ Summary yyjson.c »