lib/JSON/JSONFold.pm

filter: Statementstmt 81.5% 469 / 575 Branchbran 66.2% 106 / 160 Conditioncond 63.3% 69 / 109 MC/DCmcdc 37.9% 22 / 58 Subroutinesub 79.3% 92 / 116 Podpod 85.7% 12 / 14 totaltotal 74.6% 770 / 1032 CC 129 SCAR 17.6
CC
129
Cov
76%
run
30.0
fold
20.0
read_input
20.0
CRAP 5.8
↑ Summary script/jsonfold.pl »
1 package JSON::JSONFold;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use 5.014 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.1.7';
11 5 our @EXPORT = qw(
12 format_json write_json fold_text
13 encode_json to_json) ;
14  
15 5 our @EXPORT_OK = qw(
16 jsonfold_config
17 create_formatter
18 ) ;
19 # Object Orient Interface
20  
21 sub new {
22 19 ▶ my ($class, %overrides) = @_ ;
Subroutine new: called CC=5 SCAR=17.1
Pod documented
23  
24 # Required parameters
25 19 my $width = delete $overrides{width} ;
26 19 my $config = delete $overrides{config} ;
27  
28 19 ▶ my $gold = delete $overrides{gold} // 1 ;
Condition100%2/2
delete $overrides{"gold"} // 1
l!l
118
MC/DC100%1/1
delete $overrides{"gold"} // 1
delete $overrides{"gold"}
Truth table100%2/2
A
A= delete $overrides{"gold"}
Aresult
11
00
29 19 my $indent = delete $overrides{indent} ;
30 19 ▶ my $json = delete $overrides{json} // _json_coder($gold, $indent, %overrides) ;
Condition33%1/3
delete $overrides{"json"} // _json_coder($gold, $indent, %overrides)
l!l&&r!l&&!r
0190
MC/DC0%0/2
delete $overrides{"json"} // _json_coder($gold, $indent, %overrides)
delete $overrides{"json"} _json_coder($gold, $indent, %overrides)
Truth table33%1/3
A // B
A= delete $overrides{"json"}B= _json_coder($gold, $indent, %overrides)
ABresult
1X1
011
000
31 19 my $do_close = delete $overrides{do_close} ;
32 19 ▶ return bless {
Condition40%2/5
ref $class || $class || __PACKAGE__
l!l
190
ref $class or $class
l!l&&r!l&&!r
0190
MC/DC0%0/2
ref $class || $class || __PACKAGE__
ref $class $class
Truth table33%1/3
A or B
A= ref $classB= $class
ABresult
1X1
011
000
33 json => $json,
34 width => $width,
35 config => _config($config, $width),
36 do_close => $do_close,
37 }, ref($class) || $class || __PACKAGE__ ;
38 }
39  
40 sub format {
41 18 ▶ my($self, $data) = @_ ;
Subroutine format: called CC=4 SCAR=14.0
Pod documented
42  
43 18 my $config = $self->{config} ;
44  
45 18 my $output = '' ;
46 18 ▶ open my $out, '>', \$output or die "open output: $!" ;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", \$output or die "open output: $!" 0 18 18
47  
48 18 my $stream = _stream($out, $config, 0) ;
49 18 my $json = $self->{json} ;
50 18 my $text = $json->encode($data) ;
51  
52 18 $stream->write($text);
53 18 $stream->finish;
54 18 $stream->flush;
55  
56 18 ▶ close $out or die "close output: $!" ;
Branch: 1 branch
BranchTrueFalseTotal
close $out or die "close output: $!" 0 18 18
57 18 ▶ $output .= "\n" unless $output =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $output =~ /\n\z/u 0 18 18
58 18 return $output ;
59 }
60  
61 sub fold {
62 0 ▶ my ($self, $text) = @_ ;
Subroutine fold: not called CC=4 SCAR=30.0
Pod documented
63  
64 0 my $config = $self->{config} ;
65  
66 0 my $output = '' ;
67 0 ▶ open my $out, '>', \$output or die "open output: $!" ;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", \$output or die "open output: $!" 0 0 0
68  
69 0 my $stream = _stream($out, $config, 0) ;
70  
71 0 $stream->write($text);
72 0 $stream->finish;
73 0 $stream->flush;
74  
75 0 ▶ close $out or die "close output: $!" ;
Branch: 1 branch
BranchTrueFalseTotal
close $out or die "close output: $!" 0 0 0
76 0 ▶ $output .= "\n" unless $output =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $output =~ /\n\z/u 0 0 0
77 0 return $output ;
78 }
79  
80 sub write {
81 1 ▶ my($self, $data, $fh) = @_ ;
Subroutine write: called CC=1 SCAR=0.0
Pod documented
82  
83 1 my $config = $self->{config} ;
84  
85 1 my $do_close = $self->{do_close} ;
86 1 my $stream = _stream($fh, $config, $do_close) ;
87 1 my $json = $self->{json} ;
88  
89 1 my $text = $json->encode($data) ;
90 1 $stream->write($text);
91 1 $stream->finish;
92 1 $stream->flush;
93 1 my $info = $stream->stats ;
94 1 $stream->close() ;
95  
96 1 return $info ;
97 }
98  
99 # Functional Interface
100  
101 # Not exportable. Allow using DEV::JSONFold::config(...)
102 sub config {
103 0 ▶ my($base_config, $width, %overrides) = @_ ;
Subroutine config: not called CC=2 SCAR=17.9
Pod undocumented
104 0 ▶ $width //= $overrides{width} ;
Condition0%0/3
$width //= $overrides{"width"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$width //= $overrides{"width"}
$width $overrides{"width"}
Truth table0%0/3
A //= B
A= $widthB= $overrides{"width"}
ABresult
1X1
011
000
105 0 return _config($base_config, $width, %overrides) ;
106 }
107  
108 sub jsonfold_config {
109 1 ▶ my($base_config, $width, %overrides) = @_ ;
Subroutine jsonfold_config: called CC=2 SCAR=7.6
Pod documented
110 1 ▶ $width //= $overrides{width} ;
Condition33%1/3
$width //= $overrides{"width"}
l!l&&r!l&&!r
001
MC/DC0%0/2
$width //= $overrides{"width"}
$width $overrides{"width"}
Truth table33%1/3
A //= B
A= $widthB= $overrides{"width"}
ABresult
1X1
011
000
111 1 return _config($base_config, $width, %overrides) ;
112 }
113  
114 sub format_json {
115 0 ▶ my($data, $width, $config, %overrides) = @_ ;
Subroutine format_json: not called CC=1 SCAR=6.9
Pod documented
116  
117 0 my $fmt = __PACKAGE__->new(width => $width, config => $config, %overrides) ;
118 0 my $output = $fmt->format($data) ;
119 0 return $output ;
120 }
121  
122 sub write_json {
123 1 ▶ my($data, $fh, $width, $config, %overrides) = @_ ;
Subroutine write_json: called CC=1 SCAR=0.0
Pod documented
124  
125 1 my $fmt = __PACKAGE__->new(width => $width, config => $config, %overrides) ;
126 1 my $info = $fmt->write($data, $fh) ;
127 1 return $info ;
128 }
129  
130 sub fold_text {
131 0 ▶ my($text, $width, $config) = @_ ;
Subroutine fold_text: not called CC=1 SCAR=6.9
Pod documented
132 0 my $fmt = __PACKAGE__->new(width => $width, config => $config) ;
133  
134 0 return $fmt->fold($text) ;
135 }
136  
137 sub create_formatter {
138 0 ▶ my($fh, $width, $config, %overrides) = @_ ;
Subroutine create_formatter: not called CC=1 SCAR=6.9
Pod documented
139 0 my $do_close = delete $overrides{close_fp} ;
140 0 return _stream($fh, _config($config, $width, %overrides), $do_close) ;
141 }
142  
143 # Helper for Function/OO interface methods
144  
145 sub _config {
146 20 ▶ my ($preset, $width, %overrides) = @_ ;
Subroutine _config: called CC=2 SCAR=7.1
147 20 ▶ $overrides{width} = $width if $width ;
Branch: 1 branch
BranchTrueFalseTotal
if $width 0 20 20
148 20 return JSON::JSONFold::Config::config($preset, %overrides) ;
149 }
150  
151 sub _stream {
152 19 ▶ my ($fp, $config, $close_fp) = @_ ;
Subroutine _stream: called CC=1 SCAR=0.0
153 19 return JSON::JSONFold::Writer->new($fp, $config, $close_fp) ;
154 }
155  
156 sub _json_coder {
157 19 ▶ my ($gold, $indent, %opt) = @_;
Subroutine _json_coder: called CC=5 SCAR=16.4
158 # Must have valid indent, otherwise cannot parse the data
159 19 my $json = JSON::PP->new->pretty ;
160 19 ▶ if ( $gold ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gold) 19 0 19
161 19 ▶ my $sort_keys = $opt{sort_keys} // 1 ;
Condition100%2/2
$opt{"sort_keys"} // 1
l!l
118
MC/DC100%1/1
$opt{"sort_keys"} // 1
$opt{"sort_keys"}
Truth table100%2/2
A
A= $opt{"sort_keys"}
Aresult
11
00
162 19 ▶ $indent //=2 ;
Condition50%1/2
$indent //= 2
l!l
019
MC/DC0%0/1
$indent //= 2
$indent
Truth table50%1/2
A
A= $indent
Aresult
11
00
163 19 $json->allow_nonref->canonical($sort_keys);
164 19 $json->space_before(0)->space_after(1);
165 }
166 19 ▶ $json->indent_length($indent) if defined $indent ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $indent 19 0 19
167 19 return $json;
168 }
169  
170 # JSON compatible API - OO
171  
172 sub encode {
173 0 ▶ my ($self, $data) = @_ ;
Subroutine encode: not called CC=1 SCAR=6.9
Pod documented
174 0 return $self->format($data) ;
175 }
176  
177  
178 # JSON compatiable API - Functional
179  
180 sub encode_json {
181 18 ▶ my ($data, $opts) = @_ ;
Subroutine encode_json: called CC=2 SCAR=6.9
Pod documented
182 18 ▶ my %overrides = %$opts if $opts ;
Branch: 1 branch
BranchTrueFalseTotal
if $opts 17 1 18
183 18 my $width = delete $overrides{width} ;
184 18 my $compact = delete $overrides{compact} ;
185  
186 18 my $fmt = __PACKAGE__->new(width => $width, config => $compact, %overrides) ;
187 18 my $output = $fmt->format($data) ;
188 18 return $output ;
189 }
190  
191 # Same as encode_json - for users of legacy "JSON" wrapper.
192  
193 sub to_json {
194 0 ▶ my ($data, $opts) = @_ ;
Subroutine to_json: not called CC=2 SCAR=17.9
Pod documented
195 0 ▶ my %overrides = %$opts if $opts ;
Branch: 1 branch
BranchTrueFalseTotal
if $opts 0 0 0
196 0 my $width = delete $overrides{width} ;
197 0 my $compact = delete $overrides{compact} ;
198  
199 0 my $fmt = __PACKAGE__->new(width => $width, config => $compact, %overrides) ;
200 0 my $output = $fmt->format($data) ;
201 0 return $output ;
202 }
203
204 sub run {
205 0 ▶ JSON::JSONFold::CLI::run() ;
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
206 }
207  
208 package JSON::JSONFold::Kind;
209  
210 5 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
211 5 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
212 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
213  
214 5 our @EXPORT_OK = qw(
215 KIND_NONE
216 KIND_DICT
217 KIND_LIST
218 %OPENING_KIND
219 %CLOSING_KIND
220 );
221  
222 5 ▶use constant KIND_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
223 5 ▶use constant KIND_DICT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
224 5 ▶use constant KIND_LIST => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
225  
226 5 our %OPENING_KIND = (
227 '{' => KIND_DICT,
228 '[' => KIND_LIST,
229 );
230  
231 5 our %CLOSING_KIND = (
232 '}' => KIND_DICT,
233 '},' => KIND_DICT,
234 ']' => KIND_LIST,
235 '],' => KIND_LIST,
236 );
237  
238 # -------------------------------------------------------------------------
239 # Internal package: immutable-ish configuration record
240 # -------------------------------------------------------------------------
241  
242 package JSON::JSONFold::Config;
243  
244 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
245 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
246 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
247  
248 5 ▶use constant MAX_ARRAY_ITEMS => 1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
249 5 ▶use constant MAX_OBJ_ITEMS => 1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
250 5 ▶use constant MAX_NESTING => 10;
Subroutine BEGIN: called CC=1 SCAR=0.0
251 5 ▶use constant DEFAULT_WIDTH => 100;
Subroutine BEGIN: called CC=1 SCAR=0.0
252  
253 5 our $SEQ;
254 use constant {
255 5 C_OFF => $SEQ++,
256 C_WIDTH => $SEQ++,
257  
258 C_PACK_ARRAY_ITEMS => $SEQ++,
259 C_PACK_OBJ_ITEMS => $SEQ++,
260 C_PACK_NESTING => $SEQ++,
261  
262 C_FOLD_ARRAY_ITEMS => $SEQ++,
263 C_FOLD_OBJ_ITEMS => $SEQ++,
264 C_FOLD_NESTING => $SEQ++,
265  
266 C_JOIN_ARRAY_ITEMS => $SEQ++,
267 C_JOIN_OBJ_ITEMS => $SEQ++,
268 C_JOIN_NESTING => $SEQ++,
269 C_UNUSED_LAST => $SEQ++,
270 5 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
271  
272 5 our %PRESETS ; # values defined later
273  
274 BEGIN {
275  
276 5 ▶ our @EXPORT = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
277 C_OFF
278 C_WIDTH
279 C_PACK_ARRAY_ITEMS
280 C_PACK_OBJ_ITEMS
281 C_PACK_NESTING
282 C_FOLD_ARRAY_ITEMS
283 C_FOLD_OBJ_ITEMS
284 C_FOLD_NESTING
285 C_JOIN_ARRAY_ITEMS
286 C_JOIN_OBJ_ITEMS
287 C_JOIN_NESTING
288 );
289 }
290  
291 5 our @FIELDS = (
292 [ 'off', C_OFF ],
293 [ 'width', C_WIDTH ],
294 [ 'pack_array_items', C_PACK_ARRAY_ITEMS ],
295 [ 'pack_obj_items', C_PACK_OBJ_ITEMS ],
296 [ 'pack_nesting', C_PACK_NESTING ],
297 [ 'fold_array_items', C_FOLD_ARRAY_ITEMS ],
298 [ 'fold_obj_items', C_FOLD_OBJ_ITEMS ],
299 [ 'fold_nesting', C_FOLD_NESTING ],
300 [ 'join_array_items', C_JOIN_ARRAY_ITEMS ],
301 [ 'join_obj_items', C_JOIN_OBJ_ITEMS ],
302 [ 'join_nesting', C_JOIN_NESTING ],
303 );
304  
305 5 our %NAME_TO_INDEX = map { @$_ } @FIELDS ;
306  
307 sub as_hash {
308 0 ▶ my ($self) = @_ ;
Subroutine as_hash: not called CC=1 SCAR=6.9
309 0 map { my ($name, $idx) = @$_ ; ($name => $self->[$idx]) ; } @FIELDS ;
310 }
311  
312 sub _make {
313 15 ▶ my ($class, %arg) = @_;
Subroutine _make: called CC=1 SCAR=0.0
314 15 my @d;
315 15 $#d = $SEQ;
316 15 $d[C_OFF] = $arg{off} ;
317 15 $d[C_WIDTH] = $arg{width};
318  
319 15 $d[C_PACK_ARRAY_ITEMS] = $arg{pack_array_items};
320 15 $d[C_PACK_OBJ_ITEMS] = $arg{pack_obj_items};
321 15 $d[C_PACK_NESTING] = $arg{pack_nesting};
322  
323 15 $d[C_FOLD_ARRAY_ITEMS] = $arg{fold_array_items};
324 15 $d[C_FOLD_OBJ_ITEMS] = $arg{fold_obj_items};
325 15 $d[C_FOLD_NESTING] = $arg{fold_nesting};
326  
327 15 $d[C_JOIN_ARRAY_ITEMS] = $arg{join_array_items};
328 15 $d[C_JOIN_OBJ_ITEMS] = $arg{join_obj_items};
329 15 $d[C_JOIN_NESTING] = $arg{join_nesting};
330 15 return bless \@d, $class;
331 }
332  
333 5 our $NONE = __PACKAGE__->_make(
334 width => DEFAULT_WIDTH,
335  
336 pack_array_items => 0,
337 pack_obj_items => 0,
338 pack_nesting => 0,
339  
340 fold_array_items => 0,
341 fold_obj_items => 0,
342 fold_nesting => 0,
343  
344 join_array_items => 0,
345 join_obj_items => 0,
346 join_nesting => 0,
347 );
348  
349 5 our $DEFAULT = __PACKAGE__->_make(
350 width => DEFAULT_WIDTH,
351  
352 pack_array_items => 8,
353 pack_obj_items => 4,
354 pack_nesting => 1,
355  
356 fold_array_items => 8,
357 fold_obj_items => 4,
358 fold_nesting => 1,
359  
360 join_array_items => 8,
361 join_obj_items => 4,
362 join_nesting => 1,
363 );
364  
365 sub _replace {
366 74 ▶ my ($base, $validate) = (shift, shift) ;
Subroutine _replace: called CC=9 SCAR=22.3
367 74 ▶ return $base unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 74 74
368 74 ▶ my $overrides = @_ == 1 && ref($_[0]) ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] ? : 39 35 74
Condition66%2/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
35039
MC/DC50%1/2
@_ == 1 and ref $_[0]
@_ == 1 ref $_[0]
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0]
ABresult
0X0
100
111
369 74 ▶ return $base unless %$overrides;
Branch: 1 branch
BranchTrueFalseTotal
unless %$overrides 38 36 74
370  
371 # Make acopy of the original object.
372 36 my @d = @$base;
373 36 for my $key (keys %$overrides) {
374 171 ▶ unless ( exists $NAME_TO_INDEX{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $JSON::JSONFold::Config::NAME_TO_INDEX{$key}) 1 170 171
375 1 ▶ die "unknown JSON::JSONFold config key: $key\n" if $validate ;
Branch: 1 branch
BranchTrueFalseTotal
if $validate 0 1 1
376 1 next ;
377 } ;
378 170 $d[$NAME_TO_INDEX{$key}] = $overrides->{$key};
379 }
380 36 ▶ return bless \@d, ref($base) || __PACKAGE__;
Condition50%1/2
ref $base || __PACKAGE__
l!l
360
MC/DC0%0/1
ref $base || __PACKAGE__
ref $base
Truth table50%1/2
A
A= ref $base
Aresult
11
00
381 }
382  
383 # Resolve named config into actual config object.
384 sub _resolve_config {
385 39 ▶ my ($config) = @_;
Subroutine _resolve_config: called CC=4 SCAR=13.9
386  
387 39 ▶ return $config if ref($config) ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $config 20 19 39
388  
389 19 ▶ my $name = $config // '';
Condition100%2/2
$config // ""
l!l
181
MC/DC100%1/1
$config // ""
$config
Truth table100%2/2
A
A= $config
Aresult
11
00
390 19 $config = $PRESETS{$name} ;
391 19 ▶ die "unknown JSON::JSONFold preset: $name\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $config 0 19 19
392 unless defined $config ;
393  
394 19 return $config ;
395 }
396  
397  
398 sub config {
399 39 ▶ my ($preset, %overrides) = @_;
Subroutine config: called CC=1 SCAR=0.0
400 39 return _replace(_resolve_config($preset), 0, \%overrides);
401 }
402  
403 sub new {
404 0 ▶ my ($class, $config, @args) = @_;
Subroutine new: not called CC=1 SCAR=6.9
405 0 return config($config, @args);
406 }
407  
408 # Create preset - force validation.
409 sub _new_preset {
410 35 ▶ my $base = shift ;
Subroutine _new_preset: called CC=1 SCAR=0.0
411 35 return _replace($base, 1, @_) ;
412 }
413  
414 5 %PRESETS = (
415 off => __PACKAGE__->_make(off => 1),
416 '' => $DEFAULT,
417 default => $DEFAULT,
418 none => $NONE,
419 low => _new_preset($DEFAULT, fold_nesting => 0, join_nesting => 0),
420 med => _new_preset($DEFAULT, join_nesting => 0),
421 high => _new_preset($DEFAULT,
422 pack_array_items => 16, pack_obj_items => 8, pack_nesting => 4,
423 fold_array_items => 16, fold_obj_items => 8, fold_nesting => 4,
424 join_array_items => 16, join_obj_items => 8, join_nesting => 2,
425 ),
426 max => _new_preset($NONE,
427 width => 255,
428 pack_array_items => MAX_ARRAY_ITEMS, pack_obj_items => MAX_OBJ_ITEMS,
429 pack_nesting => MAX_NESTING,
430 fold_array_items => MAX_ARRAY_ITEMS, fold_obj_items => MAX_OBJ_ITEMS,
431 fold_nesting => MAX_NESTING,
432 join_array_items => MAX_ARRAY_ITEMS, join_obj_items => MAX_OBJ_ITEMS,
433 join_nesting => MAX_NESTING,
434 ),
435 pack => _new_preset($NONE,
436 pack_array_items => MAX_ARRAY_ITEMS, pack_obj_items => MAX_OBJ_ITEMS,
437 pack_nesting => MAX_NESTING,
438 ),
439 fold => _new_preset($NONE,
440 fold_array_items => MAX_ARRAY_ITEMS, fold_obj_items => MAX_OBJ_ITEMS,
441 fold_nesting => MAX_NESTING,
442 ),
443 join => _new_preset($NONE,
444 fold_array_items => MAX_ARRAY_ITEMS, fold_obj_items => MAX_OBJ_ITEMS,
445 fold_nesting => MAX_NESTING,
446 join_array_items => MAX_ARRAY_ITEMS, join_obj_items => MAX_OBJ_ITEMS,
447 join_nesting => MAX_NESTING,
448 ),
449 );
450  
451  
452 # -------------------------------------------------------------------------
453 # Internal package: one physical pretty-printed line
454 # -------------------------------------------------------------------------
455  
456 package JSON::JSONFold::Line;
457 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
458 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
459 5 ▶use Exporter 'import' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
460  
461 5 ▶use constant KIND_NONE => $JSON::JSONFold::Kind::KIND_NONE ;
Subroutine BEGIN: called CC=1 SCAR=0.0
462  
463 5 our $SEQ ;
464 use constant {
465 5 L_INDENT => $SEQ++,
466 L_TEXT => $SEQ++,
467 L_PARENT_KIND => $SEQ++,
468 L_ITEMS => $SEQ++,
469 L_LEAFS => $SEQ++,
470 L_CHILD_NESTING => $SEQ++,
471 L_OPENER => $SEQ++,
472 L_CLOSER => $SEQ++,
473 L_CAN_JOIN => $SEQ++,
474 L_CAN_PACK => $SEQ++,
475 5 ▶} ;
Subroutine BEGIN: called CC=1 SCAR=0.0
476  
477 BEGIN {
478 5 ▶ our @EXPORT = qw( L_INDENT
Subroutine BEGIN: called CC=1 SCAR=0.0
479 L_TEXT
480 L_PARENT_KIND
481 L_ITEMS
482 L_LEAFS
483 L_CHILD_NESTING
484 L_OPENER
485 L_CLOSER
486 L_CAN_JOIN
487 L_CAN_PACK
488 ) ;
489 }
490  
491 sub parse {
492 169 ▶ my ($class, $s, $parent_kind) = @_;
Subroutine parse: called CC=7 SCAR=19.5
493 169 ▶ $parent_kind //= KIND_NONE;
Condition100%2/2
$parent_kind //= undef
l!l
15118
MC/DC100%1/1
$parent_kind //= undef
$parent_kind
Truth table100%2/2
A
A= $parent_kind
Aresult
11
00
494  
495 169 my ($spaces) = $s =~ /^(\s*)/;
496 169 my $body = substr($s, length($spaces));
497 169 $body =~ s/\s+\z//;
498  
499 169 my $last = substr($body, -1, 1);
500  
501 169 ▶ my $opener = $OPENING_KIND{$last} // KIND_NONE ;
Condition100%2/2
$JSON::JSONFold::Kind::OPENING_KIND{$last} // undef
l!l
47122
MC/DC100%1/1
$JSON::JSONFold::Kind::OPENING_KIND{$last} // undef
$JSON::JSONFold::Kind::OPENING_KIND{$last}
Truth table100%2/2
A
A= $JSON::JSONFold::Kind::OPENING_KIND{$last}
Aresult
11
00
502 169 ▶ my $closer = $CLOSING_KIND{$body} // KIND_NONE;
Condition100%2/2
$JSON::JSONFold::Kind::CLOSING_KIND{$body} // undef
l!l
47122
MC/DC100%1/1
$JSON::JSONFold::Kind::CLOSING_KIND{$body} // undef
$JSON::JSONFold::Kind::CLOSING_KIND{$body}
Truth table100%2/2
A
A= $JSON::JSONFold::Kind::CLOSING_KIND{$body}
Aresult
11
00
503  
504 169 ▶ my $is_body = $parent_kind && !$opener && !$closer ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$parent_kind && !$opener && !$closer ? : 73 96 169
Condition100%6/6
$parent_kind and not $opener and not $closer
!ll&&!rl&&r
494773
$parent_kind and not $opener
!ll&&!rl&&r
1831120
MC/DC100%3/3
$parent_kind and not $opener and not $closer
$parent_kind not $opener not $closer
Truth table100%4/4
A and not(B) and not(C)
A= $parent_kindB= not $openerC= not $closer
ABCresult
0XX0
10X0
1100
1111
505  
506 169 my @d ;
507 169 $#d = $SEQ ;
508 169 $d[L_INDENT] = length($spaces);
509 169 $d[L_TEXT] = $body;
510 169 $d[L_PARENT_KIND] = $parent_kind;
511 169 $d[L_ITEMS] = 1;
512 169 $d[L_LEAFS] = 1;
513 169 $d[L_CHILD_NESTING] = -1;
514 169 $d[L_OPENER] = $opener;
515 169 $d[L_CLOSER] = $closer;
516 169 $d[L_CAN_JOIN] = $is_body;
517 169 $d[L_CAN_PACK] = $is_body;
518  
519 169 return bless \@d, $class ;
520 }
521  
522 sub fold {
523 25 ▶ my ($class, $lines, $leafs, $child_nesting) = @_;
Subroutine fold: called CC=1 SCAR=0.0
524 25 my $first_line = $lines->[0] ;
525 25 my @d ;
526 25 $#d = $SEQ ;
527 25 $d[L_INDENT] = $first_line->[L_INDENT] ;
528 25 $d[L_TEXT] = join(' ', map { $_->[L_TEXT] } @$lines) ;
529 25 $d[L_PARENT_KIND] = $first_line->[L_PARENT_KIND] ;
530 25 $d[L_ITEMS] = 1;
531 25 $d[L_LEAFS] = $leafs;
532 25 $d[L_CHILD_NESTING] = $child_nesting;
533 25 $d[L_OPENER] = KIND_NONE;
534 25 $d[L_CLOSER] = KIND_NONE;
535 25 $d[L_CAN_PACK] = 0;
536 25 $d[L_CAN_JOIN] = 1,
537  
538 return bless \@d, $class ;
539 }
540  
541 74 ▶sub raw { return (' ' x $_[0][L_INDENT]) . $_[0][L_TEXT] . "\n" }
Subroutine raw: called CC=1 SCAR=0.0
542 41 ▶sub width { return $_[0][L_INDENT] + length($_[0][L_TEXT]) }
Subroutine width: called CC=1 SCAR=0.0
543  
544 sub join_line {
545 45 ▶ my ($self, $other) = @_;
Subroutine join_line: called CC=2 SCAR=7.5
546 45 $self->[L_TEXT] .= ' ' . $other->[L_TEXT];
547 45 $self->[L_ITEMS] += $other->[L_ITEMS];
548 45 $self->[L_LEAFS] += $other->[L_LEAFS];
549 45 ▶ if ($other->[L_CHILD_NESTING] > $self->[L_CHILD_NESTING]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other->[5] > $self->[5]) 0 45 45
550 0 $self->[L_CHILD_NESTING] = $other->[L_CHILD_NESTING];
551 0 $self->[L_CAN_PACK] = 0;
552 }
553 45 return $self;
554 }
555  
556 # -------------------------------------------------------------------------
557 # Internal package: stack frame for a currently open JSON container
558 # -------------------------------------------------------------------------
559  
560  
561  
562 package JSON::JSONFold::Frame;
563 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
564 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
565 5 ▶use Exporter 'import' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
566  
567 BEGIN {
568 5 ▶ JSON::JSONFold::Line->import() ;
Subroutine BEGIN: called CC=1 SCAR=0.0
569 }
570  
571 5 our $SEQ ;
572 use constant {
573 5 F_UNUSED_FIRST => $SEQ++,
574 F_KIND => $SEQ++,
575 F_DEPTH => $SEQ++,
576 F_LINES => $SEQ++,
577 F_PACK_LIMIT => $SEQ++,
578 F_FOLD_LIMIT => $SEQ++,
579 F_JOIN_LIMIT => $SEQ++,
580 F_CONTENT_LINES => $SEQ++,
581 F_ITEMS => $SEQ++,
582 F_LEAFS => $SEQ++,
583 F_FOLD_OK => $SEQ++,
584 F_CHILD_NESTING => $SEQ++,
585 F_UNUSED_LAST => $SEQ++,
586 5 ▶} ;
Subroutine BEGIN: called CC=1 SCAR=0.0
587  
588 BEGIN {
589 5 ▶ our @EXPORT = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
590 F_UNUSED_FIRST
591 F_KIND
592 F_DEPTH
593 F_LINES
594 F_PACK_LIMIT
595 F_FOLD_LIMIT
596 F_JOIN_LIMIT
597 F_CONTENT_LINES
598 F_ITEMS
599 F_LEAFS
600 F_FOLD_OK
601 F_CHILD_NESTING
602 F_UNUSED_LAST
603 ) ;
604 }
605  
606 sub new {
607 47 ▶ my ($class, %arg) = @_;
Subroutine new: called CC=6 SCAR=18.4
608 47 my @d ;
609 47 $#d = $SEQ ;
610 47 $d[F_KIND] = $arg{kind};
611 47 ▶ $d[F_DEPTH] = $arg{depth} // 0;
Condition50%1/2
$arg{"depth"} // 0
l!l
470
MC/DC0%0/1
$arg{"depth"} // 0
$arg{"depth"}
Truth table50%1/2
A
A= $arg{"depth"}
Aresult
11
00
612 47 ▶ $d[F_LINES] = $arg{lines} || [];
Condition50%1/2
$arg{'lines'} || []
l!l
470
MC/DC0%0/1
$arg{'lines'} || []
$arg{'lines'}
Truth table50%1/2
A
A= $arg{'lines'}
Aresult
11
00
613 47 ▶ $d[F_PACK_LIMIT] = $arg{pack_limit} // 0;
Condition50%1/2
$arg{"pack_limit"} // 0
l!l
470
MC/DC0%0/1
$arg{"pack_limit"} // 0
$arg{"pack_limit"}
Truth table50%1/2
A
A= $arg{"pack_limit"}
Aresult
11
00
614 47 ▶ $d[F_FOLD_LIMIT] = $arg{fold_limit} // 0;
Condition50%1/2
$arg{"fold_limit"} // 0
l!l
470
MC/DC0%0/1
$arg{"fold_limit"} // 0
$arg{"fold_limit"}
Truth table50%1/2
A
A= $arg{"fold_limit"}
Aresult
11
00
615 47 ▶ $d[F_JOIN_LIMIT] = $arg{join_limit} // 0;
Condition50%1/2
$arg{"join_limit"} // 0
l!l
470
MC/DC0%0/1
$arg{"join_limit"} // 0
$arg{"join_limit"}
Truth table50%1/2
A
A= $arg{"join_limit"}
Aresult
11
00
616 47 $d[F_CONTENT_LINES] = 0;
617 47 $d[F_ITEMS] = 0;
618 47 $d[F_LEAFS] = 0;
619 47 $d[F_FOLD_OK] = 1;
620 47 $d[F_CHILD_NESTING] = -1;
621 47 return bless \@d, $class;
622 }
623  
624 # Update Frame information based on added line
625 sub update_add {
626 104 ▶ my ($self, $line) = @_ ;
Subroutine update_add: called CC=2 SCAR=6.9
627 104 $self->[F_ITEMS] += $line->[L_ITEMS];
628 104 $self->[F_LEAFS] += $line->[L_LEAFS];
629 104 ▶ if ($line->[L_CHILD_NESTING] >= $self->[F_CHILD_NESTING]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[5] >= $self->[11]) 51 53 104
630 51 $self->[F_CHILD_NESTING] = $line->[L_CHILD_NESTING] + 1;
631 }
632 }
633  
634 232 ▶sub is_empty { return @{ $_[0][F_LINES] } == 0 }
Subroutine is_empty: called CC=1 SCAR=0.0
635 101 ▶sub last_line { return $_[0][F_LINES][-1] }
Subroutine last_line: called CC=1 SCAR=0.0
636  
637 # -------------------------------------------------------------------------
638 # Internal package: counters
639 # -------------------------------------------------------------------------
640  
641 package JSON::JSONFold::Stats;
642 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
643 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
644  
645 sub new {
646 19 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
647 19 return bless {
648 bytes_in => 0,
649 bytes_out => 0,
650 lines_in => 0,
651 lines_out => 0,
652 }, $class;
653 }
654  
655 0 ▶sub bytes_in { $_[0]{bytes_in} }
Subroutine bytes_in: not called CC=1 SCAR=6.9
656 0 ▶sub bytes_out { $_[0]{bytes_out} }
Subroutine bytes_out: not called CC=1 SCAR=6.9
657 0 ▶sub lines_in { $_[0]{lines_in} }
Subroutine lines_in: not called CC=1 SCAR=6.9
658 0 ▶sub lines_out { $_[0]{lines_out} }
Subroutine lines_out: not called CC=1 SCAR=6.9
659  
660 0 ▶sub as_hash { return %{ $_[0] } }
Subroutine as_hash: not called CC=1 SCAR=6.9
661  
662 # -------------------------------------------------------------------------
663 # Internal package: streaming folding filter/writer
664 # -------------------------------------------------------------------------
665  
666 package JSON::JSONFold::Writer;
667 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
668 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
669  
670 BEGIN {
671 5 ▶ JSON::JSONFold::Line->import() ;
Subroutine BEGIN: called CC=1 SCAR=0.0
672 5 JSON::JSONFold::Frame->import() ;
673 5 JSON::JSONFold::Config->import() ;
674 }
675  
676 5 our $SEQ ;
677 use constant {
678 5 W_UNUSED_FIRST => $SEQ++,
679 W_FH => $SEQ++,
680 W_CFG => $SEQ++,
681 W_PENDING => $SEQ++,
682 W_STACK => $SEQ++,
683 W_STATS => $SEQ++,
684 W_DO_CLOSE => $SEQ++,
685 W_UNUSED_LAST => $SEQ++,
686 5 ▶} ;
Subroutine BEGIN: called CC=1 SCAR=0.0
687  
688 BEGIN {
689 5 ▶ our @EXPORT = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
690 W_UNUSED_FIRST
691 W_FH
692 W_CFG
693 W_PENDING
694 W_STACK
695 W_STATS
696 W_DO_CLOSE
697 W_UNUSED_LAST
698 ) ;
699 }
700  
701 sub new {
702 19 ▶ my ($class, $fh, $config, $do_close) = @_;
Subroutine new: called CC=2 SCAR=6.9
703  
704 19 my $cfg = JSON::JSONFold::Config::config($config) ;
705 19 my @d ;
706 19 $#d = $SEQ ;
707 19 $d[W_FH] = $fh;
708 19 ▶ $d[W_CFG] = $cfg unless $cfg->[C_OFF] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->[0] 18 1 19
709 19 $d[W_PENDING] = '';
710 19 $d[W_STACK] = [];
711 19 $d[W_STATS] = JSON::JSONFold::Stats->new;
712 19 $d[W_DO_CLOSE] = $do_close;
713 19 return bless \@d, $class;
714 }
715  
716 1 ▶sub stats { return $_[0][W_STATS] }
Subroutine stats: called CC=1 SCAR=0.0
717  
718 sub write {
719 19 ▶ my ($self, $s) = @_;
Subroutine write: called CC=6 SCAR=18.0
720 19 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 19 19
721 19 my $len = length($s);
722 19 $self->[W_STATS]{bytes_in} += $len;
723  
724 19 ▶ unless ($self->[W_CFG]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[2]) 1 18 19
725 1 $self->[W_STATS]{lines_in} += _count_newlines($s);
726 1 return $self->_write_str($s);
727 }
728  
729 18 my $nl = index($s, "\n");
730 18 ▶ if ($nl < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl < 0) 0 18 18
731 0 $self->[W_PENDING] .= $s;
732 0 return $len;
733 }
734  
735 18 my $nl2 = index($s, "\n", $nl + 1);
736 18 ▶ if ($nl2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl2 < 0) 2 16 18
737 2 $self->[W_STATS]{lines_in}++;
738 2 my $line_text = $self->[W_PENDING] . substr($s, 0, $nl);
739 2 $self->[W_PENDING] = substr($s, $nl + 1);
740 2 $self->_feed(JSON::JSONFold::Line->parse($line_text, $self->_parent_kind));
741 2 return $len;
742 }
743  
744 # We have multiple lines - at least 2 new lines in the new buffer
745 16 my @lines = split("\n", $s, -1) ;
746 16 $lines[0] = $self->[W_PENDING] . $lines[0] ;
747 16 $self->[W_PENDING] = pop @lines ;
748 16 for my $part ( @lines ) {
749 167 $self->_feed(JSON::JSONFold::Line->parse($part, $self->_parent_kind));
750  
751 }
752 16 $self->[W_STATS]{lines_in} += @lines;
753  
754 16 return $len;
755 }
756  
757 sub finish {
758 20 ▶ my ($self) = @_;
Subroutine finish: called CC=4 SCAR=16.4
759 20 ▶ if (length $self->[W_PENDING]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->[3]) 0 20 20
760 0 $self->_feed(JSON::JSONFold::Line->parse($self->[W_PENDING], $self->_parent_kind));
761 0 $self->[W_PENDING] = '';
762 }
763  
764 20 for my $frame (@{ $self->[W_STACK] }) {
765 0 $self->_write_line($_) for @{ $frame->[F_LINES] };
766 }
767 20 @{ $self->[W_STACK] } = ();
768 }
769  
770 sub flush {
771 20 ▶ my ($self) = @_;
Subroutine flush: called CC=3 SCAR=12.5
772 20 my $fh = $self->[W_FH];
773 20 ▶ $fh->flush if $fh && $fh->can('flush');
Branch: 1 branch
BranchTrueFalseTotal
if $fh and $fh->can("flush") 20 0 20
Condition33%1/3
$fh and $fh->can("flush")
!ll&&!rl&&r
0020
MC/DC0%0/2
$fh and $fh->can("flush")
$fh $fh->can("flush")
Truth table33%1/3
A and B
A= $fhB= $fh->can("flush")
ABresult
0X0
100
111
774 }
775  
776 sub close {
777 1 ▶ my ($self) = @_;
Subroutine close: called CC=2 SCAR=7.0
778 1 $self->finish;
779 1 $self->flush;
780 1 ▶ $self->[W_FH]->close if $self->[W_DO_CLOSE] ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[6] 0 1 1
781 }
782  
783 sub _feed {
784 169 ▶ my ($self, $line) = @_;
Subroutine _feed: called CC=6 SCAR=17.9
785 # Opener
786 169 ▶ if ($line->[L_OPENER]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[6]) 47 122 169
787 47 push @{ $self->[W_STACK] }, JSON::JSONFold::Frame->new(
788 kind => $line->[L_OPENER],
789 47 depth => scalar(@{ $self->[W_STACK] }),
790 lines => [ $line ],
791 pack_limit => $self->_pack_limit($line->[L_OPENER]),
792 fold_limit => $self->_fold_limit($line->[L_OPENER]),
793 join_limit => $self->_join_limit($line->[L_OPENER]),
794 );
795 47 return;
796 }
797  
798 # Closer
799 122 ▶ if ($line->[L_CLOSER]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[7]) 47 75 122
800 47 $self->_close_frame($line, $line->[L_CLOSER]);
801 47 return;
802 }
803  
804 # Regular Line
805 75 ▶ if (@{ $self->[W_STACK] }) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->[4];}) { } 73 2 75
806 73 my $frame = $self->[W_STACK][-1];
807 73 ▶ $line->[L_CAN_PACK] = 0 if $line->[L_ITEMS] >= $frame->[F_PACK_LIMIT];
Branch: 1 branch
BranchTrueFalseTotal
if $line->[3] >= $frame->[4] 15 58 73
808 73 ▶ $line->[L_CAN_JOIN] = 0 if $line->[L_ITEMS] >= $frame->[F_JOIN_LIMIT];
Branch: 1 branch
BranchTrueFalseTotal
if $line->[3] >= $frame->[6] 15 58 73
809 73 $self->_add_to_frame($frame, $line);
810 } else {
811 2 $self->_write_line($line);
812 }
813 75 return ;
814 }
815  
816 sub _emit_lines {
817 79 ▶ my ($self, $lines, $depth) = @_;
Subroutine _emit_lines: called CC=6 SCAR=17.9
818 79 ▶ return unless @$lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lines 0 79 79
819 79 ▶ $depth = @{ $self->[W_STACK] } - 1 unless defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 47 32 79
820  
821 79 ▶ if ($depth < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < 0) 35 44 79
822 35 $self->_write_line($_) for @$lines;
823 return
824 35 }
825  
826 44 my $frame = $self->[W_STACK][$depth];
827 44 $self->_add_to_frame($frame, $_) for @$lines;
828 return
829 44 }
830  
831  
832  
833 sub _add_to_frame {
834 131 ▶ my ($self, $frame, $line) = @_;
Subroutine _add_to_frame: called CC=15 SCAR=27.2
835  
836 131 ▶ if (!$frame->is_empty) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $frame->is_empty) { } 103 28 131
elsif (not $frame->[10] || $line->[9] || $line->[8]) { } 18 10 28
837 103 ▶ return if $line->[L_CAN_PACK] && $self->_try_pack($frame, $line);
Branch: 1 branch
BranchTrueFalseTotal
if $line->[9] and $self->_try_pack($frame, $line) 35 68 103
Condition100%3/3
$line->[9] and $self->_try_pack($frame, $line)
!ll&&!rl&&r
452335
MC/DC100%2/2
$line->[9] and $self->_try_pack($frame, $line)
$line->[9] $self->_try_pack($frame, $line)
Truth table100%3/3
A and B
A= $line->[9]B= $self->_try_pack($frame, $line)
ABresult
0X0
100
111
838 68 ▶ return if $line->[L_CAN_JOIN] && $self->_try_join($frame, $line);
Branch: 1 branch
BranchTrueFalseTotal
if $line->[8] and $self->_try_join($frame, $line) 10 58 68
Condition100%3/3
$line->[8] and $self->_try_join($frame, $line)
!ll&&!rl&&r
233510
MC/DC100%2/2
$line->[8] and $self->_try_join($frame, $line)
$line->[8] $self->_try_join($frame, $line)
Truth table100%3/3
A and B
A= $line->[8]B= $self->_try_join($frame, $line)
ABresult
0X0
100
111
839  
840 # If frame is empty, may be it's in "streaming" mode, which
841 # mean that lines that can not be packed/joined can be sent
842 # directly to the output:
843 ▶ } elsif (!$frame->[F_FOLD_OK] && !$line->[L_CAN_PACK] && !$line->[L_CAN_JOIN]) {
Condition83%5/6
$frame->[10] || $line->[9] || $line->[8]
l!l&&r!l&&!r
4618
$frame->[10] or $line->[9]
l!l&&r!l&&!r
0424
MC/DC66%2/3
$frame->[10] || $line->[9] || $line->[8]
$frame->[10] $line->[9] $line->[8]
Truth table75%3/4
A or B || C
A= $frame->[10]B= $line->[9]C= $line->[8]
ABCresult
1XX1
01X1
0011
0000
844 18 $self->_write_line($line);
845 18 return;
846 }
847  
848 68 push @{ $frame->[F_LINES] }, $line;
849  
850 68 ▶ if ( $frame->[F_FOLD_OK] && $line->width > $self->[W_CFG][C_WIDTH] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[10] and $line->width > $self->[2][1]) 0 68 68
Condition66%2/3
$frame->[10] and $line->width > $self->[2][1]
!ll&&!rl&&r
27410
MC/DC0%0/2
$frame->[10] and $line->width > $self->[2][1]
$frame->[10] $line->width > $self->[2][1]
Truth table66%2/3
A and B
A= $frame->[10]B= $line->width > $self->[2][1]
ABresult
0X0
100
111
851 0 $self->_mark_no_fold ;
852 }
853  
854 68 ▶ unless ($line->[L_CLOSER]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line->[7]) 59 9 68
855 59 $frame->[F_CONTENT_LINES]++;
856 59 $frame->update_add($line) ;
857 59 ▶ if ( $frame->[F_FOLD_OK] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[10]) 41 18 59
858 41 ▶ $self->_mark_no_fold unless $self->_check_fold_limits($frame)
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_check_fold_limits($frame) 15 26 41
859 }
860 }
861  
862 68 ▶ $self->_stream_frame($frame) unless $frame->[F_FOLD_OK];
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->[10] 42 26 68
863 return
864 68 }
865  
866 sub _can_merge {
867 45 ▶ my ($self, $prev, $line, $limit) = @_;
Subroutine _can_merge: called CC=3 SCAR=14.2
868 45 ▶ return $prev->[L_INDENT] == $line->[L_INDENT]
Condition33%2/6
$prev->[0] == $line->[0] && $prev->[3] + $line->[3] <= $limit && $prev->[0] + length($prev->[1]) + 1 + length($line->[1]) <= $self->[2][1]
!ll&&!rl&&r
0045
$prev->[0] == $line->[0] and $prev->[3] + $line->[3] <= $limit
!ll&&!rl&&r
0045
MC/DC0%0/3
$prev->[0] == $line->[0] && $prev->[3] + $line->[3] <= $limit && $prev->[0] + length($prev->[1]) + 1 + length($line->[1]) <= $self->[2][1]
$prev->[0] == $line->[0] $prev->[3] + $line->[3] <= $limit $prev->[0] + length($prev->[1]) + 1 + length($line->[1]) <= $self->[2][1]
Truth table25%1/4
A and B && C
A= $prev->[0] == $line->[0]B= $prev->[3] + $line->[3] <= $limitC= $prev->[0] + length($prev->[1]) + 1 + length($line->[1]) <= $self->[2][1]
ABCresult
0XX0
10X0
1100
1111
869 && $prev->[L_ITEMS] + $line->[L_ITEMS] <= $limit
870 && $prev->[L_INDENT] + length($prev->[L_TEXT]) + 1 + length($line->[L_TEXT]) <= $self->[W_CFG][C_WIDTH];
871 }
872  
873 sub _merge_into_frame {
874 45 ▶ my ($self, $frame, $prev, $line) = @_;
Subroutine _merge_into_frame: called CC=5 SCAR=16.4
875 45 $prev->join_line($line);
876 45 $frame->update_add($line) ;
877  
878 45 ▶ $prev->[L_CAN_PACK] = 0 if $prev->[L_ITEMS] >= $frame->[F_PACK_LIMIT];
Branch: 1 branch
BranchTrueFalseTotal
if $prev->[3] >= $frame->[4] 5 40 45
879 45 ▶ $prev->[L_CAN_JOIN] = 0 if $prev->[L_ITEMS] >= $frame->[F_JOIN_LIMIT];
Branch: 1 branch
BranchTrueFalseTotal
if $prev->[3] >= $frame->[6] 4 41 45
880 45 ▶ if ( $frame->[F_FOLD_OK] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[10]) 35 10 45
881 35 ▶ unless ( $self->_check_fold_limits($frame)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_check_fold_limits($frame)) 0 35 35
882 0 $self->_mark_no_fold ;
883 0 $self->_stream_frame($frame) ;
884 }
885 }
886 }
887  
888 sub _try_pack {
889 58 ▶ my ($self, $frame, $line) = @_;
Subroutine _try_pack: called CC=8 SCAR=23.1
890  
891 58 ▶ return 0 if $frame->[F_PACK_LIMIT] <= 1 || !$line->[L_CAN_PACK] ||
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[4] <= 1 or not $line->[9] or $frame->is_empty 0 58 58
Condition33%2/6
$frame->[4] <= 1 or not $line->[9] or $frame->is_empty
l!l&&r!l&&!r
0058
$frame->[4] <= 1 or not $line->[9]
l!l&&r!l&&!r
0058
MC/DC0%0/3
$frame->[4] <= 1 or not $line->[9] or $frame->is_empty
$frame->[4] <= 1 not $line->[9] $frame->is_empty
Truth table25%1/4
A or not(B) or C
A= $frame->[4] <= 1B= not $line->[9]C= $frame->is_empty
ABCresult
1XX1
01X1
0011
0000
892 $frame->is_empty;
893  
894 58 my $prev = $frame->last_line;
895 58 ▶ return 0 unless $prev->[L_CAN_PACK]
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->[9] and $prev->[5] < $self->[2][4] and $self->_can_merge($prev, $line, $frame->[4]) 0 35 35
Condition66%4/6
$prev->[9] and $prev->[5] < $self->[2][4] and $self->_can_merge($prev, $line, $frame->[4])
!ll&&!rl&&r
23035
$prev->[9] and $prev->[5] < $self->[2][4]
!ll&&!rl&&r
23035
MC/DC33%1/3
$prev->[9] and $prev->[5] < $self->[2][4] and $self->_can_merge($prev, $line, $frame->[4])
$prev->[9] $prev->[5] < $self->[2][4] $self->_can_merge($prev, $line, $frame->[4])
Truth table50%2/4
A and B and C
A= $prev->[9]B= $prev->[5] < $self->[2][4]C= $self->_can_merge($prev, $line, $frame->[4])
ABCresult
0XX0
10X0
1100
1111
896 && $prev->[L_CHILD_NESTING] < $self->[W_CFG][C_PACK_NESTING]
897 && $self->_can_merge($prev, $line, $frame->[F_PACK_LIMIT]);
898 35 $self->_merge_into_frame($frame, $prev, $line);
899 # Disable join, or pack limits reached
900 35 ▶ $prev->[L_CAN_JOIN] = 0 unless $prev->[L_CAN_PACK] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->[9] 1 34 35
901 35 return 1;
902 }
903  
904 sub _try_join {
905 45 ▶ my ($self, $frame, $line) = @_;
Subroutine _try_join: called CC=8 SCAR=22.4
906  
907 45 ▶ return 0 if $frame->[F_JOIN_LIMIT] <= 1 ||
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[6] <= 1 or not $line->[8] or $line->[5] >= $self->[2][10] or $frame->is_empty 2 43 45
Condition55%5/9
$frame->[6] <= 1 or not $line->[8] or $line->[5] >= $self->[2][10] or $frame->is_empty
l!l&&r!l&&!r
2043
$frame->[6] <= 1 or not $line->[8] or $line->[5] >= $self->[2][10]
l!l&&r!l&&!r
0243
$frame->[6] <= 1 or not $line->[8]
l!l&&r!l&&!r
0045
MC/DC25%1/4
$frame->[6] <= 1 or not $line->[8] or $line->[5] >= $self->[2][10] or $frame->is_empty
$frame->[6] <= 1 not $line->[8] $line->[5] >= $self->[2][10] $frame->is_empty
Truth table40%2/5
A or not(B) or C or D
A= $frame->[6] <= 1B= not $line->[8]C= $line->[5] >= $self->[2][10]D= $frame->is_empty
ABCDresult
1XXX1
01XX1
001X1
00011
00000
908 !$line->[L_CAN_JOIN] || $line->[L_CHILD_NESTING] >= $self->[W_CFG][C_JOIN_NESTING] ||
909 $frame->is_empty;
910  
911 43 my $prev = $frame->last_line;
912 43 ▶ return 0 unless $prev->[L_CAN_JOIN]
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->[8] and $prev->[5] < $self->[2][10] and $self->_can_merge($prev, $line, $frame->[6]) 0 10 10
Condition66%4/6
$prev->[8] and $prev->[5] < $self->[2][10] and $self->_can_merge($prev, $line, $frame->[6])
!ll&&!rl&&r
33010
$prev->[8] and $prev->[5] < $self->[2][10]
!ll&&!rl&&r
33010
MC/DC33%1/3
$prev->[8] and $prev->[5] < $self->[2][10] and $self->_can_merge($prev, $line, $frame->[6])
$prev->[8] $prev->[5] < $self->[2][10] $self->_can_merge($prev, $line, $frame->[6])
Truth table50%2/4
A and B and C
A= $prev->[8]B= $prev->[5] < $self->[2][10]C= $self->_can_merge($prev, $line, $frame->[6])
ABCresult
0XX0
10X0
1100
1111
913 && $prev->[L_CHILD_NESTING] < $self->[W_CFG][C_JOIN_NESTING]
914 && $self->_can_merge($prev, $line, $frame->[F_JOIN_LIMIT]);
915 10 $self->_merge_into_frame($frame, $prev, $line);
916 10 return 1;
917 }
918  
919 sub _check_fold_limits {
920 76 ▶ my ($self, $frame) = @_;
Subroutine _check_fold_limits: called CC=4 SCAR=13.9
921  
922 76 ▶ return if $frame->[F_CONTENT_LINES] > 1 ;
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[7] > 1 1 75 76
923 75 ▶ return if $frame->[F_ITEMS] > $frame->[F_FOLD_LIMIT] ;
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[8] > $frame->[5] 4 71 75
924 71 ▶ return if $frame->[F_CHILD_NESTING] >= $self->[W_CFG][C_FOLD_NESTING] ;
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[11] >= $self->[2][7] 10 61 71
925 61 return 1 ;
926 }
927  
928 sub _close_frame {
929 47 ▶ my ($self, $closer, $closing_kind) = @_;
Subroutine _close_frame: called CC=3 SCAR=11.2
930 47 ▶ unless (@{ $self->[W_STACK] }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->[4];}) 0 47 47
931 0 $self->_write_line($closer);
932 0 return;
933 }
934  
935 # Frame is removed/destroyed
936 47 my $frame = pop @{ $self->[W_STACK] };
937 47 my $lines = $frame->[F_LINES] ;
938 47 push @$lines, $closer ;
939 # MAYBE: $frame->[F_FOLD_OK] = 0 if $frame->[F_KIND] != $closing_kind;
940  
941 47 ▶ if ( my $folded = $self->_try_fold($frame)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $folded = $self->_try_fold($frame)) 25 22 47
942 25 $lines = [ $folded ] ;
943 }
944 47 $self->_emit_lines($lines);
945 }
946  
947 sub _try_fold {
948 47 ▶ my ($self, $frame) = @_;
Subroutine _try_fold: called CC=6 SCAR=18.6
949 47 ▶ return undef if !$frame->[F_FOLD_OK] || $frame->[F_CONTENT_LINES] != 1 || @{ $frame->[F_LINES] } != 3;
Branch: 1 branch
BranchTrueFalseTotal
if not $frame->[10] or $frame->[7] != 1 or @{$frame->[3];} != 3 0 25 25
Condition66%4/6
not $frame->[10] or $frame->[7] != 1 or @{$frame->[3];} != 3
l!l&&r!l&&!r
22025
not $frame->[10] or $frame->[7] != 1
l!l&&r!l&&!r
22025
MC/DC33%1/3
not $frame->[10] or $frame->[7] != 1 or @{$frame->[3];} != 3
not $frame->[10] $frame->[7] != 1 @{$frame->[3];} != 3
Truth table50%2/4
not(A) or B or C
A= not $frame->[10]B= $frame->[7] != 1C= @{$frame->[3];} != 3
ABCresult
1XX1
01X1
0011
0000
950  
951 25 my $folded_len = -1;
952 25 $folded_len += 1 + length($_->[L_TEXT]) for @{ $frame->[F_LINES] };
953 25 ▶ return undef if $frame->[F_LINES][0][L_INDENT] + $folded_len > $self->[W_CFG][C_WIDTH];
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[3][0][0] + $folded_len > $self->[2][1] 0 25 25
954  
955 25 return JSON::JSONFold::Line->fold(
956 $frame->[F_LINES],
957 $frame->[F_LEAFS],
958 $frame->[F_CHILD_NESTING],
959 );
960 }
961  
962 sub _stream_frame {
963 42 ▶ my ($self, $frame) = @_;
Subroutine _stream_frame: called CC=6 SCAR=17.9
964 42 my $lines = $frame->[F_LINES];
965 42 ▶ return unless @$lines ;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lines 0 42 42
966  
967 42 my $last = $lines->[-1] ;
968 42 ▶ my $keep_last = $last->[L_CAN_PACK] || $last->[L_CAN_JOIN] ;
Condition100%3/3
$last->[9] || $last->[8]
l!l&&r!l&&!r
81519
MC/DC100%2/2
$last->[9] || $last->[8]
$last->[9] $last->[8]
Truth table100%3/3
A || B
A= $last->[9]B= $last->[8]
ABresult
1X1
011
000
969 42 ▶ pop @$lines if $keep_last ;
Branch: 1 branch
BranchTrueFalseTotal
if $keep_last 23 19 42
970 42 ▶ if ( @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 32 10 42
971 32 $self->_emit_lines($lines, $frame->[F_DEPTH] - 1) ;
972 32 @$lines = ();
973 }
974 42 ▶ push @$lines, $last if $keep_last ;
Branch: 1 branch
BranchTrueFalseTotal
if $keep_last 23 19 42
975 return
976 42 }
977  
978 sub _mark_no_fold {
979 15 ▶ my ($self) = @_;
Subroutine _mark_no_fold: called CC=2 SCAR=6.9
980 15 $_->[F_FOLD_OK] = 0 for @{ $self->[W_STACK] };
981 }
982  
983 sub _write_line {
984 74 ▶ my ($self, $line) = @_ ;
Subroutine _write_line: called CC=1 SCAR=0.0
985 74 return $self->_write_str($line->raw) ;
986 }
987  
988 sub _write_str {
989 75 ▶ my ($self, $s) = @_;
Subroutine _write_str: called CC=1 SCAR=0.0
990  
991 75 $self->[W_FH]->print($s) ;
992 75 $self->[W_STATS]{bytes_out} += length($s);
993 75 $self->[W_STATS]{lines_out} += _count_newlines($s);
994 75 return length($s);
995 }
996  
997 sub _parent_kind {
998 169 ▶ my ($self) = @_ ;
Subroutine _parent_kind: called CC=3 SCAR=11.0
999 169 my $stack = $self->[W_STACK] ;
1000 169 ▶ return unless @$stack ;
Branch: 1 branch
BranchTrueFalseTotal
unless @$stack 18 151 169
1001 151 ▶ return @$stack ? $stack->[-1][F_KIND] : JSON::JSONFold::Kind::KIND_NONE ;
Branch: 1 branch
BranchTrueFalseTotal
@$stack ? : 151 0 151
1002 }
1003  
1004 sub _choose_limit {
1005 141 ▶ my ($kind, $list, $dict) = @_;
Subroutine _choose_limit: called CC=3 SCAR=11.1
1006 141 ▶ return $kind == JSON::JSONFold::Kind::KIND_LIST() ? $list
Branch: 2 branches
BranchTrueFalseTotal
$kind == 2 ? : 57 84 141
$kind == 1 ? : 84 0 84
1007 : $kind == JSON::JSONFold::Kind::KIND_DICT() ? $dict
1008 : 0;
1009 }
1010 47 ▶sub _pack_limit { _choose_limit($_[1], $_[0][W_CFG][C_PACK_ARRAY_ITEMS], $_[0][W_CFG][C_PACK_OBJ_ITEMS]) }
Subroutine _pack_limit: called CC=1 SCAR=0.0
1011 47 ▶sub _fold_limit { _choose_limit($_[1], $_[0][W_CFG][C_FOLD_ARRAY_ITEMS], $_[0][W_CFG][C_FOLD_OBJ_ITEMS]) }
Subroutine _fold_limit: called CC=1 SCAR=0.0
1012 47 ▶sub _join_limit { _choose_limit($_[1], $_[0][W_CFG][C_JOIN_ARRAY_ITEMS], $_[0][W_CFG][C_JOIN_OBJ_ITEMS]) }
Subroutine _join_limit: called CC=1 SCAR=0.0
1013 76 ▶sub _count_newlines { return ($_[0] =~ tr/\n//) }
Subroutine _count_newlines: called CC=1 SCAR=0.0
1014  
1015 package JSON::JSONFold::CLI ;
1016  
1017 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
1018  
1019 5 our @EXPORT_OK = qw(
1020 demo_data
1021 run
1022 ) ;
1023  
1024 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1025 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1026  
1027 sub setup {
1028 0 ▶ require Carp ;
Subroutine setup: not called CC=1 SCAR=6.9
1029  
1030 $SIG{__DIE__} = sub {
1031 0 ▶ return if $^S;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 0 0
1032 0 local $SIG{__DIE__};
1033 0 Carp::confess(@_);
1034 0 };
1035  
1036 $SIG{__WARN__} = sub {
1037 0 ▶ local $SIG{__WARN__};
Subroutine __ANON__: not called CC=1 SCAR=6.9
1038 0 Carp::cluck(@_);
1039 0 };
1040  
1041 0 require Getopt::Long ;
1042  
1043 }
1044  
1045 sub demo_data {
1046 return {
1047 meta => { version => 1, ok => JSON::PP::true, name => "jsonfold demo" },
1048 ids => [ 1, 2, 3, 4, 5, 6 ],
1049 items => [ { id => 1, name => "alpha" }, { id => 2, name => "beta" }, ],
1050 matrix => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
1051 long => [
1052 "this is a long message that may force the block to stay expanded",
1053 "second",
1054 "third",
1055 "fourth",
1056 ],
1057 "single-array" => [1],
1058 "single-obj" => { x => 2 },
1059 0 long_array => [ map { "a$_" } 1..50 ],
1060 0 wide_array => [ map { "abcdefghijklmnopqrstuvwxyz$_" } 1 .. 9 ],
1061 0 ▶ wide_object => { map { ; "abcdefghijk$_" => "lmnopqrstuvwxyz$_" } 1 .. 9 },
Subroutine demo_data: not called CC=1 SCAR=6.9
1062  
1063 };
1064 }
1065  
1066 sub parse_options {
1067 0 ▶ my %opt = (
Subroutine parse_options: not called CC=2 SCAR=17.9
1068 compact => 'default',
1069 indent => 2,
1070 sort_keys => 1,
1071 );
1072  
1073 Getopt::Long::GetOptions(
1074 'demo' => \$opt{demo},
1075 'verbose|v' => \$opt{verbose},
1076 'help|h' => \$opt{help},
1077 'input|i=s' => \$opt{input},
1078 'compact=s' => \$opt{compact},
1079 'indent=i' => \$opt{indent},
1080 'sort-keys!' => \$opt{sort_keys},
1081  
1082 'width=i' => \$opt{width},
1083 0 ▶ ) or die "Try --help\n";
Branch: 1 branch
BranchTrueFalseTotal
Getopt::Long::GetOptions("demo", \$opt{"demo"}, "verbose|v", \$opt{"verbose"}, "help|h", \$opt{"help"}, "input|i=s", \$opt{"input"}, "compact=s", \$opt{"compact"}, "indent=i", \$opt{"indent"}, "sort-keys!", \$opt{"sort_keys"}, "width=i", \$opt{"width"}) or die "Try --help\n" 0 0 0
1084  
1085 0 return \%opt;
1086 }
1087  
1088 sub usage {
1089 0 ▶ my $out = shift ;
Subroutine usage: not called CC=1 SCAR=6.9
1090 0 $out->print(<<___
1091 Usage: json-jsonfold [options] < input.json
1092  
1093 --demo
1094 --compact=default|none|low|med|high|max|pack|fold|join|off
1095 --width=N
1096 --indent=N
1097 --sort-keys
1098 --input=FILE
1099 ___
1100 ) ;
1101 }
1102  
1103 sub read_input {
1104 0 ▶ my ($input) = @_ ;
Subroutine read_input: not called CC=4 SCAR=30.0
1105  
1106 0 my $json_text;
1107 0 ▶ if (defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input) { } 0 0 0
1108 0 ▶ open my $fh, '<', $input or die "$input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $input or die "${input}: $!\n" 0 0 0
1109 0 local $/;
1110 0 $json_text = <$fh>;
1111 0 ▶ close $fh or die "$input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "${input}: $!\n" 0 0 0
1112 } else {
1113 0 local $/;
1114 0 $json_text = <STDIN>;
1115 }
1116  
1117 0 return JSON::PP->new->allow_nonref->decode($json_text);
1118 }
1119  
1120 sub show_verbose {
1121 0 ▶ require Data::Dumper ;
Subroutine show_verbose: not called CC=1 SCAR=6.9
1122  
1123 0 my ($label) = shift ;
1124 0 my $dumper = new Data::Dumper([])->Terse(1)->Indent(1)->Sortkeys(1)->Pair('=')->Quotekeys(0) ;
1125  
1126 0 my $s = $dumper->Values( \@_)->Dump ;
1127 0 $s =~ s/\s+/ /gsm ;
1128  
1129 0 print STDERR "$label: $s\n" ;
1130  
1131 }
1132  
1133 sub run {
1134 0 ▶ setup() ;
Subroutine run: not called CC=5 SCAR=34.0
1135 0 my $opt = parse_options();
1136  
1137 0 ▶ if ($opt->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"help"}) 0 0 0
1138 0 usage(\*STDOUT);
1139 0 return 0;
1140 }
1141  
1142 0 ▶ my $data = $opt->{demo} ? demo_data() : read_input($opt->{input});
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'demo'} ? : 0 0 0
1143 0 my %cfg ;
1144 0 my $config = JSON::JSONFold::config($opt->{compact}, $opt->{width}, %cfg);
1145 0 my $verbose = $opt->{verbose} ;
1146  
1147 0 ▶ show_verbose("config", { $config->as_hash } ) if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
1148
1149 0 my $info = JSON::JSONFold::write_json($data, \*STDOUT, $opt->{width}, $config, sort_keys => $opt->{sort_keys});
1150  
1151 0 ▶ show_verbose("stats", { % $info }) if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
1152 0 return 0;
1153 }
1154  
1155 5 ▶run() unless caller() ;
Branch: 1 branch
BranchTrueFalseTotal
unless caller() 0 5 5
1156  
1157 5 1;
1158  
1159 __END__
↑ Summary script/jsonfold.pl »