| 1 | package STRling::StrictJSON; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use JSON::PP (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | # STRling-public-arity: encode=1 | ||||||||||||||||||||||||||||
| 9 | 0 | ▶ | sub encode { | ||||||||||||||||||||||||||
|
Subroutine
encode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 10 | 0 | my ($value) = @_; | |||||||||||||||||||||||||||
| 11 | 0 | return JSON::PP->new->utf8(1)->canonical(1)->allow_nonref(1)->encode($value); | |||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | # STRling-public-arity: decode_object=1 | ||||||||||||||||||||||||||||
| 15 | 0 | ▶ | sub decode_object { | ||||||||||||||||||||||||||
|
Subroutine
decode_object: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 0 | my ($text) = @_; | |||||||||||||||||||||||||||
| 17 | 0 | my $offset = 0; | |||||||||||||||||||||||||||
| 18 | 0 | _scan_value($text, \$offset); | |||||||||||||||||||||||||||
| 19 | 0 | _skip_whitespace($text, \$offset); | |||||||||||||||||||||||||||
| 20 | 0 | ▶ | die "trailing JSON content" if $offset != length($text); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | 0 | my $value = JSON::PP->new->utf8(1)->allow_nonref(1)->decode($text); | |||||||||||||||||||||||||||
| 22 | 0 | ▶ | die "interop response must be an object" unless ref($value) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 0 | return $value; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub _scan_value { | ||||||||||||||||||||||||||
|
Subroutine
_scan_value: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 27 | 0 | my ($text, $offset) = @_; | |||||||||||||||||||||||||||
| 28 | 0 | _skip_whitespace($text, $offset); | |||||||||||||||||||||||||||
| 29 | 0 | my $char = substr($text, $$offset, 1); | |||||||||||||||||||||||||||
| 30 | 0 | ▶ | return _scan_object($text, $offset) if $char eq '{'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | return _scan_array($text, $offset) if $char eq '['; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | return _scan_string($text, $offset) if $char eq '"'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 0 | my $remaining = substr($text, $$offset); | |||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ($remaining =~ /\A(?:true|false|null)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | $$offset += length($&); | |||||||||||||||||||||||||||
| 36 | 0 | return; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ($remaining =~ /\A-?(?:0|[1-9][0-9]*)(?:\.[0-9]+)?(?:[eE][+-]?[0-9]+)?/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | 0 | $$offset += length($&); | |||||||||||||||||||||||||||
| 40 | 0 | return; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | 0 | die "invalid JSON token at byte $$offset"; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub _scan_object { | ||||||||||||||||||||||||||
|
Subroutine
_scan_object: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 46 | 0 | my ($text, $offset) = @_; | |||||||||||||||||||||||||||
| 47 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 48 | 0 | _skip_whitespace($text, $offset); | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | if (substr($text, $$offset, 1) eq '}') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 51 | 0 | return; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | 0 | my %keys; | |||||||||||||||||||||||||||
| 54 | 0 | while (1) { | |||||||||||||||||||||||||||
| 55 | 0 | _skip_whitespace($text, $offset); | |||||||||||||||||||||||||||
| 56 | 0 | my $literal = _scan_string($text, $offset); | |||||||||||||||||||||||||||
| 57 | 0 | my $key = JSON::PP->new->utf8(1)->allow_nonref(1)->decode($literal); | |||||||||||||||||||||||||||
| 58 | 0 | ▶ | die "duplicate property $literal" if exists $keys{$key}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | $keys{$key} = 1; | |||||||||||||||||||||||||||
| 60 | 0 | _skip_whitespace($text, $offset); | |||||||||||||||||||||||||||
| 61 | 0 | ▶ | die "expected colon at byte $$offset" unless substr($text, $$offset, 1) eq ':'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 63 | 0 | _scan_value($text, $offset); | |||||||||||||||||||||||||||
| 64 | 0 | _skip_whitespace($text, $offset); | |||||||||||||||||||||||||||
| 65 | 0 | my $delimiter = substr($text, $$offset, 1); | |||||||||||||||||||||||||||
| 66 | 0 | ▶ | if ($delimiter eq '}') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 68 | 0 | return; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | die "expected object delimiter at byte $$offset" unless $delimiter eq ','; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub _scan_array { | ||||||||||||||||||||||||||
|
Subroutine
_scan_array: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 76 | 0 | my ($text, $offset) = @_; | |||||||||||||||||||||||||||
| 77 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 78 | 0 | _skip_whitespace($text, $offset); | |||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (substr($text, $$offset, 1) eq ']') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 81 | 0 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 0 | while (1) { | |||||||||||||||||||||||||||
| 84 | 0 | _scan_value($text, $offset); | |||||||||||||||||||||||||||
| 85 | 0 | _skip_whitespace($text, $offset); | |||||||||||||||||||||||||||
| 86 | 0 | my $delimiter = substr($text, $$offset, 1); | |||||||||||||||||||||||||||
| 87 | 0 | ▶ | if ($delimiter eq ']') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 89 | 0 | return; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | die "expected array delimiter at byte $$offset" unless $delimiter eq ','; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | sub _scan_string { | ||||||||||||||||||||||||||
|
Subroutine
_scan_string: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||
| 97 | 0 | my ($text, $offset) = @_; | |||||||||||||||||||||||||||
| 98 | 0 | my $start = $$offset; | |||||||||||||||||||||||||||
| 99 | 0 | ▶ | die "expected string at byte $$offset" unless substr($text, $$offset, 1) eq '"'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 101 | 0 | ▶ | while ($$offset < length($text)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | my $char = substr($text, $$offset, 1); | |||||||||||||||||||||||||||
| 103 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | return substr($text, $start, $$offset - $start) if $char eq '"'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | die "unescaped control character in string" if ord($char) < 0x20; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | next unless $char eq '\\'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 0 | my $escape = substr($text, $$offset, 1); | |||||||||||||||||||||||||||
| 108 | 0 | ++$$offset; | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if ($escape eq 'u') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 0 | my $hex = substr($text, $$offset, 4); | |||||||||||||||||||||||||||
| 111 | 0 | ▶ | die "invalid Unicode escape" unless $hex =~ /\A[0-9a-fA-F]{4}\z/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 0 | $$offset += 4; | |||||||||||||||||||||||||||
| 113 | } else { | ||||||||||||||||||||||||||||
| 114 | 0 | ▶ | die "invalid string escape" unless index('"\\/bfnrt', $escape) >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | 0 | die "unterminated string"; | |||||||||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 0 | ▶ | sub _skip_whitespace { | ||||||||||||||||||||||||||
|
Subroutine
_skip_whitespace: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 121 | 0 | my ($text, $offset) = @_; | |||||||||||||||||||||||||||
| 122 | 0 | ▶ | ++$$offset while $$offset < length($text) && index(" \t\r\n", substr($text, $$offset, 1)) >= 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$offset < length $text and index(" \t\r\n", substr($text, $$offset, 1)) != -1
MC/DC0%0/2
$$offset < length $text and index(" \t\r\n", substr($text, $$offset, 1)) != -1
$$offset < length $text
index(" \t\r\n", substr($text, $$offset, 1)) != -1
Truth table0%0/3
A and B
A= $$offset < length $textB= index(" \t\r\n", substr($text, $$offset, 1)) != -1
| |||||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 1 | 1; | |||||||||||||||||||||||||||