blib/lib/MojoX/AlmostJSON.pm

filter: Statementstmt 43.4% 56 / 129 Branchbran 16.6% 14 / 84 Conditioncond 36.3% 8 / 22 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 60.0% 15 / 25 Podpod 0.0% 0 / 7 totaltotal 34.2% 96 / 280 CC 50 SCAR 35.4
CC
50
Cov
33%
_decode_string
90.0
_decode_value
72.0
_decode
42.0
CRAP 34.4
↑ Summary
1 package MojoX::AlmostJSON;
2 3 $MojoX::AlmostJSON::VERSION = '1.151250';
3 # ABSTRACT: Almost JSON but allow javascript function
4 # Code stolen from Mojo::JSON
5 3 ▶use Mojo::Base -strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 3 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
12
13 3 our @EXPORT_OK = qw(decode_json encode_json false from_json j to_json true);
14
15 # Booleans
16 3 my ($FALSE, $TRUE) = map { bless \(my $dummy = $_), 'MojoX::AlmostJSON::_Bool' } 0, 1;
17
18 # Escaped special character map (with u2028 and u2029)
19 3 my %ESCAPE = (
20 '"' => '"',
21 '\\' => '\\',
22 '/' => '/',
23 'b' => "\x08",
24 'f' => "\x0c",
25 'n' => "\x0a",
26 'r' => "\x0d",
27 't' => "\x09",
28 'u2028' => "\x{2028}",
29 'u2029' => "\x{2029}"
30 );
31 3 my %REVERSE = map { $ESCAPE{$_} => "\\$_" } keys %ESCAPE;
32 3 ▶for (0x00 .. 0x1f) { $REVERSE{pack 'C', $_} //= sprintf '\u%.4X', $_ }
Condition100%2/2
$REVERSE{pack "C", $_} //= sprintf("\\u%.4X", $_)
l!l
1581
MC/DC100%1/1
$REVERSE{pack "C", $_} //= sprintf("\\u%.4X", $_)
$REVERSE{pack "C", $_}
Truth table100%2/2
A
A= $REVERSE{pack "C", $_}
Aresult
11
00
33
34 0 ▶sub decode_json {
Subroutine decode_json: not called CC=2 SCAR=17.9
Pod undocumented
35 0 my $err = _decode(\my $value, shift);
36 0 ▶ return defined $err ? croak $err : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 0 0 0
37 }
38
39 2 ▶sub encode_json { Mojo::Util::encode 'UTF-8', _encode_value(shift) }
Subroutine encode_json: called CC=1 SCAR=0.0
Pod undocumented
40
41 1 ▶sub false () {$FALSE}
Subroutine false: called CC=1 SCAR=0.0
Pod undocumented
42
43 0 ▶sub from_json {
Subroutine from_json: not called CC=2 SCAR=17.9
Pod undocumented
44 0 my $err = _decode(\my $value, shift, 1);
45 0 ▶ return defined $err ? croak $err : $value;
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 0 0 0
46 }
47
48 0 ▶sub j {
Subroutine j: not called CC=3 SCAR=24.8
Pod undocumented
49 0 ▶ return encode_json($_[0]) if ref $_[0] eq 'ARRAY' || ref $_[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "ARRAY" or ref $_[0] eq "HASH" 0 0 0
Condition0%0/3
ref $_[0] eq "ARRAY" or ref $_[0] eq "HASH"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] eq "ARRAY" or ref $_[0] eq "HASH"
ref $_[0] eq "ARRAY" ref $_[0] eq "HASH"
Truth table0%0/3
A or B
A= ref $_[0] eq "ARRAY"B= ref $_[0] eq "HASH"
ABresult
1X1
011
000
50 0 return eval { decode_json($_[0]) };
51 }
52
53 0 ▶sub to_json { _encode_value(shift) }
Subroutine to_json: not called CC=1 SCAR=6.9
Pod undocumented
54
55 1 ▶sub true () {$TRUE}
Subroutine true: called CC=1 SCAR=0.0
Pod undocumented
56
57 0 ▶sub _decode {
Subroutine _decode: not called CC=6 SCAR=37.4
58 0 my $valueref = shift;
59
60 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { die "Missing or empty input\n" unless length(local $_ = shift()); $_ = Mojo::Util::decode('UTF-8', $_) unless shift(); die "Input is not UTF-8 encoded\n" unless defined $_; $$valueref = _decode_value(); /\G[\x20\x09\x0a\x0d]*\z/cgu or _throw('Unexpected data') } } ? : 0 0 0
61
62 # Missing input
63 0 ▶ die "Missing or empty input\n" unless length(local $_ = shift);
Branch: 1 branch
BranchTrueFalseTotal
unless length(local $_ = shift()) 0 0 0
64
65 # UTF-8
66 0 ▶ $_ = Mojo::Util::decode 'UTF-8', $_ unless shift;
Branch: 1 branch
BranchTrueFalseTotal
unless shift() 0 0 0
67 0 ▶ die "Input is not UTF-8 encoded\n" unless defined;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
68
69 # Value
70 0 $$valueref = _decode_value();
71
72 # Leftover data
73 0 ▶ /\G[\x20\x09\x0a\x0d]*\z/gc or _throw('Unexpected data');
Condition0%0/3
/\G[\x20\x09\x0a\x0d]*\z/cgu || _throw('Unexpected data')
l!l&&r!l&&!r
000
MC/DC0%0/2
/\G[\x20\x09\x0a\x0d]*\z/cgu || _throw('Unexpected data')
/\G[\x20\x09\x0a\x0d]*\z/cgu _throw('Unexpected data')
Truth table0%0/3
A || B
A= /\G[\x20\x09\x0a\x0d]*\z/cguB= _throw('Unexpected data')
ABresult
1X1
011
000
74 } ? return undef : chomp $@;
75
76 0 return $@;
77 }
78
79 0 ▶sub _decode_array {
Subroutine _decode_array: not called CC=4 SCAR=30.0
80 0 my @array;
81 0 ▶ until (m/\G[\x20\x09\x0a\x0d]*\]/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G[\x20\x09\x0a\x0d]*\]/cgu) 0 0 0
82
83 # Value
84 0 push @array, _decode_value();
85
86 # Separator
87 0 ▶ redo if /\G[\x20\x09\x0a\x0d]*,/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G[\x20\x09\x0a\x0d]*,/cgu 0 0 0
88
89 # End
90 0 ▶ last if /\G[\x20\x09\x0a\x0d]*\]/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G[\x20\x09\x0a\x0d]*\]/cgu 0 0 0
91
92 # Invalid character
93 0 _throw('Expected comma or right square bracket while parsing array');
94 }
95
96 0 return \@array;
97 }
98
99 0 ▶sub _decode_object {
Subroutine _decode_object: not called CC=6 SCAR=37.4
100 0 my %hash;
101 0 ▶ until (m/\G[\x20\x09\x0a\x0d]*\}/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G[\x20\x09\x0a\x0d]*\}/cgu) 0 0 0
102
103 # Quote
104 0 ▶ /\G[\x20\x09\x0a\x0d]*"/gc
Branch: 1 branch
BranchTrueFalseTotal
/\G[\x20\x09\x0a\x0d]*"/cgu or _throw("Expected string while parsing object") 0 0 0
105 or _throw('Expected string while parsing object');
106
107 # Key
108 0 my $key = _decode_string();
109
110 # Colon
111 0 ▶ /\G[\x20\x09\x0a\x0d]*:/gc
Branch: 1 branch
BranchTrueFalseTotal
/\G[\x20\x09\x0a\x0d]*:/cgu or _throw("Expected colon while parsing object") 0 0 0
112 or _throw('Expected colon while parsing object');
113
114 # Value
115 0 $hash{$key} = _decode_value();
116
117 # Separator
118 0 ▶ redo if /\G[\x20\x09\x0a\x0d]*,/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G[\x20\x09\x0a\x0d]*,/cgu 0 0 0
119
120 # End
121 0 ▶ last if /\G[\x20\x09\x0a\x0d]*\}/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G[\x20\x09\x0a\x0d]*\}/cgu 0 0 0
122
123 # Invalid character
124 0 _throw('Expected comma or right curly bracket while parsing object');
125 }
126
127 0 return \%hash;
128 }
129
130 0 ▶sub _decode_string {
Subroutine _decode_string: not called CC=9 SCAR=45.0
131 0 my $pos = pos;
132
133 # Extract string with escaped characters
134 0 m!\G((?:(?:[^\x00-\x1f\\"]|\\(?:["\\/bfnrt]|u[0-9a-fA-F]{4})){0,32766})*)!gc;
135 0 my $str = $1;
136
137 # Invalid character
138 0 ▶ unless (m/\G"/gc) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\G"/cgu) 0 0 0
139 0 ▶ _throw('Unexpected character or invalid escape while parsing string')
Branch: 1 branch
BranchTrueFalseTotal
if /\G[\x00-\x1f\\]/u 0 0 0
140 if /\G[\x00-\x1f\\]/;
141 0 _throw('Unterminated string');
142 }
143
144 # Unescape popular characters
145 0 ▶ if (index($str, '\\u') < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($str, "\\u") == -1) 0 0 0
146 0 $str =~ s!\\(["\\/bfnrt])!$ESCAPE{$1}!gs;
147 0 return $str;
148 }
149
150 # Unescape everything else
151 0 my $buffer = '';
152 0 ▶ while ($str =~ /\G([^\\]*)\\(?:([^u])|u(.{4}))/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /\G([^\\]*)\\(?:([^u])|u(.{4}))/cgu) 0 0 0
153 0 $buffer .= $1;
154
155 # Popular character
156 0 ▶ if ($2) { $buffer .= $ESCAPE{$2} }
Branch: 1 branch
BranchTrueFalseTotal
if ($2) { } 0 0 0
157
158 # Escaped
159 else {
160 0 my $ord = hex $3;
161
162 # Surrogate pair
163 0 ▶ if (($ord & 0xf800) == 0xd800) {
Branch: 1 branch
BranchTrueFalseTotal
if (($ord & 63488) == 55296) 0 0 0
164
165 # High surrogate
166 0 ▶ ($ord & 0xfc00) == 0xd800
Branch: 1 branch
BranchTrueFalseTotal
($ord & 64512) == 55296 or pos $_ = $pos + pos($str), _throw("Missing high-surrogate") 0 0 0
167 or pos($_) = $pos + pos($str), _throw('Missing high-surrogate');
168
169 # Low surrogate
170 0 ▶ $str =~ /\G\\u([Dd][C-Fc-f]..)/gc
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /\G\\u([Dd][C-Fc-f]..)/cgu or pos $_ = $pos + pos($str), _throw("Missing low-surrogate") 0 0 0
171 or pos($_) = $pos + pos($str), _throw('Missing low-surrogate');
172
173 0 $ord = 0x10000 + ($ord - 0xd800) * 0x400 + (hex($1) - 0xdc00);
174 }
175
176 # Character
177 0 $buffer .= pack 'U', $ord;
178 }
179 }
180
181 # The rest
182 0 return $buffer . substr $str, pos($str), length($str);
183 }
184
185 0 ▶sub _decode_value {
Subroutine _decode_value: not called CC=8 SCAR=42.8
186
187 # Leading whitespace
188 0 /\G[\x20\x09\x0a\x0d]*/gc;
189
190 # String
191 0 ▶ return _decode_string() if /\G"/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G"/cgu 0 0 0
192
193 # Object
194 0 ▶ return _decode_object() if /\G\{/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G\{/cgu 0 0 0
195
196 # Array
197 0 ▶ return _decode_array() if /\G\[/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G\[/cgu 0 0 0
198
199 # Number
200 0 ▶ return 0 + $1
Branch: 1 branch
BranchTrueFalseTotal
if /\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/cgu 0 0 0
201 if /\G([-]?(?:0|[1-9][0-9]*)(?:\.[0-9]*)?(?:[eE][+-]?[0-9]+)?)/gc;
202
203 # True
204 0 ▶ return $TRUE if /\Gtrue/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\Gtrue/cgu 0 0 0
205
206 # False
207 0 ▶ return $FALSE if /\Gfalse/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\Gfalse/cgu 0 0 0
208
209 # Null
210 0 ▶ return undef if /\Gnull/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\Gnull/cgu 0 0 0
211
212 # Invalid character
213 0 _throw('Expected string, array, object, number, boolean or null');
214 }
215
216 3 ▶sub _encode_array {
Subroutine _encode_array: called CC=1 SCAR=0.0
217 3 '[' . join(',', map { _encode_value($_) } @{$_[0]}) . ']';
218 }
219
220 4 ▶sub _encode_object {
Subroutine _encode_object: called CC=1 SCAR=0.0
221 4 my $object = shift;
222 4 my @pairs = map { _encode_string($_) . ':' . _encode_value($object->{$_}) }
223 keys %$object;
224 4 return '{' . join(',', @pairs) . '}';
225 }
226
227 5 ▶sub _encode_string {
Subroutine _encode_string: called CC=1 SCAR=0.0
228 5 my $str = shift;
229 5 $str =~ s!([\x00-\x1f\x{2028}\x{2029}\\"/])!$REVERSE{$1}!gs;
230 5 return "\"$str\"";
231 }
232
233 15 ▶sub _encode_value {
Subroutine _encode_value: called CC=16 SCAR=33.6
234 15 my $value = shift;
235
236 # Reference
237 15 ▶ if (my $ref = ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $value) 10 5 15
238
239 # Object
240 10 ▶ return _encode_object($value) if $ref eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "HASH" 4 6 10
241
242 # Array
243 6 ▶ return _encode_array($value) if $ref eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "ARRAY" 3 3 6
244  
245 #Function
246 3 ▶ return $$value if $ref eq 'SCALAR' and $$value !~/^[01]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ref eq "SCALAR" and not $$value =~ /^[01]$/u 1 2 3
Condition66%2/3
$ref eq "SCALAR" and not $$value =~ /^[01]$/u
!ll&&!rl&&r
201
MC/DC50%1/2
$ref eq "SCALAR" and not $$value =~ /^[01]$/u
$ref eq "SCALAR" not $$value =~ /^[01]$/u
Truth table66%2/3
A and not(B)
A= $ref eq "SCALAR"B= not $$value =~ /^[01]$/u
ABresult
0X0
100
111
247  
248 # True or false
249 2 ▶ return $value ? 'true' : 'false' if $ref =~ /^(?:MojoX::Almost|Mojo::)JSON::_Bool$/;
Branch: 2 branches
BranchTrueFalseTotal
if $ref =~ /^(?:MojoX::Almost|Mojo::)JSON::_Bool$/u 2 0 2
$value ? : 1 1 2
250 0 ▶ return $$value ? 'true' : 'false' if $ref eq 'SCALAR';
Branch: 2 branches
BranchTrueFalseTotal
if $ref eq "SCALAR" 0 0 0
$$value ? : 0 0 0
251
252 # Blessed reference with TO_JSON method
253 0 ▶ if (blessed $value && (my $sub = $value->can('TO_JSON'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and my $sub = $value->can("TO_JSON")) 0 0 0
Condition0%0/3
builtin::blessed($value) and my $sub = $value->can("TO_JSON")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($value) and my $sub = $value->can("TO_JSON")
builtin::blessed($value) my $sub = $value->can("TO_JSON")
Truth table0%0/3
A and B
A= builtin::blessed($value)B= my $sub = $value->can("TO_JSON")
ABresult
0X0
100
111
254 0 return _encode_value($value->$sub);
255 }
256 }
257
258 # Null
259 5 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 5 5
260
261 # Number
262 5 ▶ return $value
Branch: 1 branch
BranchTrueFalseTotal
if B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value and $value * 0 == 0 4 1 5
Condition66%4/6
B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value and $value * 0 == 0
!ll&&!rl&&r
104
B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value
!ll&&!rl&&r
104
MC/DC33%1/3
B::svref_2object(\$value)->FLAGS & 12288 and 0 + $value eq $value and $value * 0 == 0
B::svref_2object(\$value)->FLAGS & 12288 0 + $value eq $value $value * 0 == 0
Truth table50%2/4
A and B and C
A= B::svref_2object(\$value)->FLAGS & 12288B= 0 + $value eq $valueC= $value * 0 == 0
ABCresult
0XX0
10X0
1100
1111
263 if B::svref_2object(\$value)->FLAGS & (B::SVp_IOK | B::SVp_NOK)
264 && 0 + $value eq $value
265 && $value * 0 == 0;
266
267 # String
268 1 return _encode_string($value);
269 }
270
271 0 ▶sub _throw {
Subroutine _throw: not called CC=3 SCAR=24.8
272
273 # Leading whitespace
274 0 /\G[\x20\x09\x0a\x0d]*/gc;
275
276 # Context
277 0 my $context = 'Malformed JSON: ' . shift;
278 0 ▶ if (m/\G\z/gc) { $context .= ' before end of data' }
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\z/cgu) { } 0 0 0
279 else {
280 0 my @lines = split "\n", substr($_, 0, pos);
281 0 ▶ $context .= ' at line ' . @lines . ', offset ' . length(pop @lines || '');
Condition0%0/2
pop @lines || ''
l!l
00
MC/DC0%0/1
pop @lines || ''
pop @lines
Truth table0%0/2
A
A= pop @lines
Aresult
11
00
282 }
283
284 0 die "$context\n";
285 }
286
287 # Emulate boolean type
288 package MojoX::AlmostJSON::_Bool;
289 3 $MojoX::AlmostJSON::_Bool::VERSION = '1.151250';
290 3 ▶use overload '""' => sub { ${$_[0]} }, fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
291
292 3 1;
293  
294 __END__
↑ Summary