Corvus/Json.pm

filter: Statementstmt 93.9% 217 / 231 Branchbran 77.9% 145 / 186 Conditioncond 58.2% 53 / 91 MC/DCmcdc 36.5% 19 / 52 Subroutinesub 94.5% 35 / 37 Podpod 0.0% 0 / 11 totaltotal 77.1% 469 / 608 CC 126 SCAR 27.9
CC
126
Cov
82%
from_data
37.3
_string
22.5
_value
22.3
CRAP 16.3
« Corvus/Pattern.pm ↑ Summary Corvus/Node.pm »
1 package JSON::Schema::Corvus::Json;
2  
3 # The JSON values the evaluator works on, and a parser that produces them.
4 #
5 # A JSON value is a Perl value of one of these forms, which the parser and from_data produce and nothing else changes:
6 # null undef
7 # boolean $TRUE or $FALSE (JSON::PP::Boolean, so that JSON::PP and other modules read them as booleans)
8 # object an unblessed hash reference
9 # array an unblessed array reference
10 # number a scalar created as a number and never used as a string (an integer from an integer literal, a floating
11 # point number, created as one, from any other, so that writing it again gives 1.0 for 1.0), or a
12 # Math::BigInt for an integer beyond 64 bits
13 # string a scalar created as a string
14 # So a number is told from a string by how the scalar was created (is_number), never by what the text looks like.
15 #
16 # An object's names have no order in a Perl hash. Where the order matters (a schema's keywords, which decide the order
17 # of results, and an instance evaluated with a results collector), the parser records it (ordered mode), and names()
18 # gives the names in that order, or in sorted order for a hash with none recorded (Perl data).
19  
20 5 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 5 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Hash::Util::FieldHash ();
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Math::BigInt ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
29 # builtin::created_as_number (5.36 and later) is still experimental, and its warning category is unknown before.
30 5 ▶no if $] >= 5.036, warnings => 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 5 our $TRUE = $JSON::PP::true;
33 5 our $FALSE = $JSON::PP::false;
34  
35 # The order of an object's names, for objects parsed in ordered mode. Keyed by the hash itself: an entry goes when its
36 # hash does.
37 5 Hash::Util::FieldHash::fieldhash(my %ORDER);
38  
39 # Whether a scalar was created as a number (and has not been used as a string since), as JSON::PP decides it.
40 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.5
41 5 ▶ if (defined &builtin::created_as_number) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &builtin::created_as_number) { } 5 0 5
42 5 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 5 *_created_as_number = \&builtin::created_as_number;
44 }
45 else {
46 0 ▶ *_created_as_number = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
47 0 my $flags = B::svref_2object(\$_[0])->FLAGS;
48 0 ▶ return ($flags & (B::SVp_IOK | B::SVp_NOK)) && !($flags & B::SVp_POK) ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$flags & 12288 && !($flags & 16384) ? : 0 0 0
Condition0%0/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
000
MC/DC0%0/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table0%0/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
49 0 };
50 }
51 }
52  
53 1629 ▶sub is_number {
Subroutine is_number: called CC=3 SCAR=11.4
Pod undocumented
54 1629 my $r = ref $_[0];
55 1629 ▶ return $r eq 'Math::BigInt' if $r;
Branch: 1 branch
BranchTrueFalseTotal
if $r 0 1629 1629
56 1629 ▶ return defined $_[0] && _created_as_number($_[0]);
Condition66%2/3
defined $_[0] && &_created_as_number($_[0])
!ll&&!rl&&r
1616013
MC/DC50%1/2
defined $_[0] && &_created_as_number($_[0])
defined $_[0] &_created_as_number($_[0])
Truth table66%2/3
A && B
A= defined $_[0]B= &_created_as_number($_[0])
ABresult
0X0
100
111
57 }
58  
59 1287 ▶sub is_string {
Subroutine is_string: called CC=3 SCAR=11.7
Pod undocumented
60 1287 ▶ return !ref $_[0] && defined $_[0] && !_created_as_number($_[0]);
Condition66%4/6
!ref($_[0]) && defined $_[0] && !&_created_as_number($_[0])
!ll&&!rl&&r
11050182
not ref $_[0] and defined $_[0]
!ll&&!rl&&r
01105182
MC/DC33%1/3
!ref($_[0]) && defined $_[0] && !&_created_as_number($_[0])
not ref $_[0] defined $_[0] !&_created_as_number($_[0])
Truth table50%2/4
not(A) and B && not(C)
A= not ref $_[0]B= defined $_[0]C= !&_created_as_number($_[0])
ABCresult
0XX0
10X0
1100
1111
61 }
62  
63 312 ▶sub is_bool { ref $_[0] eq 'JSON::PP::Boolean' }
Subroutine is_bool: called CC=1 SCAR=0.0
Pod undocumented
64  
65 # Whether a number is an integer: a whole number, in every draft. (Draft 4 says an integer has no fraction or exponent
66 # in its text, but Perl's numeric operations change the flags that would tell 1.0 from 1, so a parsed document could
67 # answer differently the second time; the C library, and so JSON::Schema::Corvus::XS, takes 1.0 as an integer in every
68 # draft too.)
69 232 ▶sub is_integer {
Subroutine is_integer: called CC=7 SCAR=25.0
Pod undocumented
70 232 my ($v) = @_;
71 232 ▶ return 1 if ref $v eq 'Math::BigInt';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "Math::BigInt" 0 232 232
72 232 ▶ return '' unless defined $v && !ref $v && _created_as_number($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and not ref $v and &_created_as_number($v) 0 232 232
Condition33%2/6
defined $v and not ref $v and &_created_as_number($v)
!ll&&!rl&&r
00232
defined $v and not ref $v
!ll&&!rl&&r
00232
MC/DC0%0/3
defined $v and not ref $v and &_created_as_number($v)
defined $v not ref $v &_created_as_number($v)
Truth table25%1/4
A and not(B) and C
A= defined $vB= not ref $vC= &_created_as_number($v)
ABCresult
0XX0
10X0
1100
1111
73 232 ▶ return $v == int($v) && $v - $v == 0 ? 1 : '';
Branch: 1 branch
BranchTrueFalseTotal
$v == int $v && $v - $v == 0 ? : 232 0 232
Condition33%1/3
$v == int $v and $v - $v == 0
!ll&&!rl&&r
00232
MC/DC0%0/2
$v == int $v and $v - $v == 0
$v == int $v $v - $v == 0
Truth table33%1/3
A and B
A= $v == int $vB= $v - $v == 0
ABresult
0X0
100
111
74 }
75  
76 # The JSON type of a value: null, boolean, object, array, number or string.
77 70 ▶sub type_of {
Subroutine type_of: called CC=8 SCAR=23.0
Pod undocumented
78 70 my ($v) = @_;
79 70 ▶ return 'null' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 70 70
80 70 my $r = ref $v;
81 70 ▶ return $r eq 'HASH' ? 'object' : $r eq 'ARRAY' ? 'array' : $r eq 'JSON::PP::Boolean' ? 'boolean'
Branch: 5 branches
BranchTrueFalseTotal
if $r 11 59 70
$r eq 'HASH' ? : 9 2 11
$r eq 'ARRAY' ? : 2 0 2
$r eq 'JSON::PP::Boolean' ? : 0 0 0
$r eq 'Math::BigInt' ? : 0 0 0
82 : $r eq 'Math::BigInt' ? 'number' : Carp::confess("not a JSON value: $r") if $r;
83 59 ▶ return _created_as_number($v) ? 'number' : 'string';
Branch: 1 branch
BranchTrueFalseTotal
&_created_as_number($v) ? : 10 49 59
84 }
85  
86 # An object's names, in the order the text gave them (ordered mode), or sorted.
87 201 ▶sub names {
Subroutine names: called CC=3 SCAR=11.0
Pod undocumented
88 201 my ($hash) = @_;
89 201 my $order = $ORDER{$hash};
90 201 ▶ return $order && @$order == keys %$hash ? @$order : sort keys %$hash;
Branch: 1 branch
BranchTrueFalseTotal
$order && @$order == keys %$hash ? : 185 16 201
Condition66%2/3
$order and @$order == keys %$hash
!ll&&!rl&&r
160185
MC/DC50%1/2
$order and @$order == keys %$hash
$order @$order == keys %$hash
Truth table66%2/3
A and B
A= $orderB= @$order == keys %$hash
ABresult
0X0
100
111
91 }
92  
93 174 ▶sub set_order {
Subroutine set_order: called CC=1 SCAR=0.0
Pod undocumented
94 174 my ($hash, $names) = @_;
95 174 $ORDER{$hash} = $names;
96 174 return $hash;
97 }
98  
99 # --- The parser ---
100  
101 5 my %ESCAPES = ('"' => '"', '\\' => '\\', '/' => '/', b => "\b", f => "\f", n => "\n", r => "\r", t => "\t");
102  
103 # Parses JSON text into a JSON value. The text is characters or UTF-8 bytes (a string with the UTF8 flag off is
104 # decoded as UTF-8). Dies with a JSON::Schema::Corvus::Error::InvalidJson for text that is not JSON, or not UTF-8.
105 # With $ordered, objects keep the order of their names (see names).
106 115 ▶sub decode {
Subroutine decode: called CC=6 SCAR=18.0
Pod undocumented
107 115 my ($text, $ordered) = @_;
108 115 ▶ JSON::Schema::Corvus::Error->throw_usage('the text is undef') unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 114 115
109 114 ▶ if (!utf8::is_utf8($text)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (utf8::is_utf8($text)) 112 2 114
110 112 my $copy = $text;
111 112 ▶ if (!utf8::decode($copy) && $copy =~ /[\x80-\xff]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not utf8::decode($copy) and $copy =~ /[\x80-\xff]/u) 1 111 112
Condition66%2/3
not utf8::decode($copy) and $copy =~ /[\x80-\xff]/u
!ll&&!rl&&r
11101
MC/DC50%1/2
not utf8::decode($copy) and $copy =~ /[\x80-\xff]/u
not utf8::decode($copy) $copy =~ /[\x80-\xff]/u
Truth table66%2/3
not(A) and B
A= not utf8::decode($copy)B= $copy =~ /[\x80-\xff]/u
ABresult
0X0
100
111
112 1 _invalid_utf8($text);
113 }
114 111 $text = $copy;
115 }
116 113 pos($text) = 0;
117 113 my $value = _value(\$text, $ordered, 0);
118 109 $text =~ /\G[ \t\r\n]*/gc;
119 109 ▶ _fail(\$text, 'unexpected text after the value') if pos($text) < length $text;
Branch: 1 branch
BranchTrueFalseTotal
if pos $text < length $text 0 109 109
120 109 return $value;
121 }
122  
123 1 ▶sub _invalid_utf8 {
Subroutine _invalid_utf8: called CC=2 SCAR=6.9
124 1 my ($bytes) = @_;
125 # The offset of the first byte that does not start or continue a valid UTF-8 sequence.
126 1 my $offset = 0;
127 1 ▶ while ($bytes =~ /\G(?:[\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec\xee\xef][\x80-\xbf]{2}
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes =~ /\G(?:[\x00-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xec\xee\xef][\x80-\xbf]{2} |\xed[\x80-\x9f][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|\xf4[\x80-\x8f][\x80-\xbf]{2})/cgux) 1 1 2
128 |\xed[\x80-\x9f][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf]{2}|[\xf1-\xf3][\x80-\xbf]{3}|\xf4[\x80-\x8f][\x80-\xbf]{2})/gcx) {
129 1 $offset = pos $bytes;
130 }
131 1 die JSON::Schema::Corvus::Error::InvalidJson->_new("invalid UTF-8 at byte $offset", $offset);
132 }
133  
134 4 ▶sub _fail {
Subroutine _fail: called CC=2 SCAR=7.0
135 4 my ($text, $what) = @_;
136 4 ▶ my $at = pos($$text) // 0;
Condition50%1/2
pos $$text // 0
l!l
40
MC/DC0%0/1
pos $$text // 0
pos $$text
Truth table50%1/2
A
A= pos $$text
Aresult
11
00
137 # The offset in UTF-8 bytes, as the C library reports it.
138 4 my $prefix = substr($$text, 0, $at);
139 4 utf8::encode($prefix);
140 4 my $offset = length $prefix;
141 4 die JSON::Schema::Corvus::Error::InvalidJson->_new("invalid JSON: $what at byte $offset", $offset);
142 }
143  
144 524 ▶sub _value {
Subroutine _value: called CC=22 SCAR=31.0
145 524 my ($text, $ordered, $depth) = @_;
146 524 ▶ _fail($text, 'the value is nested too deeply') if $depth > 10000;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 10000 0 524 524
147 524 $$text =~ /\G[ \t\r\n]*/gc;
148 524 ▶ if ($$text =~ /\G"/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G"/cgu) 232 292 524
149 232 return _string($text);
150 }
151 292 ▶ if ($$text =~ /\G\{/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\{/cgu) 197 95 292
152 197 my %object;
153 my @order;
154 197 $$text =~ /\G[ \t\r\n]*/gc;
155 197 ▶ if ($$text =~ /\G\}/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\}/cgu) 8 189 197
156 8 ▶ set_order(\%object, []) if $ordered;
Branch: 1 branch
BranchTrueFalseTotal
if $ordered 5 3 8
157 8 return \%object;
158 }
159 189 while (1) {
160 363 $$text =~ /\G[ \t\r\n]*/gc;
161 363 ▶ $$text =~ /\G"/gc or _fail($text, 'expected a name');
Branch: 1 branch
BranchTrueFalseTotal
$$text =~ /\G"/cgu or _fail($text, "expected a name") 362 1 363
162 362 my $name = _string($text);
163 362 ▶ $$text =~ /\G[ \t\r\n]*:/gc or _fail($text, 'expected :');
Branch: 1 branch
BranchTrueFalseTotal
$$text =~ /\G[ \t\r\n]*:/cgu or _fail($text, "expected :") 362 0 362
164 362 ▶ push @order, $name if $ordered && !exists $object{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $ordered and not exists $object{$name} 341 21 362
Condition66%2/3
$ordered and not exists $object{$name}
!ll&&!rl&&r
210341
MC/DC50%1/2
$ordered and not exists $object{$name}
$ordered not exists $object{$name}
Truth table66%2/3
A and not(B)
A= $orderedB= not exists $object{$name}
ABresult
0X0
100
111
165 362 $object{$name} = _value($text, $ordered, $depth + 1);
166 361 $$text =~ /\G[ \t\r\n]*/gc;
167 361 ▶ next if $$text =~ /\G,/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G,/cgu 174 187 361
168 187 ▶ last if $$text =~ /\G\}/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G\}/cgu 187 0 187
169 0 _fail($text, 'expected , or }');
170 }
171 187 ▶ set_order(\%object, \@order) if $ordered;
Branch: 1 branch
BranchTrueFalseTotal
if $ordered 169 18 187
172 187 return \%object;
173 }
174 95 ▶ if ($$text =~ /\G\[/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\[/cgu) 21 74 95
175 21 my @array;
176 21 $$text =~ /\G[ \t\r\n]*/gc;
177 21 ▶ return \@array if $$text =~ /\G\]/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G\]/cgu 1 20 21
178 20 while (1) {
179 49 push @array, _value($text, $ordered, $depth + 1);
180 48 $$text =~ /\G[ \t\r\n]*/gc;
181 48 ▶ next if $$text =~ /\G,/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G,/cgu 29 19 48
182 19 ▶ last if $$text =~ /\G\]/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\G\]/cgu 18 1 19
183 1 _fail($text, 'expected , or ]');
184 }
185 18 return \@array;
186 }
187 74 ▶ if ($$text =~ /\G(-?(?:0|[1-9][0-9]*))((?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G(-?(?:0|[1-9][0-9]*))((?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?)/cgu) 50 24 74
188 50 return _number($1, $2);
189 }
190 24 ▶ return $TRUE if $$text =~ /\Gtrue/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\Gtrue/cgu 18 6 24
191 6 ▶ return $FALSE if $$text =~ /\Gfalse/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\Gfalse/cgu 4 2 6
192 2 ▶ return undef if $$text =~ /\Gnull/gc;
Branch: 1 branch
BranchTrueFalseTotal
if $$text =~ /\Gnull/cgu 0 2 2
193 2 ▶ _fail($text, pos($$text) >= length $$text ? 'unexpected end of input' : 'unexpected character');
Branch: 1 branch
BranchTrueFalseTotal
pos $$text >= length $$text ? : 2 0 2
194 }
195  
196 # A number from its integer part and the rest (a fraction and an exponent). An integer literal is an integer (a
197 # Math::BigInt beyond 64 bits); any other is a floating point number, created as one even when it is whole.
198 50 ▶sub _number {
Subroutine _number: called CC=6 SCAR=18.0
199 50 my ($integer, $rest) = @_;
200 50 ▶ if ($rest eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "") 49 1 50
201 # The digits of the largest 64-bit values: shorter integers fit.
202 49 ▶ return 0 + $integer if length($integer) < 19;
Branch: 1 branch
BranchTrueFalseTotal
if length $integer < 19 43 6 49
203 6 my $n = 0 + $integer;
204 # The digits are compared through a copy: making a string of $n itself would make it a string to is_number.
205 6 my $copy = $n;
206 6 ▶ return $n if "$copy" eq $integer && $n == int($n) && B::svref_2object(\$n)->FLAGS & B::SVf_IOK;
Branch: 1 branch
BranchTrueFalseTotal
if "$copy" eq $integer and $n == int $n and B::svref_2object(\$n)->FLAGS & 256 5 1 6
Condition66%4/6
"$copy" eq $integer and $n == int $n and B::svref_2object(\$n)->FLAGS & 256
!ll&&!rl&&r
105
"$copy" eq $integer and $n == int $n
!ll&&!rl&&r
105
MC/DC33%1/3
"$copy" eq $integer and $n == int $n and B::svref_2object(\$n)->FLAGS & 256
"$copy" eq $integer $n == int $n B::svref_2object(\$n)->FLAGS & 256
Truth table50%2/4
A and B and C
A= "$copy" eq $integerB= $n == int $nC= B::svref_2object(\$n)->FLAGS & 256
ABCresult
0XX0
10X0
1100
1111
207 1 return Math::BigInt->new($integer);
208 }
209 1 return to_double($integer . $rest);
210 }
211  
212 # A decimal number's text as the nearest double, created as a floating point number. Perl reads decimal text correctly
213 # rounded from 5.30; before that, a long mantissa can come back as a neighbouring double, so the C library's strtod
214 # reads it there (Perl keeps the numeric locale of C for it outside 'use locale').
215 5 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=9.2
216 5 ▶ if ($] >= 5.030) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.03) { } 5 0 5
217 5 ▶ *to_double = sub { unpack('d', pack('d', $_[0])) };
Subroutine __ANON__: called CC=1 SCAR=0.0
218 }
219 else {
220 0 require POSIX;
221 0 ▶ *to_double = sub { (POSIX::strtod($_[0]))[0] };
Subroutine __ANON__: not called CC=1 SCAR=6.9
222 }
223 }
224  
225 594 ▶sub _string {
Subroutine _string: called CC=10 SCAR=31.1
226 594 my ($text) = @_;
227 594 my $s = '';
228 594 while (1) {
229 615 ▶ if ($$text =~ /\G([^"\\\x00-\x1f]+)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G([^"\\\x00-\x1f]+)/cgu) 611 4 615
230 611 $s .= $1;
231 }
232 615 ▶ if ($$text =~ /\G"/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G"/cgu) 594 21 615
233 594 return $s;
234 }
235 21 ▶ if ($$text =~ /\G\\/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$text =~ /\G\\/cgu) 21 0 21
236 21 ▶ if ($$text =~ /\G(["\\\/bfnrt])/gc) {
Branch: 2 branches
BranchTrueFalseTotal
if ($$text =~ m[\G(["\\/bfnrt])]cgu) { } 21 0 21
elsif ($$text =~ /\Gu([0-9a-fA-F]{4})/cgu) { } 0 0 0
237 21 $s .= $ESCAPES{$1};
238 }
239 elsif ($$text =~ /\Gu([0-9a-fA-F]{4})/gc) {
240 0 my $unit = hex $1;
241 0 ▶ if ($unit >= 0xd800 && $unit <= 0xdbff && $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit >= 55296 and $unit <= 56319 and $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu) { } 0 0 0
Condition0%0/6
$unit >= 55296 and $unit <= 56319 and $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu
!ll&&!rl&&r
000
$unit >= 55296 and $unit <= 56319
!ll&&!rl&&r
000
MC/DC0%0/3
$unit >= 55296 and $unit <= 56319 and $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu
$unit >= 55296 $unit <= 56319 $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu
Truth table0%0/4
A and B and C
A= $unit >= 55296B= $unit <= 56319C= $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu
ABCresult
0XX0
10X0
1100
1111
242 0 $s .= chr(0x10000 + (($unit - 0xd800) << 10) + (hex($1) - 0xdc00));
243 }
244 else {
245 # A lone surrogate is kept as its code point, as JavaScript keeps it.
246 5 ▶ no warnings 'surrogate';
Subroutine BEGIN: called CC=1 SCAR=0.0
247 0 $s .= chr $unit;
248 }
249 }
250 else {
251 0 _fail($text, 'invalid escape');
252 }
253 21 next;
254 }
255 0 ▶ _fail($text, pos($$text) >= length $$text ? 'unterminated string' : 'control character in a string');
Branch: 1 branch
BranchTrueFalseTotal
pos $$text >= length $$text ? : 0 0 0
256 }
257 }
258  
259 # --- Perl data ---
260  
261 # Converts Perl data into a JSON value, by the rules JSON::PP follows (see JSON::Schema::Corvus): a copy, in which
262 # every scalar has the form above. Dies for data that has no JSON form.
263 589 ▶sub from_data {
Subroutine from_data: called CC=35 SCAR=36.2
Pod undocumented
264 589 my ($data, $depth) = @_;
265 589 ▶ $depth //= 0;
Condition100%2/2
$depth //= 0
l!l
54742
MC/DC100%1/1
$depth //= 0
$depth
Truth table100%2/2
A
A= $depth
Aresult
11
00
266 589 ▶ Carp::croak('JSON::Schema::Corvus: the data is nested more than 512 deep (or holds a cycle)') if $depth > 512;
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 512 1 588 589
267 588 ▶ return undef unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 2 586 588
268 586 ▶ if (my $r = ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = ref $data) 552 34 586
269 552 ▶ return { map { $_ => from_data($data->{$_}, $depth + 1) } keys %$data } if $r eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "HASH" 18 534 552
270 534 ▶ return [ map { from_data($_, $depth + 1) } @$data ] if $r eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "ARRAY" 518 16 534
271 16 ▶ return $data ? $TRUE : $FALSE if $r eq 'JSON::PP::Boolean';
Branch: 2 branches
BranchTrueFalseTotal
if $r eq "JSON::PP::Boolean" 4 12 16
$data ? : 2 2 4
272 12 ▶ if ($r eq 'SCALAR' && !ref $$data && defined $$data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "SCALAR" and not ref $$data and defined $$data) 2 10 12
Condition66%4/6
$r eq "SCALAR" and not ref $$data and defined $$data
!ll&&!rl&&r
1002
$r eq "SCALAR" and not ref $$data
!ll&&!rl&&r
1002
MC/DC33%1/3
$r eq "SCALAR" and not ref $$data and defined $$data
$r eq "SCALAR" not ref $$data defined $$data
Truth table50%2/4
A and not(B) and C
A= $r eq "SCALAR"B= not ref $$dataC= defined $$data
ABCresult
0XX0
10X0
1100
1111
273 2 ▶ return $TRUE if $$data eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $$data eq 1 1 1 2
274 1 ▶ return $FALSE if $$data eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $$data eq 0 1 0 1
275 }
276 10 ▶ if (Scalar::Util::blessed($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($data)) 8 2 10
277 8 ▶ return $data->copy if $data->isa('Math::BigInt') && $data->is_int;
Branch: 1 branch
BranchTrueFalseTotal
if $data->isa("Math::BigInt") and $data->is_int 3 5 8
Condition100%3/3
$data->isa("Math::BigInt") and $data->is_int
!ll&&!rl&&r
413
MC/DC100%2/2
$data->isa("Math::BigInt") and $data->is_int
$data->isa("Math::BigInt") $data->is_int
Truth table100%3/3
A and B
A= $data->isa("Math::BigInt")B= $data->is_int
ABresult
0X0
100
111
278 5 ▶ if ($data->isa('Math::BigInt') || $data->isa('Math::BigFloat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa("Math::BigInt") or $data->isa("Math::BigFloat")) 3 2 5
Condition100%3/3
$data->isa("Math::BigInt") or $data->isa("Math::BigFloat")
l!l&&r!l&&!r
122
MC/DC100%2/2
$data->isa("Math::BigInt") or $data->isa("Math::BigFloat")
$data->isa("Math::BigInt") $data->isa("Math::BigFloat")
Truth table100%3/3
A or B
A= $data->isa("Math::BigInt")B= $data->isa("Math::BigFloat")
ABresult
1X1
011
000
279 3 ▶ Carp::croak("JSON::Schema::Corvus: the $r $data has no JSON form") if $data->is_nan || $data->is_inf;
Branch: 1 branch
BranchTrueFalseTotal
if $data->is_nan or $data->is_inf 1 2 3
Condition66%2/3
$data->is_nan or $data->is_inf
l!l&&r!l&&!r
012
MC/DC50%1/2
$data->is_nan or $data->is_inf
$data->is_nan $data->is_inf
Truth table66%2/3
A or B
A= $data->is_nanB= $data->is_inf
ABresult
1X1
011
000
280 2 return _from_decimal("$data");
281 }
282 2 ▶ return $data ? $TRUE : $FALSE if $data->isa('JSON::PP::Boolean');
Branch: 2 branches
BranchTrueFalseTotal
if $data->isa("JSON::PP::Boolean") 0 2 2
$data ? : 0 0 0
283 2 ▶ if (my $to_json = $data->can('TO_JSON')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $to_json = $data->can("TO_JSON")) 1 1 2
284 1 my $value = $data->$to_json;
285 1 ▶ Carp::croak("JSON::Schema::Corvus: ${r}::TO_JSON returned the object itself")
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and builtin::refaddr($value) == builtin::refaddr($data) 0 1 1
Condition33%1/3
ref $value and builtin::refaddr($value) == builtin::refaddr($data)
!ll&&!rl&&r
010
MC/DC0%0/2
ref $value and builtin::refaddr($value) == builtin::refaddr($data)
ref $value builtin::refaddr($value) == builtin::refaddr($data)
Truth table33%1/3
A and B
A= ref $valueB= builtin::refaddr($value) == builtin::refaddr($data)
ABresult
0X0
100
111
286 if ref $value && Scalar::Util::refaddr($value) == Scalar::Util::refaddr($data);
287 1 return from_data($value, $depth + 1);
288 }
289 1 Carp::croak("JSON::Schema::Corvus: an object of class $r has no JSON form (it has no TO_JSON method)");
290 }
291 2 Carp::croak('JSON::Schema::Corvus: a reference that is not to a hash or an array has no JSON form '
292 . '(\1 and \0 are true and false)');
293 }
294 34 ▶ if (defined &builtin::is_bool) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &builtin::is_bool) 34 0 34
295 5 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
296 34 ▶ return $data ? $TRUE : $FALSE if builtin::is_bool($data);
Branch: 2 branches
BranchTrueFalseTotal
if builtin::is_bool($data) 2 32 34
$data ? : 1 1 2
297 }
298 # A number by JSON::PP's rule: a scalar with a number and no string, by its flags (builtin::created_as_number says
299 # number for a number that has since been used as a string, which JSON::PP writes as a string).
300 32 my $flags = B::svref_2object(\$data)->FLAGS;
301 32 ▶ if (($flags & (B::SVp_IOK | B::SVp_NOK)) && !($flags & B::SVp_POK)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 12288 and not $flags & 16384) 14 18 32
Condition100%3/3
$flags & 12288 and not $flags & 16384
!ll&&!rl&&r
17114
MC/DC100%2/2
$flags & 12288 and not $flags & 16384
$flags & 12288 not $flags & 16384
Truth table100%3/3
A and not(B)
A= $flags & 12288B= not $flags & 16384
ABresult
0X0
100
111
302 14 my $copy = $data;
303 14 ▶ Carp::croak('JSON::Schema::Corvus: ' . ($copy != $copy ? 'NaN' : 'an infinite number') . ' has no JSON form')
Branch: 2 branches
BranchTrueFalseTotal
if $copy != $copy or $copy == $copy * 2 and $copy != 0 1 13 14
$copy != $copy ? : 0 1 1
Condition66%4/6
$copy != $copy or $copy == $copy * 2 and $copy != 0
l!l&&r!l&&!r
0113
$copy == $copy * 2 and $copy != 0
!ll&&!rl&&r
1301
MC/DC33%1/3
$copy != $copy or $copy == $copy * 2 and $copy != 0
$copy != $copy $copy == $copy * 2 $copy != 0
Truth table50%2/4
A or B and C
A= $copy != $copyB= $copy == $copy * 2C= $copy != 0
ABCresult
1XX1
0111
00X0
0100
304 if $copy != $copy || ($copy == $copy * 2 && $copy != 0);
305 # A plain copy, which keeps how the number was created (arithmetic would make a whole floating point number an
306 # integer).
307 13 return $copy;
308 }
309 18 return "$data";
310 }
311  
312 # A number from its decimal text (a Math::BigFloat's): an integer when it has no fraction, a floating point number
313 # otherwise.
314 2 ▶sub _from_decimal {
Subroutine _from_decimal: called CC=4 SCAR=14.1
315 2 my ($text) = @_;
316 2 ▶ return Math::BigInt->new($text) if $text =~ /\A-?[0-9]+\z/ && length($text) >= 19;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A-?[0-9]+\z/u and length $text >= 19 1 1 2
Condition66%2/3
$text =~ /\A-?[0-9]+\z/u and length $text >= 19
!ll&&!rl&&r
101
MC/DC50%1/2
$text =~ /\A-?[0-9]+\z/u and length $text >= 19
$text =~ /\A-?[0-9]+\z/u length $text >= 19
Truth table66%2/3
A and B
A= $text =~ /\A-?[0-9]+\z/uB= length $text >= 19
ABresult
0X0
100
111
317 1 ▶ return 0 + $text if $text =~ /\A-?[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A-?[0-9]+\z/u 0 1 1
318 1 return to_double($text);
319 }
320  
321 # --- Writing JSON text ---
322  
323 # The JSON text of a value: names in the order names() gives (canonical for Perl data), UTF-8 characters (not bytes).
324 38 ▶sub encode {
Subroutine encode: called CC=10 SCAR=23.0
Pod undocumented
325 38 my ($v) = @_;
326 38 ▶ return 'null' unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 2 36 38
327 36 my $r = ref $v;
328 36 ▶ if ($r eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq "HASH") 8 28 36
329 8 return '{' . join(',', map { _quote($_) . ':' . encode($v->{$_}) } names($v)) . '}';
330 }
331 28 ▶ return '[' . join(',', map { encode($_) } @$v) . ']' if $r eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "ARRAY" 3 25 28
332 25 ▶ return $v ? 'true' : 'false' if $r eq 'JSON::PP::Boolean';
Branch: 2 branches
BranchTrueFalseTotal
if $r eq "JSON::PP::Boolean" 6 19 25
$v ? : 3 3 6
333 19 ▶ return "$v" if $r eq 'Math::BigInt';
Branch: 1 branch
BranchTrueFalseTotal
if $r eq "Math::BigInt" 3 16 19
334 16 ▶ if (_created_as_number($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&_created_as_number($v)) 9 7 16
335 9 my $text = number_text($v);
336 # A floating point number that is whole keeps a fraction, so that reading the text again gives a floating point
337 # number (as Python's json writes it).
338 9 ▶ $text .= '.0' if $text =~ /\A-?[0-9]+\z/ && !(B::svref_2object(\$v)->FLAGS & B::SVf_IOK);
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A-?[0-9]+\z/u and not B::svref_2object(\$v)->FLAGS & 256 0 9 9
Condition66%2/3
$text =~ /\A-?[0-9]+\z/u and not B::svref_2object(\$v)->FLAGS & 256
!ll&&!rl&&r
270
MC/DC0%0/2
$text =~ /\A-?[0-9]+\z/u and not B::svref_2object(\$v)->FLAGS & 256
$text =~ /\A-?[0-9]+\z/u not B::svref_2object(\$v)->FLAGS & 256
Truth table66%2/3
A and not(B)
A= $text =~ /\A-?[0-9]+\z/uB= not B::svref_2object(\$v)->FLAGS & 256
ABresult
0X0
100
111
339 9 return $text;
340 }
341 7 return _quote($v);
342 }
343  
344 # The text of a number: the shortest that reads back as the same double (as JavaScript and Python write it).
345 84 ▶sub number_text {
Subroutine number_text: called CC=10 SCAR=25.1
Pod undocumented
346 84 my ($n) = @_;
347 84 ▶ return "$n" if ref $n;
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 0 84 84
348 84 ▶ return sprintf('%d', $n) if $n == int($n) && abs($n) < 1e15 && B::svref_2object(\$n)->FLAGS & B::SVf_IOK;
Branch: 1 branch
BranchTrueFalseTotal
if $n == int $n and abs $n < "1e+15" and B::svref_2object(\$n)->FLAGS & 256 81 3 84
Condition83%5/6
$n == int $n and abs $n < "1e+15" and B::svref_2object(\$n)->FLAGS & 256
!ll&&!rl&&r
3081
$n == int $n and abs $n < "1e+15"
!ll&&!rl&&r
2181
MC/DC66%2/3
$n == int $n and abs $n < "1e+15" and B::svref_2object(\$n)->FLAGS & 256
$n == int $n abs $n < "1e+15" B::svref_2object(\$n)->FLAGS & 256
Truth table75%3/4
A and B and C
A= $n == int $nB= abs $n < "1e+15"C= B::svref_2object(\$n)->FLAGS & 256
ABCresult
0XX0
10X0
1100
1111
349 3 ▶ return 'null' if $n != $n || ($n == $n * 2 && $n != 0);
Branch: 1 branch
BranchTrueFalseTotal
if $n != $n or $n == $n * 2 and $n != 0 0 3 3
Condition33%2/6
$n != $n or $n == $n * 2 and $n != 0
l!l&&r!l&&!r
003
$n == $n * 2 and $n != 0
!ll&&!rl&&r
300
MC/DC0%0/3
$n != $n or $n == $n * 2 and $n != 0
$n != $n $n == $n * 2 $n != 0
Truth table25%1/4
A or B and C
A= $n != $nB= $n == $n * 2C= $n != 0
ABCresult
1XX1
0111
00X0
0100
350 3 for my $precision (15 .. 17) {
351 5 my $text = sprintf("%.${precision}g", $n);
352 5 ▶ return _tidy($text) if to_double($text) == $n;
Branch: 1 branch
BranchTrueFalseTotal
if to_double($text) == $n 3 2 5
353 }
354 0 return _tidy(sprintf('%.17g', $n));
355 }
356  
357 3 ▶sub _tidy {
Subroutine _tidy: called CC=1 SCAR=0.0
358 3 my ($text) = @_;
359 3 $text =~ s/e([+-])0*([0-9])/e$1$2/;
360 3 $text =~ s/e\+/e+/;
361 3 return $text;
362 }
363  
364 20 ▶sub _quote {
Subroutine _quote: called CC=6 SCAR=18.1
365 20 my ($s) = @_;
366 20 ▶ $s =~ s/(["\\\x00-\x1f])/$1 eq '"' ? '\\"' : $1 eq '\\' ? '\\\\' : $1 eq "\n" ? '\\n' : $1 eq "\t" ? '\\t'
Branch: 5 branches
BranchTrueFalseTotal
$1 eq '"' ? : 1 3 4
$1 eq '\\' ? : 1 2 3
$1 eq "\n" ? : 0 2 2
$1 eq "\t" ? : 1 1 2
$1 eq "\r" ? : 0 1 1
367 : $1 eq "\r" ? '\\r' : sprintf('\\u%04x', ord $1)/ge;
368 20 return qq{"$s"};
369 }
370  
371 5 1;
« Corvus/Pattern.pm ↑ Summary Corvus/Node.pm »