| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
$flags & 12288 and not $flags & 16384
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1629 | ▶ | return defined $_[0] && _created_as_number($_[0]); | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
defined $_[0] && &_created_as_number($_[0])
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])
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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])
not ref $_[0] and defined $_[0]
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])
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 232 | ▶ | return '' unless defined $v && !ref $v && _created_as_number($v); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
defined $v and not ref $v and &_created_as_number($v)
defined $v and not ref $v
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)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 232 | ▶ | return $v == int($v) && $v - $v == 0 ? 1 : ''; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$v == int $v and $v - $v == 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$order and @$order == keys %$hash
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 114 | ▶ | if (!utf8::is_utf8($text)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | 112 | my $copy = $text; | |||||||||||||||||||||||||||||||||||||||||||||
| 111 | 112 | ▶ | if (!utf8::decode($copy) && $copy =~ /[\x80-\xff]/) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not utf8::decode($copy) and $copy =~ /[\x80-\xff]/u
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
pos $$text // 0
pos $$text
Truth table50%1/2
A
A= pos $$text
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 524 | $$text =~ /\G[ \t\r\n]*/gc; | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | 524 | ▶ | if ($$text =~ /\G"/gc) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 232 | return _string($text); | |||||||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | 292 | ▶ | if ($$text =~ /\G\{/gc) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 197 | my %object; | |||||||||||||||||||||||||||||||||||||||||||||
| 153 | my @order; | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | 197 | $$text =~ /\G[ \t\r\n]*/gc; | |||||||||||||||||||||||||||||||||||||||||||||
| 155 | 197 | ▶ | if ($$text =~ /\G\}/gc) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 8 | ▶ | set_order(\%object, []) if $ordered; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 362 | my $name = _string($text); | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | 362 | ▶ | $$text =~ /\G[ \t\r\n]*:/gc or _fail($text, 'expected :'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | 362 | ▶ | push @order, $name if $ordered && !exists $object{$name}; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ordered and not exists $object{$name}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 187 | ▶ | last if $$text =~ /\G\}/gc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | _fail($text, 'expected , or }'); | |||||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 187 | ▶ | set_order(\%object, \@order) if $ordered; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 187 | return \%object; | |||||||||||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 95 | ▶ | if ($$text =~ /\G\[/gc) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 21 | my @array; | |||||||||||||||||||||||||||||||||||||||||||||
| 176 | 21 | $$text =~ /\G[ \t\r\n]*/gc; | |||||||||||||||||||||||||||||||||||||||||||||
| 177 | 21 | ▶ | return \@array if $$text =~ /\G\]/gc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 19 | ▶ | last if $$text =~ /\G\]/gc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 50 | return _number($1, $2); | |||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 24 | ▶ | return $TRUE if $$text =~ /\Gtrue/gc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | 6 | ▶ | return $FALSE if $$text =~ /\Gfalse/gc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 192 | 2 | ▶ | return undef if $$text =~ /\Gnull/gc; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 2 | ▶ | _fail($text, pos($$text) >= length $$text ? 'unexpected end of input' : 'unexpected character'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | # The digits of the largest 64-bit values: shorter integers fit. | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 49 | ▶ | return 0 + $integer if length($integer) < 19; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
"$copy" eq $integer and $n == int $n and B::svref_2object(\$n)->FLAGS & 256
"$copy" eq $integer and $n == int $n
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 611 | $s .= $1; | |||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 615 | ▶ | if ($$text =~ /\G"/gc) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 594 | return $s; | |||||||||||||||||||||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 21 | ▶ | if ($$text =~ /\G\\/gc) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 21 | ▶ | if ($$text =~ /\G(["\\\/bfnrt])/gc) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$unit >= 55296 and $unit <= 56319 and $$text =~ /\G\\u([dD][c-fC-F][0-9a-fA-F]{2})/cgu
$unit >= 55296 and $unit <= 56319
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%1/1
$depth //= 0
$depth
Truth table100%2/2
A
A= $depth
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 267 | 588 | ▶ | return undef unless defined $data; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 586 | ▶ | if (my $r = ref $data) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 269 | 552 | ▶ | return { map { $_ => from_data($data->{$_}, $depth + 1) } keys %$data } if $r eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 534 | ▶ | return [ map { from_data($_, $depth + 1) } @$data ] if $r eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 16 | ▶ | return $data ? $TRUE : $FALSE if $r eq 'JSON::PP::Boolean'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 272 | 12 | ▶ | if ($r eq 'SCALAR' && !ref $$data && defined $$data) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$r eq "SCALAR" and not ref $$data and defined $$data
$r eq "SCALAR" and not ref $$data
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 2 | ▶ | return $TRUE if $$data eq '1'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 274 | 1 | ▶ | return $FALSE if $$data eq '0'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 10 | ▶ | if (Scalar::Util::blessed($data)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 8 | ▶ | return $data->copy if $data->isa('Math::BigInt') && $data->is_int; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$data->isa("Math::BigInt") and $data->is_int
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 5 | ▶ | if ($data->isa('Math::BigInt') || $data->isa('Math::BigFloat')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$data->isa("Math::BigInt") or $data->isa("Math::BigFloat")
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")
| |||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 3 | ▶ | Carp::croak("JSON::Schema::Corvus: the $r $data has no JSON form") if $data->is_nan || $data->is_inf; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$data->is_nan or $data->is_inf
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 280 | 2 | return _from_decimal("$data"); | |||||||||||||||||||||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 282 | 2 | ▶ | return $data ? $TRUE : $FALSE if $data->isa('JSON::PP::Boolean'); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 2 | ▶ | if (my $to_json = $data->can('TO_JSON')) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 1 | my $value = $data->$to_json; | |||||||||||||||||||||||||||||||||||||||||||||
| 285 | 1 | ▶ | Carp::croak("JSON::Schema::Corvus: ${r}::TO_JSON returned the object itself") | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $value and builtin::refaddr($value) == builtin::refaddr($data)
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)
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$flags & 12288 and not $flags & 16384
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%4/6
$copy != $copy or $copy == $copy * 2 and $copy != 0
$copy == $copy * 2 and $copy != 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$text =~ /\A-?[0-9]+\z/u and length $text >= 19
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 1 | ▶ | return 0 + $text if $text =~ /\A-?[0-9]+\z/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 36 | my $r = ref $v; | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | 36 | ▶ | if ($r eq 'HASH') { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 25 | ▶ | return $v ? 'true' : 'false' if $r eq 'JSON::PP::Boolean'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 19 | ▶ | return "$v" if $r eq 'Math::BigInt'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 16 | ▶ | if (_created_as_number($v)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$text =~ /\A-?[0-9]+\z/u and not B::svref_2object(\$v)->FLAGS & 256
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 84 | ▶ | return sprintf('%d', $n) if $n == int($n) && abs($n) < 1e15 && B::svref_2object(\$n)->FLAGS & B::SVf_IOK; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$n == int $n and abs $n < "1e+15" and B::svref_2object(\$n)->FLAGS & 256
$n == int $n and abs $n < "1e+15"
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 3 | ▶ | return 'null' if $n != $n || ($n == $n * 2 && $n != 0); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$n != $n or $n == $n * 2 and $n != 0
$n == $n * 2 and $n != 0
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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||
| 367 | : $1 eq "\r" ? '\\r' : sprintf('\\u%04x', ord $1)/ge; | ||||||||||||||||||||||||||||||||||||||||||||||
| 368 | 20 | return qq{"$s"}; | |||||||||||||||||||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||
| 371 | 5 | 1; | |||||||||||||||||||||||||||||||||||||||||||||