blib/lib/JSON/YY.pm

filter: Statementstmt 96.3% 105 / 109 Branchbran 88.4% 23 / 26 Conditioncond 55.0% 11 / 20 MC/DCmcdc 36.3% 4 / 11 Subroutinesub 92.5% 25 / 27 Podpod 100% 8 / 8 totaltotal 87.5% 176 / 201 CC 29 SCAR 16.3
CC
29
Cov
90%
import
13.0
canonical
6.0
allow_unknown
6.0
CRAP 5.1
↑ Summary yyjson.c »
1 package JSON::YY;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 our $VERSION = '0.07';
8  
9 27 require XSLoader;
10 27 XSLoader::load('JSON::YY', $VERSION);
11  
12 27 our @EXPORT_OK = qw(encode_json decode_json decode_json_ro);
13  
14 27 my @DOC_KEYWORDS = qw(jdoc jget jgetp jset jdel jhas jclone jencode
15 jstr jnum jbool jnull jarr jobj jtype jlen jkeys jdecode
16 jiter jnext jkey jpatch jmerge jfrom jvals jeq
17 jpp jraw jread jwrite jpaths jfind
18 jis_obj jis_arr jis_str jis_num jis_int jis_real jis_bool jis_null);
19  
20 # functional API — fast XS path, always utf8
21 27 *encode_json = \&_xs_encode_json;
22 27 *decode_json = \&_xs_decode_json;
23 27 *decode_json_ro = \&_xs_decode_json_ro;
24  
25 # max_depth reaches XS as a U32: -1 would wrap to 4294967295, which is not
26 # "unlimited" but "deep enough to blow the C stack".
27 sub _checked_max_depth {
28 21 ▶ my ($self, $val) = @_;
Subroutine _checked_max_depth: called CC=4 SCAR=13.9
29 21 ▶ $val = 512 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1 20 21
30 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
31 unless $val =~ /\A[0-9]+\z/ && $val <= 0xFFFFFFFF;
32 11 $self->_set_max_depth($val);
33 }
34  
35 27 my %SETTERS = (
36 utf8 => \&_set_utf8,
37 pretty => \&_set_pretty,
38 canonical => \&_set_canonical,
39 allow_nonref => \&_set_allow_nonref,
40 allow_unknown => \&_set_allow_unknown,
41 allow_blessed => \&_set_allow_blessed,
42 convert_blessed => \&_set_convert_blessed,
43 max_depth => \&_checked_max_depth,
44 );
45  
46 sub import {
47 43 ▶ my $class = shift;
Subroutine import: called CC=13 SCAR=25.7
48 43 my @exports;
49 my @flags;
50 43 my $want_doc = 0;
51 43 for my $arg (@_) {
52 62 ▶ if ($arg eq ':doc') {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg eq ':doc') { } 18 44 62
elsif ($arg =~ /^-(.+)/) { } 4 40 44
53 18 $want_doc = 1;
54 } elsif ($arg =~ /^-(.+)/) {
55 4 push @flags, $1;
56 } else {
57 40 push @exports, $arg;
58 }
59 }
60 43 my $caller = caller;
61  
62 # activate keywords via XS::Parse::Keyword hint keys
63 43 ▶ if ($want_doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($want_doc) 18 25 43
64 18 $^H{"JSON::YY/$_"} = 1 for @DOC_KEYWORDS;
65 }
66 43 ▶ if (@flags) {
Branch: 1 branch
BranchTrueFalseTotal
if (@flags) 3 40 43
67 3 my $coder = $class->new;
68 3 for my $f (@flags) {
69 4 ▶ my $setter = $SETTERS{$f}
Branch: 1 branch
BranchTrueFalseTotal
my $setter = $SETTERS{$f} or Carp::croak("unknown flag: -$f") 0 4 4
70 or Carp::croak("unknown flag: -$f");
71 4 $coder->$setter(1);
72 }
73 27 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
74 27 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 # a qw() import in the same scope leaves the keyword hint set, and the
76 # compiler would then bypass these closures entirely -- silently
77 # dropping the flags the caller just asked for
78 3 delete $^H{"JSON::YY/$_"} for qw(encode_json decode_json);
79 3 ▶ *{"${caller}::encode_json"} = sub { $coder->encode($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
80 3 ▶ *{"${caller}::decode_json"} = sub { $coder->decode($_[0]) };
Subroutine __ANON__: called CC=1 SCAR=0.0
81 }
82 43 ▶ if (@exports) {
Branch: 1 branch
BranchTrueFalseTotal
if (@exports) 22 21 43
83 27 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
84 22 for my $e (@exports) {
85 Carp::croak("'$e' is not exported by JSON::YY")
86 40 ▶ unless grep { $_ eq $e } @EXPORT_OK;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $e;} @JSON::YY::EXPORT_OK 0 40 40
87 40 *{"${caller}::$e"} = \&{$e};
88 # also enable keyword for exported functions
89 40 $^H{"JSON::YY/$e"} = 1;
90 }
91 }
92 }
93  
94 # chaining setters
95 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
96 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
97 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
98 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
99 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
100 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
101 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
102 4 ▶sub max_depth { $_[0]->_checked_max_depth($_[1]); $_[0] }
Subroutine max_depth: called CC=1 SCAR=0.0
Pod documented
103  
104 # wrap XS new to accept keyword args
105 {
106 27 my $orig_new = JSON::YY->can('new');
107 27 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
108 *new = sub {
109 68 ▶ my ($class, %args) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
110 68 my $self = $orig_new->($class);
111 68 for my $k (keys %args) {
112 78 ▶ my $setter = $SETTERS{$k}
Branch: 1 branch
BranchTrueFalseTotal
my $setter = $SETTERS{$k} or Carp::croak("unknown option: $k") 0 78 78
113 or Carp::croak("unknown option: $k");
114 78 $self->$setter($args{$k});
115 }
116 59 $self;
117 27 };
118 }
119  
120 # Doc overloading: stringify to JSON, boolean always true, eq/ne deep compare
121 package JSON::YY::Doc;
122 27 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 # _doc_eq only understands Docs, so compare the JSON text when the other side
125 # is not one -- otherwise $doc eq '{"a":1}' is false while "$doc" eq it is true.
126 sub _eq {
127 17 ▶ my ($self, $other) = @_;
Subroutine _eq: called CC=5 SCAR=16.1
128 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
129 if Scalar::Util::blessed($other) && $other->isa('JSON::YY::Doc');
130 # _doc_stringify returns UTF-8 bytes. The UTF8 flag cannot tell a character
131 # string from a byte string (chars < 256 may be stored either way), so
132 # accept the operand under either reading.
133 12 ▶ my $str = defined $other ? "$other" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $other ? : 11 1 12
134 12 my $json = JSON::YY::_doc_stringify($self);
135 12 ▶ return 1 if $json eq $str;
Branch: 1 branch
BranchTrueFalseTotal
if $json eq $str 5 7 12
136 7 my $enc = $str;
137 7 utf8::encode($enc);
138 7 return $json eq $enc;
139 }
140  
141 use overload
142 39 ▶ '""' => sub { JSON::YY::_doc_stringify($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
143 2 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
144 # without this `fallback` numifies via the string and every pair of Docs
145 # compares equal; identity is the only sensible number for a handle
146 6 ▶ '0+' => sub { Scalar::Util::refaddr($_[0]) },
Subroutine __ANON__: called CC=1 SCAR=0.0
147 'eq' => \&_eq,
148 6 ▶ 'ne' => sub { !_eq(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
149 27 ▶ fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 package JSON::YY;
152 27 1;
153  
154 __END__
↑ Summary yyjson.c »