lib/JSON/JSONFold.pm

filter: Statementstmt 76.2% 548 / 719 Branchbran 60.9% 134 / 220 Conditioncond 61.8% 86 / 139 MC/DCmcdc 35.9% 28 / 78 Subroutinesub 77.6% 101 / 130 Podpod 85.7% 12 / 14 totaltotal 69.9% 909 / 1300 CC 179 SCAR 32.9
CC
179
Cov
71%
_try_grid
298.7
run
30.0
_add_to_frame
22.8
CRAP 26.9
↑ 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.2.0';
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_writer
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.1
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  
54 18 ▶ close $out or die "close output: $!" ;
Branch: 1 branch
BranchTrueFalseTotal
close $out or die "close output: $!" 0 18 18
55 18 ▶ $output .= "\n" unless $output =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $output =~ /\n\z/u 0 18 18
56 18 return $output ;
57 }
58  
59 sub fold {
60 0 ▶ my ($self, $text) = @_ ;
Subroutine fold: not called CC=4 SCAR=30.0
Pod documented
61  
62 0 my $config = $self->{config} ;
63  
64 0 my $output = '' ;
65 0 ▶ open my $out, '>', \$output or die "open output: $!" ;
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">", \$output or die "open output: $!" 0 0 0
66  
67 0 my $stream = _stream($out, $config, 0) ;
68  
69 0 $stream->write($text);
70  
71 0 ▶ close $out or die "close output: $!" ;
Branch: 1 branch
BranchTrueFalseTotal
close $out or die "close output: $!" 0 0 0
72 0 ▶ $output .= "\n" unless $output =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $output =~ /\n\z/u 0 0 0
73 0 return $output ;
74 }
75  
76 sub write {
77 1 ▶ my($self, $data, $fh) = @_ ;
Subroutine write: called CC=1 SCAR=0.0
Pod documented
78  
79 1 my $config = $self->{config} ;
80  
81 1 my $do_close = $self->{do_close} ;
82 1 my $stream = _stream($fh, $config, $do_close) ;
83 1 my $json = $self->{json} ;
84  
85 1 my $text = $json->encode($data) ;
86 1 $stream->write($text);
87 1 $stream->finish;
88 1 $stream->flush;
89 1 my $info = $stream->stats ;
90 1 $stream->close() ;
91  
92 1 return $info ;
93 }
94  
95 # Functional Interface
96  
97 # Not exportable. Allow using DEV::JSONFold::config(...)
98 sub config {
99 0 ▶ my($base_config, $width, %overrides) = @_ ;
Subroutine config: not called CC=2 SCAR=17.9
Pod undocumented
100 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
101 0 return _config($base_config, $width, %overrides) ;
102 }
103  
104 sub jsonfold_config {
105 1 ▶ my($base_config, $width, %overrides) = @_ ;
Subroutine jsonfold_config: called CC=2 SCAR=7.6
Pod documented
106 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
107 1 return _config($base_config, $width, %overrides) ;
108 }
109  
110 sub format_json {
111 0 ▶ my($data, $width, $config, %overrides) = @_ ;
Subroutine format_json: not called CC=1 SCAR=6.9
Pod documented
112  
113 0 my $fmt = __PACKAGE__->new(width => $width, config => $config, %overrides) ;
114 0 my $output = $fmt->format($data) ;
115 0 return $output ;
116 }
117  
118 sub write_json {
119 1 ▶ my($data, $fh, $width, $config, %overrides) = @_ ;
Subroutine write_json: called CC=1 SCAR=0.0
Pod documented
120  
121 1 my $fmt = __PACKAGE__->new(width => $width, config => $config, %overrides) ;
122 1 my $info = $fmt->write($data, $fh) ;
123 1 return $info ;
124 }
125  
126 sub fold_text {
127 0 ▶ my($text, $width, $config) = @_ ;
Subroutine fold_text: not called CC=1 SCAR=6.9
Pod documented
128 0 my $fmt = __PACKAGE__->new(width => $width, config => $config) ;
129  
130 0 return $fmt->fold($text) ;
131 }
132  
133 sub create_writer {
134 0 ▶ my($fh, $width, $config, %overrides) = @_ ;
Subroutine create_writer: not called CC=1 SCAR=6.9
Pod documented
135 0 my $do_close = delete $overrides{close_fp} ;
136 0 return _stream($fh, _config($config, $width, %overrides), $do_close) ;
137 }
138  
139 # Helper for Function/OO interface methods
140  
141 sub _config {
142 20 ▶ my ($preset, $width, %overrides) = @_ ;
Subroutine _config: called CC=2 SCAR=7.1
143 20 ▶ $overrides{width} = $width if defined $width ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $width 0 20 20
144 20 return JSON::JSONFold::Config::config($preset, %overrides) ;
145 }
146  
147 sub _stream {
148 19 ▶ my ($fp, $config, $close_fp) = @_ ;
Subroutine _stream: called CC=1 SCAR=0.0
149 19 return JSON::JSONFold::Writer->new($fp, $config, $close_fp) ;
150 }
151  
152 sub _json_coder {
153 19 ▶ my ($gold, $indent, %opt) = @_;
Subroutine _json_coder: called CC=5 SCAR=16.4
154 # Must have valid indent, otherwise cannot parse the data
155 19 my $json = JSON::PP->new->pretty ;
156 19 ▶ if ( $gold ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gold) 19 0 19
157 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
158 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
159 19 $json->allow_nonref->canonical($sort_keys);
160 19 $json->space_before(0)->space_after(1);
161 }
162 19 ▶ $json->indent_length($indent) if defined $indent ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $indent 19 0 19
163 19 return $json;
164 }
165  
166 # JSON compatible API - OO
167  
168 sub encode {
169 0 ▶ my ($self, $data) = @_ ;
Subroutine encode: not called CC=1 SCAR=6.9
Pod documented
170 0 return $self->format($data) ;
171 }
172  
173  
174 # JSON compatiable API - Functional
175  
176 sub encode_json {
177 18 ▶ my ($data, $opts) = @_ ;
Subroutine encode_json: called CC=2 SCAR=6.9
Pod documented
178 18 ▶ my %overrides = %$opts if $opts ;
Branch: 1 branch
BranchTrueFalseTotal
if $opts 17 1 18
179 18 my $width = delete $overrides{width} ;
180 18 my $compact = delete $overrides{compact} ;
181  
182 18 my $fmt = __PACKAGE__->new(width => $width, config => $compact, %overrides) ;
183 18 my $output = $fmt->format($data) ;
184 18 return $output ;
185 }
186  
187 # Same as encode_json - for users of legacy "JSON" wrapper.
188  
189 sub to_json {
190 0 ▶ my ($data, $opts) = @_ ;
Subroutine to_json: not called CC=2 SCAR=17.9
Pod documented
191 0 ▶ my %overrides = %$opts if $opts ;
Branch: 1 branch
BranchTrueFalseTotal
if $opts 0 0 0
192 0 my $width = delete $overrides{width} ;
193 0 my $compact = delete $overrides{compact} ;
194  
195 0 my $fmt = __PACKAGE__->new(width => $width, config => $compact, %overrides) ;
196 0 my $output = $fmt->format($data) ;
197 0 return $output ;
198 }
199
200 sub run {
201 0 ▶ JSON::JSONFold::CLI::run() ;
Subroutine run: not called CC=1 SCAR=6.9
Pod undocumented
202 }
203  
204 package JSON::JSONFold::Kind;
205  
206 5 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 5 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
208 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
209  
210 5 our @EXPORT_OK = qw(
211 KIND_NONE
212 KIND_DICT
213 KIND_LIST
214 %OPENING_KIND
215 %CLOSING_KIND
216 );
217  
218 5 ▶use constant KIND_NONE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
219 5 ▶use constant KIND_DICT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
220 5 ▶use constant KIND_LIST => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
221  
222 5 our %OPENING_KIND = (
223 '{' => KIND_DICT,
224 '[' => KIND_LIST,
225 );
226  
227 5 our %CLOSING_KIND = (
228 '}' => KIND_DICT,
229 '},' => KIND_DICT,
230 ']' => KIND_LIST,
231 '],' => KIND_LIST,
232 );
233  
234 # -------------------------------------------------------------------------
235 # Internal package: immutable-ish configuration record
236 # -------------------------------------------------------------------------
237  
238 package JSON::JSONFold::Config;
239  
240 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
241 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
242 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 5 ▶use constant DEFAULT_WIDTH => 100 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
245 5 ▶use constant MAX_ARRAY_ITEMS => 1000 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
246 5 ▶use constant MAX_OBJ_ITEMS => 1000 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
247 5 ▶use constant MAX_NESTING => 10 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
248 5 ▶use constant MAX_GRID_LINES => 1000 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
249 5 ▶use constant MAX_WIDTH => 255 ;
Subroutine BEGIN: called CC=1 SCAR=0.0
250  
251 5 our $SEQ = 0 ;
252 use constant {
253 5 C_OFF => $SEQ++,
254 C_WIDTH => $SEQ++,
255  
256 C_PACK_ARRAY_ITEMS => $SEQ++,
257 C_PACK_OBJ_ITEMS => $SEQ++,
258 C_PACK_NESTING => $SEQ++,
259  
260 C_FOLD_ARRAY_ITEMS => $SEQ++,
261 C_FOLD_OBJ_ITEMS => $SEQ++,
262 C_FOLD_NESTING => $SEQ++,
263  
264 C_GRID_ARRAY_ITEMS => $SEQ++,
265 C_GRID_OBJ_ITEMS => $SEQ++,
266 C_GRID_MIN_LINES => $SEQ++,
267 C_GRID_MAX_LINES => $SEQ++,
268 C_GRID_ARRAY_MIN => $SEQ++,
269 C_GRID_OBJ_MIN => $SEQ++,
270  
271 C_JOIN_ARRAY_ITEMS => $SEQ++,
272 C_JOIN_OBJ_ITEMS => $SEQ++,
273 C_JOIN_NESTING => $SEQ++,
274 C_UNUSED_LAST => $SEQ++,
275 5 ▶} ;
Subroutine BEGIN: called CC=1 SCAR=0.0
276  
277 BEGIN {
278  
279 5 ▶ our @EXPORT = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
280 C_OFF
281 C_WIDTH
282 C_PACK_ARRAY_ITEMS C_PACK_OBJ_ITEMS C_PACK_NESTING
283  
284 C_FOLD_ARRAY_ITEMS C_FOLD_OBJ_ITEMS C_FOLD_NESTING
285  
286 C_GRID_ARRAY_ITEMS C_GRID_OBJ_ITEMS C_GRID_MIN_LINES
287 C_GRID_MAX_LINES C_GRID_ARRAY_MIN C_GRID_OBJ_MIN
288  
289 C_JOIN_ARRAY_ITEMS C_JOIN_OBJ_ITEMS C_JOIN_NESTING
290 ) ;
291 }
292  
293 5 our @FIELDS = (
294 [ 'off', C_OFF ],
295 [ 'width', C_WIDTH ],
296 [ 'pack_array_items', C_PACK_ARRAY_ITEMS ],
297 [ 'pack_obj_items', C_PACK_OBJ_ITEMS ],
298 [ 'pack_nesting', C_PACK_NESTING ],
299 [ 'fold_array_items', C_FOLD_ARRAY_ITEMS ],
300 [ 'fold_obj_items', C_FOLD_OBJ_ITEMS ],
301 [ 'fold_nesting', C_FOLD_NESTING ],
302 [ grid_array_items => C_GRID_ARRAY_ITEMS ],
303 [ grid_obj_items => C_GRID_OBJ_ITEMS ],
304 [ grid_min_lines => C_GRID_MIN_LINES ],
305 [ grid_max_lines => C_GRID_MAX_LINES ],
306 [ grid_array_min => C_GRID_ARRAY_MIN ],
307 [ grid_obj_min => C_GRID_OBJ_MIN ],
308 [ 'join_array_items', C_JOIN_ARRAY_ITEMS ],
309 [ 'join_obj_items', C_JOIN_OBJ_ITEMS ],
310 [ 'join_nesting', C_JOIN_NESTING ],
311 ) ;
312  
313 5 our %NAME_TO_INDEX = map { @$_ } @FIELDS ;
314 5 our %PRESETS ;
315 5 our ($NONE, $DEFAULT) ;
316  
317 sub as_hash {
318 0 ▶ my ($self) = @_ ;
Subroutine as_hash: not called CC=1 SCAR=6.9
319 0 map { my ($name, $idx) = @$_ ; ($name => $self->[$idx]) ; } @FIELDS ;
320 }
321  
322 sub _make {
323 15 ▶ my ($class, %arg) = @_;
Subroutine _make: called CC=1 SCAR=0.0
324 15 my @d;
325 15 $#d = $SEQ;
326 15 $d[C_OFF] = $arg{off} ;
327 15 $d[C_WIDTH] = $arg{width};
328  
329 15 $d[C_PACK_ARRAY_ITEMS] = $arg{pack_array_items};
330 15 $d[C_PACK_OBJ_ITEMS] = $arg{pack_obj_items};
331 15 $d[C_PACK_NESTING] = $arg{pack_nesting};
332  
333 15 $d[C_FOLD_ARRAY_ITEMS] = $arg{fold_array_items};
334 15 $d[C_FOLD_OBJ_ITEMS] = $arg{fold_obj_items};
335 15 $d[C_FOLD_NESTING] = $arg{fold_nesting};
336  
337 15 $d[C_GRID_ARRAY_ITEMS] = $arg{grid_array_items} ;
338 15 $d[C_GRID_OBJ_ITEMS] = $arg{grid_obj_items} ;
339 15 $d[C_GRID_MIN_LINES] = $arg{grid_min_lines} ;
340 15 $d[C_GRID_MAX_LINES] = $arg{grid_max_lines} ;
341 15 $d[C_GRID_ARRAY_MIN] = $arg{grid_array_min} ;
342 15 $d[C_GRID_OBJ_MIN] = $arg{grid_obj_min} ;
343  
344 15 $d[C_JOIN_ARRAY_ITEMS] = $arg{join_array_items};
345 15 $d[C_JOIN_OBJ_ITEMS] = $arg{join_obj_items};
346 15 $d[C_JOIN_NESTING] = $arg{join_nesting};
347 15 return bless \@d, $class;
348 }
349  
350 sub _replace {
351 84 ▶ my ($base, $validate) = (shift, shift) ;
Subroutine _replace: called CC=9 SCAR=22.3
352 84 ▶ return $base unless @_ ;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 84 84
353 84 ▶ my $overrides = @_ == 1 && ref($_[0]) ? $_[0] : { @_ } ;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] ? : 39 45 84
Condition66%2/3
@_ == 1 and ref $_[0]
!ll&&!rl&&r
45039
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
354 84 ▶ return $base unless %$overrides ;
Branch: 1 branch
BranchTrueFalseTotal
unless %$overrides 38 46 84
355  
356 46 my @d = @$base ;
357 46 for my $key (keys %$overrides) {
358 276 ▶ unless (exists $NAME_TO_INDEX{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $JSON::JSONFold::Config::NAME_TO_INDEX{$key}) 1 275 276
359 1 ▶ die "unknown JSON::JSONFold config key: $key\n" if $validate ;
Branch: 1 branch
BranchTrueFalseTotal
if $validate 0 1 1
360 1 next ;
361 }
362 275 $d[$NAME_TO_INDEX{$key}] = $overrides->{$key} ;
363 }
364 46 ▶ return bless \@d, ref($base) || __PACKAGE__ ;
Condition50%1/2
ref $base || __PACKAGE__
l!l
460
MC/DC0%0/1
ref $base || __PACKAGE__
ref $base
Truth table50%1/2
A
A= ref $base
Aresult
11
00
365 }
366  
367 sub _resolve_config {
368 39 ▶ my ($config) = @_ ;
Subroutine _resolve_config: called CC=4 SCAR=13.9
369 39 ▶ return $config if ref($config) ;
Branch: 1 branch
BranchTrueFalseTotal
if ref $config 20 19 39
370  
371 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
372 die "unknown JSON::JSONFold preset: $name\n"
373 19 ▶ unless exists $PRESETS{$name} ;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $JSON::JSONFold::Config::PRESETS{$name} 0 19 19
374 19 return $PRESETS{$name} ;
375 }
376  
377 sub config {
378 39 ▶ my ($preset, %overrides) = @_ ;
Subroutine config: called CC=1 SCAR=0.0
379 39 return _replace(_resolve_config($preset), 0, \%overrides) ;
380 }
381  
382 sub new {
383 0 ▶ my ($class, $config, @args) = @_ ;
Subroutine new: not called CC=1 SCAR=6.9
384 0 return config($config, @args) ;
385 }
386  
387 sub _new_preset {
388 45 ▶ my $base = shift ;
Subroutine _new_preset: called CC=1 SCAR=0.0
389 45 return _replace($base, 1, @_) ;
390 }
391  
392 sub _class_init {
393 5 ▶ my $class = shift ;
Subroutine _class_init: called CC=1 SCAR=0.0
394  
395 5 $DEFAULT = $class->_make(
396 width => DEFAULT_WIDTH,
397  
398 pack_array_items => 10,
399 pack_obj_items => 5,
400 pack_nesting => 1,
401  
402 fold_array_items => 10,
403 fold_obj_items => 5,
404 fold_nesting => 2,
405  
406 grid_array_items => MAX_ARRAY_ITEMS,
407 grid_obj_items => MAX_OBJ_ITEMS,
408 grid_min_lines => 3,
409 grid_max_lines => 100,
410 grid_array_min => 3,
411 grid_obj_min => 3,
412  
413 join_array_items => 8,
414 join_obj_items => 4,
415 join_nesting => 1,
416 ) ;
417  
418 5 $NONE = $class->_make(
419 width => DEFAULT_WIDTH,
420  
421 pack_array_items => 0,
422 pack_obj_items => 0,
423 pack_nesting => 0,
424  
425 fold_array_items => 0,
426 fold_obj_items => 0,
427 fold_nesting => 0,
428  
429 grid_array_items => 0,
430 grid_obj_items => 0,
431 grid_min_lines => 0,
432 grid_max_lines => 0,
433 grid_array_min => 0,
434 grid_obj_min => 0,
435  
436 join_array_items => 0,
437 join_obj_items => 0,
438 join_nesting => 0,
439 ) ;
440  
441 5 my %pack_max = (
442 pack_array_items => MAX_ARRAY_ITEMS,
443 pack_obj_items => MAX_OBJ_ITEMS,
444 pack_nesting => MAX_NESTING,
445 ) ;
446 5 my %fold_max = (
447 fold_array_items => MAX_ARRAY_ITEMS,
448 fold_obj_items => MAX_OBJ_ITEMS,
449 fold_nesting => MAX_NESTING,
450 ) ;
451 5 my %join_max = (
452 join_array_items => MAX_ARRAY_ITEMS,
453 join_obj_items => MAX_OBJ_ITEMS,
454 join_nesting => MAX_NESTING,
455 ) ;
456 5 my %grid_max = (
457 grid_array_items => MAX_ARRAY_ITEMS,
458 grid_obj_items => MAX_OBJ_ITEMS,
459 grid_min_lines => 3,
460 grid_max_lines => MAX_GRID_LINES,
461 ) ;
462  
463 5 %PRESETS = (
464 off => $class->_make(off => 1),
465 '' => $DEFAULT,
466 default => $DEFAULT,
467 none => $NONE,
468  
469 low => _new_preset($DEFAULT,
470 fold_nesting => 0,
471 join_nesting => 0,
472 grid_max_lines => 0,
473 ),
474 med => _new_preset($DEFAULT,
475 join_nesting => 0,
476 grid_max_lines => 0,
477 ),
478 classic => _new_preset($DEFAULT,
479 grid_max_lines => 0,
480 ),
481 high => _new_preset($DEFAULT,
482 pack_array_items => 20, pack_obj_items => 10, pack_nesting => 4,
483 fold_array_items => 20, fold_obj_items => 10, fold_nesting => 4,
484 grid_array_min => 4, grid_obj_min => 4,
485 join_array_items => 16, join_obj_items => 8, join_nesting => 2,
486 ),
487 max => _new_preset($DEFAULT,
488 width => MAX_WIDTH,
489 %pack_max, %fold_max, %join_max, %grid_max,
490 grid_array_min => 4,
491 grid_obj_min => 4,
492 ),
493 pack => _new_preset($NONE, %pack_max),
494 fold => _new_preset($NONE, %fold_max),
495 grid => _new_preset($NONE, %pack_max, %fold_max, %grid_max),
496 join => _new_preset($NONE,
497 %fold_max,
498 join_array_items => MAX_ARRAY_ITEMS,
499 join_obj_items => MAX_OBJ_ITEMS,
500 join_nesting => MAX_NESTING,
501 ),
502 ) ;
503 }
504  
505 5 __PACKAGE__->_class_init ;
506  
507 # -------------------------------------------------------------------------
508 # Internal package: one physical pretty-printed line
509 # -------------------------------------------------------------------------
510  
511 package JSON::JSONFold::Line ;
512  
513 5 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
514 5 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
515 5 ▶use Exporter 'import' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
516  
517 5 ▶use constant KIND_NONE => $JSON::JSONFold::Kind::KIND_NONE ;
Subroutine BEGIN: called CC=1 SCAR=0.0
518 5 ▶use constant KIND_DICT => $JSON::JSONFold::Kind::KIND_DICT ;
Subroutine BEGIN: called CC=1 SCAR=0.0
519 5 ▶use constant KIND_LIST => $JSON::JSONFold::Kind::KIND_LIST ;
Subroutine BEGIN: called CC=1 SCAR=0.0
520  
521 5 our $SEQ = 0 ;
522 use constant {
523 5 L_INDENT => $SEQ++,
524 L_PARTS => $SEQ++,
525 L_PARTS_LENGTH => $SEQ++,
526 L_KIND => $SEQ++,
527 L_ITEMS => $SEQ++,
528 L_LEAFS => $SEQ++,
529 L_CHILD_NESTING => $SEQ++,
530 L_OPENER => $SEQ++,
531 L_CLOSER => $SEQ++,
532 L_CAN_JOIN => $SEQ++,
533 L_CAN_PACK => $SEQ++,
534 L_CAN_GRID => $SEQ++,
535 5 ▶} ;
Subroutine BEGIN: called CC=1 SCAR=0.0
536  
537 BEGIN {
538 5 ▶ our @EXPORT = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
539 L_INDENT
540 L_PARTS
541 L_PARTS_LENGTH
542 L_KIND
543 L_ITEMS
544 L_LEAFS
545 L_CHILD_NESTING
546 L_OPENER
547 L_CLOSER
548 L_CAN_JOIN
549 L_CAN_PACK
550 L_CAN_GRID
551 ) ;
552 }
553  
554 5 my $KEY_RE = qr/^\s*(?:(?:"[^"\\]*")|(?:'[^'\\]*')|(?:[A-Za-z_\$][A-Za-z0-9_\$]*)|)\s*:/ ;
555  
556 sub _calc_parts_length {
557 0 ▶ my ($parts) = @_ ;
Subroutine _calc_parts_length: not called CC=3 SCAR=24.8
558 0 ▶ return 0 unless @$parts ;
Branch: 1 branch
BranchTrueFalseTotal
unless @$parts 0 0 0
559 0 my $n = -1 ;
560 0 $n += 1 + length($_) for @$parts ;
561 0 return $n ;
562 }
563  
564 sub parse {
565 169 ▶ my ($class, $s) = @_ ;
Subroutine parse: called CC=8 SCAR=20.8
566  
567 169 my ($spaces) = $s =~ /^(\s*)/;
568 169 my $body = substr($s, length($spaces));
569 169 $body =~ s/\s+\z//;
570  
571 169 ▶ my $last = length($body) ? substr($body, -1, 1) : '' ;
Branch: 1 branch
BranchTrueFalseTotal
length $body ? : 169 0 169
572 169 ▶ my $opener = $JSON::JSONFold::Kind::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
573 169 ▶ my $closer = $JSON::JSONFold::Kind::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
574 169 ▶ my $is_body = !$opener && !$closer ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!($opener || $closer) ? : 75 94 169
Condition100%3/3
$opener || $closer
l!l&&r!l&&!r
474775
MC/DC100%2/2
$opener || $closer
$opener $closer
Truth table100%3/3
A || B
A= $openerB= $closer
ABresult
1X1
011
000
575  
576 169 my @d ;
577 169 $#d = $SEQ ;
578 169 $d[L_INDENT] = length($spaces);
579
580 169 $d[L_PARTS] = [ $body ] ;
581 169 $d[L_PARTS_LENGTH] = length($body) ;
582 169 $d[L_KIND] = KIND_NONE ;
583 169 ▶ $d[L_ITEMS] = $is_body ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$is_body ? : 75 94 169
584 169 ▶ $d[L_LEAFS] = $is_body ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$is_body ? : 75 94 169
585 169 $d[L_CHILD_NESTING] = -1;
586 169 $d[L_OPENER] = $opener;
587 169 $d[L_CLOSER] = $closer;
588 169 $d[L_CAN_JOIN] = $is_body;
589 169 $d[L_CAN_PACK] = $is_body;
590 169 $d[L_CAN_GRID] = 0 ;
591  
592 169 return bless \@d, $class ;
593 }
594  
595 sub raw {
596 60 ▶ return (' ' x $_[0][L_INDENT]) . join(' ', @{ $_[0][L_PARTS] }) . "\n"
Subroutine raw: called CC=1 SCAR=0.0
597 }
598  
599 sub width {
600 106 ▶ return $_[0][L_INDENT] + $_[0][L_PARTS_LENGTH]
Subroutine width: called CC=1 SCAR=0.0
601 }
602  
603 sub can_merge {
604 45 ▶ my ($self, $other, $item_limit, $width_limit) = @_ ;
Subroutine can_merge: called CC=3 SCAR=14.2
605 45 ▶ return $self->[L_INDENT] == $other->[L_INDENT]
Condition33%2/6
$self->[0] == $other->[0] && $self->[4] + $other->[4] <= $item_limit && $self->[0] + $self->[2] + 1 + $other->[2] <= $width_limit
!ll&&!rl&&r
0045
$self->[0] == $other->[0] and $self->[4] + $other->[4] <= $item_limit
!ll&&!rl&&r
0045
MC/DC0%0/3
$self->[0] == $other->[0] && $self->[4] + $other->[4] <= $item_limit && $self->[0] + $self->[2] + 1 + $other->[2] <= $width_limit
$self->[0] == $other->[0] $self->[4] + $other->[4] <= $item_limit $self->[0] + $self->[2] + 1 + $other->[2] <= $width_limit
Truth table25%1/4
A and B && C
A= $self->[0] == $other->[0]B= $self->[4] + $other->[4] <= $item_limitC= $self->[0] + $self->[2] + 1 + $other->[2] <= $width_limit
ABCresult
0XX0
10X0
1100
1111
606 && $self->[L_ITEMS] + $other->[L_ITEMS] <= $item_limit
607 && $self->[L_INDENT] + $self->[L_PARTS_LENGTH] + 1 + $other->[L_PARTS_LENGTH] <= $width_limit ;
608 }
609  
610 sub merge_line {
611 45 ▶ my ($self, $other) = @_;
Subroutine merge_line: called CC=3 SCAR=11.4
612 45 push @{ $self->[L_PARTS] }, @{ $other->[L_PARTS] } ;
613 45 ▶ $self->[L_PARTS_LENGTH] += 1 + $other->[L_PARTS_LENGTH] if @{ $other->[L_PARTS] } ;
Branch: 1 branch
BranchTrueFalseTotal
if @{$other->[1];} 45 0 45
614 45 $self->[L_ITEMS] += $other->[L_ITEMS];
615 45 $self->[L_LEAFS] += $other->[L_LEAFS];
616 45 ▶ if ($other->[L_CHILD_NESTING] > $self->[L_CHILD_NESTING]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($other->[6] > $self->[6]) 0 45 45
617 0 $self->[L_CHILD_NESTING] = $other->[L_CHILD_NESTING];
618 0 $self->[L_CAN_PACK] = 0;
619 }
620 45 return $self;
621 }
622  
623 sub set_parts {
624 0 ▶ my ($self, $parts) = @_ ;
Subroutine set_parts: not called CC=1 SCAR=6.9
625 0 $self->[L_PARTS] = $parts ;
626 0 $self->[L_PARTS_LENGTH] = _calc_parts_length($parts) ;
627 0 return $self ;
628 }
629  
630 sub dict_signature {
631 0 ▶ my ($self) = @_ ;
Subroutine dict_signature: not called CC=4 SCAR=30.0
632 0 my @parts = @{ $self->[L_PARTS] } ;
633 0 ▶ return undef if @parts < 3 ;
Branch: 1 branch
BranchTrueFalseTotal
if @parts < 3 0 0 0
634  
635 0 my @signature ;
636 0 for my $part (@parts[1 .. $#parts - 1]) {
637 0 ▶ return undef unless $part =~ /($KEY_RE)/ ;
Branch: 1 branch
BranchTrueFalseTotal
unless $part =~ /($KEY_RE)/u 0 0 0
638 0 push @signature, $1 ;
639 }
640 0 return join("\x1e", @signature) ;
641 }
642  
643 sub _format_parts {
644 0 ▶ my ($parts, $widths) = @_ ;
Subroutine _format_parts: not called CC=4 SCAR=30.0
645 0 my $last = $#$widths ;
646 0 my @out ;
647 0 for my $i (0 .. $#$parts) {
648 0 my $part = $parts->[$i] ;
649 0 my $w = $widths->[$i] ;
650 0 ▶ if ($part =~ /^[\-0-9]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($part =~ /^[\-0-9]/u) { } 0 0 0
elsif ($i < $last) { } 0 0 0
651 0 push @out, sprintf("%*s", $w, $part) ;
652 }
653 elsif ($i < $last) {
654 0 push @out, sprintf("%-*s", $w, $part) ;
655 }
656 else {
657 0 push @out, $part ;
658 }
659 }
660 0 return \@out ;
661 }
662  
663 sub apply_grid {
664 0 ▶ my ($self, $widths) = @_ ;
Subroutine apply_grid: not called CC=1 SCAR=6.9
665 0 return $self->set_parts(_format_parts($self->[L_PARTS], $widths)) ;
666 }
667  
668 # -------------------------------------------------------------------------
669 # Internal package: stack frame for a currently open JSON container
670 # -------------------------------------------------------------------------
671  
672  
673  
674 package JSON::JSONFold::Frame;
675 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
676 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
677 5 ▶use Exporter 'import' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
678  
679 BEGIN {
680 5 ▶ JSON::JSONFold::Line->import() ;
Subroutine BEGIN: called CC=1 SCAR=0.0
681 }
682  
683 5 our $SEQ = 0 ;
684 use constant {
685 5 F_KIND => $SEQ++,
686 F_INDENT => $SEQ++,
687 F_DEPTH => $SEQ++,
688 F_LINES => $SEQ++,
689 F_PARTS_LENGTH => $SEQ++,
690 F_PACK_LIMIT => $SEQ++,
691 F_FOLD_LIMIT => $SEQ++,
692 F_JOIN_LIMIT => $SEQ++,
693 F_GRID_LIMIT => $SEQ++,
694 F_GRID_MIN_ITEMS => $SEQ++,
695 F_CONTENT_LINES => $SEQ++,
696 F_ITEMS => $SEQ++,
697 F_LEAFS => $SEQ++,
698 F_FOLD_OK => $SEQ++,
699 F_GRID_OK => $SEQ++,
700 F_CHILD_NESTING => $SEQ++,
701 5 ▶} ;
Subroutine BEGIN: called CC=1 SCAR=0.0
702  
703 BEGIN {
704 5 ▶ our @EXPORT = qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
705 F_KIND F_INDENT F_DEPTH F_LINES F_PARTS_LENGTH
706 F_PACK_LIMIT F_FOLD_LIMIT F_JOIN_LIMIT F_GRID_LIMIT F_GRID_MIN_ITEMS
707 F_CONTENT_LINES F_ITEMS F_LEAFS F_FOLD_OK F_GRID_OK F_CHILD_NESTING
708 ) ;
709 }
710  
711 sub new {
712 47 ▶ my ($class, %arg) = @_;
Subroutine new: called CC=10 SCAR=24.3
713 47 my @d ;
714 47 $#d = $SEQ ;
715 47 ▶ $d[F_KIND] = $arg{kind} // 0 ;
Condition50%1/2
$arg{"kind"} // 0
l!l
470
MC/DC0%0/1
$arg{"kind"} // 0
$arg{"kind"}
Truth table50%1/2
A
A= $arg{"kind"}
Aresult
11
00
716 47 ▶ $d[F_INDENT] = $arg{indent} // 0 ;
Condition50%1/2
$arg{"indent"} // 0
l!l
470
MC/DC0%0/1
$arg{"indent"} // 0
$arg{"indent"}
Truth table50%1/2
A
A= $arg{"indent"}
Aresult
11
00
717 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
718 47 ▶ $d[F_LINES] = $arg{lines} // [];
Condition50%1/2
$arg{"lines"} // []
l!l
047
MC/DC0%0/1
$arg{"lines"} // []
$arg{"lines"}
Truth table50%1/2
A
A= $arg{"lines"}
Aresult
11
00
719 47 $d[F_PARTS_LENGTH] = 0 ;
720 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
721 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
722 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
723 47 ▶ $d[F_GRID_LIMIT] = $arg{grid_limit} // 0 ;
Condition50%1/2
$arg{"grid_limit"} // 0
l!l
470
MC/DC0%0/1
$arg{"grid_limit"} // 0
$arg{"grid_limit"}
Truth table50%1/2
A
A= $arg{"grid_limit"}
Aresult
11
00
724 47 ▶ $d[F_GRID_MIN_ITEMS] = $arg{grid_min_items} // 0 ;
Condition50%1/2
$arg{"grid_min_items"} // 0
l!l
470
MC/DC0%0/1
$arg{"grid_min_items"} // 0
$arg{"grid_min_items"}
Truth table50%1/2
A
A= $arg{"grid_min_items"}
Aresult
11
00
725 47 $d[F_CONTENT_LINES] = 0;
726 47 $d[F_ITEMS] = 0;
727 47 $d[F_LEAFS] = 0;
728 47 $d[F_FOLD_OK] = 1;
729 47 $d[F_GRID_OK] = 0 ;
730 47 $d[F_CHILD_NESTING] = -1;
731 47 return bless \@d, $class;
732 }
733  
734 124 ▶sub is_empty { return @{ $_[0][F_LINES] } == 0 }
Subroutine is_empty: called CC=1 SCAR=0.0
735 90 ▶sub last_line { return $_[0][F_LINES][-1] }
Subroutine last_line: called CC=1 SCAR=0.0
736  
737 sub update_stats {
738 205 ▶ my ($self, $line) = @_ ;
Subroutine update_stats: called CC=3 SCAR=11.0
739 205 $self->[F_ITEMS] += $line->[L_ITEMS];
740 205 $self->[F_LEAFS] += $line->[L_LEAFS];
741 205 ▶ $self->[F_PARTS_LENGTH] += $line->[L_PARTS_LENGTH] + ($self->[F_PARTS_LENGTH] ? 1 : 0) ;
Branch: 1 branch
BranchTrueFalseTotal
$self->[4] ? : 158 47 205
742 205 ▶ if ($line->[L_CHILD_NESTING] >= $self->[F_CHILD_NESTING]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[6] >= $self->[15]) 61 144 205
743 61 $self->[F_CHILD_NESTING] = $line->[L_CHILD_NESTING] + 1;
744 }
745 205 return ;
746 }
747  
748 sub add_line {
749 166 ▶ my ($self, $line) = @_ ;
Subroutine add_line: called CC=3 SCAR=11.0
750 166 push @{ $self->[F_LINES] }, $line ;
751 166 ▶ if (!$line->[L_OPENER] && !$line->[L_CLOSER]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line->[7] or $line->[8]) 62 104 166
Condition100%3/3
$line->[7] or $line->[8]
l!l&&r!l&&!r
525262
MC/DC100%2/2
$line->[7] or $line->[8]
$line->[7] $line->[8]
Truth table100%3/3
A or B
A= $line->[7]B= $line->[8]
ABresult
1X1
011
000
752 62 $self->[F_CONTENT_LINES]++ ;
753 }
754 166 $self->update_stats($line) ;
755 166 return ;
756 }
757  
758 sub check_fold_limits {
759 101 ▶ my ($self, $cfg) = @_ ;
Subroutine check_fold_limits: called CC=4 SCAR=13.9
760 101 ▶ return 0 if $self->[F_PARTS_LENGTH] > $cfg->[JSON::JSONFold::Config::C_WIDTH] ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[4] > $cfg->[1] 0 101 101
761 101 ▶ return 0 if $self->[F_ITEMS] > $self->[F_FOLD_LIMIT] ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[11] > $self->[6] 4 97 101
762 97 ▶ return 0 if $self->[F_CHILD_NESTING] >= $cfg->[JSON::JSONFold::Config::C_FOLD_NESTING] ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[15] >= $cfg->[7] 5 92 97
763 92 return 1 ;
764 }
765  
766 sub fold_lines {
767 32 ▶ my ($self, $cfg) = @_ ;
Subroutine fold_lines: called CC=4 SCAR=13.9
768 32 my @parts = map { @{ $_->[L_PARTS] } } @{ $self->[F_LINES] } ;
769  
770 32 my @d ;
771 32 $#d = $JSON::JSONFold::Line::SEQ ;
772 32 $d[L_INDENT] = $self->[F_INDENT] ;
773 32 $d[L_PARTS] = \@parts ;
774 32 $d[L_PARTS_LENGTH] = $self->[F_PARTS_LENGTH] ;
775 32 $d[L_KIND] = $self->[F_KIND] ;
776 32 $d[L_ITEMS] = 1 ;
777 32 $d[L_LEAFS] = $self->[F_LEAFS] ;
778 32 $d[L_CHILD_NESTING] = $self->[F_CHILD_NESTING] ;
779 32 $d[L_OPENER] = JSON::JSONFold::Kind::KIND_NONE ;
780 32 $d[L_CLOSER] = JSON::JSONFold::Kind::KIND_NONE ;
781 32 $d[L_CAN_PACK] = 0 ;
782 32 ▶ $d[L_CAN_JOIN] = $self->[F_CHILD_NESTING] < $cfg->[JSON::JSONFold::Config::C_JOIN_NESTING] ? 1 : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$self->[15] < $cfg->[16] ? : 22 10 32
783 32 ▶ $d[L_CAN_GRID] = ($cfg->[JSON::JSONFold::Config::C_GRID_MAX_LINES] > 0
Branch: 1 branch
BranchTrueFalseTotal
$cfg->[11] > 0 && $self->[11] <= $self->[8] ? : 26 6 32
Condition66%2/3
$cfg->[11] > 0 and $self->[11] <= $self->[8]
!ll&&!rl&&r
6026
MC/DC50%1/2
$cfg->[11] > 0 and $self->[11] <= $self->[8]
$cfg->[11] > 0 $self->[11] <= $self->[8]
Truth table66%2/3
A and B
A= $cfg->[11] > 0B= $self->[11] <= $self->[8]
ABresult
0X0
100
111
784 && $self->[F_ITEMS] <= $self->[F_GRID_LIMIT]) ? 1 : 0 ;
785  
786 32 @{ $self->[F_LINES] } = (bless \@d, 'JSON::JSONFold::Line') ;
787 32 return ;
788 }
789  
790 sub join_lines {
791 10 ▶ my ($self, $cfg) = @_ ;
Subroutine join_lines: called CC=8 SCAR=20.8
792 10 my $lines = $self->[F_LINES] ;
793 10 my $n = @$lines ;
794 10 ▶ return if $n < 2 ;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 2 0 10 10
795  
796 10 my $prev = $lines->[0] ;
797 10 my $write_pos = 1 ;
798  
799 10 ▶ for (my $read_pos = 1; $read_pos < $n; $read_pos++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_pos < $n) 26 10 36
800 26 my $line = $lines->[$read_pos] ;
801 26 ▶ if ($prev->[L_CAN_JOIN]
Branch: 1 branch
BranchTrueFalseTotal
if ($prev->[9] and $line->[9] and $prev->can_merge($line, $self->[7], $cfg->[1])) { } 6 20 26
Condition83%5/6
$prev->[9] and $line->[9] and $prev->can_merge($line, $self->[7], $cfg->[1])
!ll&&!rl&&r
2006
$prev->[9] and $line->[9]
!ll&&!rl&&r
1196
MC/DC66%2/3
$prev->[9] and $line->[9] and $prev->can_merge($line, $self->[7], $cfg->[1])
$prev->[9] $line->[9] $prev->can_merge($line, $self->[7], $cfg->[1])
Truth table75%3/4
A and B and C
A= $prev->[9]B= $line->[9]C= $prev->can_merge($line, $self->[7], $cfg->[1])
ABCresult
0XX0
10X0
1100
1111
802 && $line->[L_CAN_JOIN]
803 && $prev->can_merge($line, $self->[F_JOIN_LIMIT], $cfg->[JSON::JSONFold::Config::C_WIDTH])) {
804 6 $prev->merge_line($line) ;
805 6 $prev->[L_CAN_PACK] = 0 ;
806 }
807 else {
808 20 ▶ $lines->[$write_pos] = $line if $read_pos != $write_pos ;
Branch: 1 branch
BranchTrueFalseTotal
if $read_pos != $write_pos 6 14 20
809 20 $prev = $line ;
810 20 $write_pos++ ;
811 }
812 }
813  
814 10 ▶ splice(@$lines, $write_pos) if $write_pos < @$lines ;
Branch: 1 branch
BranchTrueFalseTotal
if $write_pos < @$lines 6 4 10
815 10 $self->[F_CONTENT_LINES] -= ($n - $write_pos) ;
816 10 return ;
817 }
818  
819 # -------------------------------------------------------------------------
820 # Internal package: counters
821 # -------------------------------------------------------------------------
822  
823 package JSON::JSONFold::Stats;
824 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
825 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
826  
827 sub new {
828 19 ▶ my ($class) = @_;
Subroutine new: called CC=1 SCAR=0.0
829 19 return bless {
830 bytes_in => 0,
831 bytes_out => 0,
832 lines_in => 0,
833 lines_out => 0,
834 }, $class;
835 }
836  
837 0 ▶sub bytes_in { $_[0]{bytes_in} }
Subroutine bytes_in: not called CC=1 SCAR=6.9
838 0 ▶sub bytes_out { $_[0]{bytes_out} }
Subroutine bytes_out: not called CC=1 SCAR=6.9
839 0 ▶sub lines_in { $_[0]{lines_in} }
Subroutine lines_in: not called CC=1 SCAR=6.9
840 0 ▶sub lines_out { $_[0]{lines_out} }
Subroutine lines_out: not called CC=1 SCAR=6.9
841  
842 0 ▶sub as_hash { return %{ $_[0] } }
Subroutine as_hash: not called CC=1 SCAR=6.9
843  
844 # -------------------------------------------------------------------------
845 # Internal package: streaming folding filter/writer
846 # -------------------------------------------------------------------------
847  
848 package JSON::JSONFold::Writer;
849 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
850 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
851  
852 BEGIN {
853 5 ▶ JSON::JSONFold::Line->import() ;
Subroutine BEGIN: called CC=1 SCAR=0.0
854 5 JSON::JSONFold::Frame->import() ;
855 5 JSON::JSONFold::Config->import() ;
856 }
857  
858 5 our $SEQ = 0 ;
859 use constant {
860 5 W_UNUSED_FIRST => $SEQ++,
861 W_FH => $SEQ++,
862 W_CFG => $SEQ++,
863 W_PENDING => $SEQ++,
864 W_STACK => $SEQ++,
865 W_STATS => $SEQ++,
866 W_DO_CLOSE => $SEQ++,
867 W_UNUSED_LAST => $SEQ++,
868 5 ▶} ;
Subroutine BEGIN: called CC=1 SCAR=0.0
869  
870 sub new {
871 19 ▶ my ($class, $fh, $config, $do_close) = @_;
Subroutine new: called CC=2 SCAR=6.9
872  
873 19 my $cfg = JSON::JSONFold::Config::config($config) ;
874 19 my @d ;
875 19 $#d = $SEQ ;
876 19 $d[W_FH] = $fh;
877 19 ▶ $d[W_CFG] = $cfg unless $cfg->[C_OFF] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $cfg->[0] 18 1 19
878 19 $d[W_PENDING] = '';
879 19 $d[W_STACK] = [];
880 19 $d[W_STATS] = JSON::JSONFold::Stats->new;
881 19 $d[W_DO_CLOSE] = $do_close;
882 19 return bless \@d, $class;
883 }
884  
885 1 ▶sub stats { return $_[0][W_STATS] }
Subroutine stats: called CC=1 SCAR=0.0
886  
887 sub write {
888 19 ▶ my ($self, $s) = @_;
Subroutine write: called CC=6 SCAR=18.0
889 19 ▶ $s = '' unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 19 19
890 19 my $len = length($s);
891 19 $self->[W_STATS]{bytes_in} += $len;
892  
893 19 ▶ unless ($self->[W_CFG]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[2]) 1 18 19
894 1 $self->[W_STATS]{lines_in} += _count_newlines($s);
895 1 return $self->_write_str($s);
896 }
897  
898 18 my $nl = index($s, "\n");
899 18 ▶ if ($nl < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl < 0) 0 18 18
900 0 $self->[W_PENDING] .= $s;
901 0 return $len;
902 }
903  
904 18 my $nl2 = index($s, "\n", $nl + 1);
905 18 ▶ if ($nl2 < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nl2 < 0) 2 16 18
906 2 $self->[W_STATS]{lines_in}++;
907 2 my $line_text = $self->[W_PENDING] . substr($s, 0, $nl);
908 2 $self->[W_PENDING] = substr($s, $nl + 1);
909 2 $self->_feed(JSON::JSONFold::Line->parse($line_text));
910 2 return $len;
911 }
912  
913 # We have multiple lines - at least 2 new lines in the new buffer
914 16 my @lines = split("\n", $s, -1) ;
915 16 $lines[0] = $self->[W_PENDING] . $lines[0] ;
916 16 $self->[W_PENDING] = pop @lines ;
917 16 for my $part ( @lines ) {
918 167 $self->_feed(JSON::JSONFold::Line->parse($part));
919  
920 }
921 16 $self->[W_STATS]{lines_in} += @lines;
922  
923 16 return $len;
924 }
925  
926 sub finish {
927 2 ▶ my ($self) = @_;
Subroutine finish: called CC=4 SCAR=16.4
928 2 ▶ if (length $self->[W_PENDING]) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->[3]) 0 2 2
929 0 $self->_feed(JSON::JSONFold::Line->parse($self->[W_PENDING], $self->_parent_kind));
930 0 $self->[W_PENDING] = '';
931 }
932  
933 2 for my $frame (@{ $self->[W_STACK] }) {
934 0 $self->_write_line($_) for @{ $frame->[F_LINES] };
935 }
936 2 @{ $self->[W_STACK] } = ();
937 }
938  
939 sub flush {
940 2 ▶ my ($self) = @_;
Subroutine flush: called CC=3 SCAR=12.5
941 2 my $fh = $self->[W_FH];
942 2 ▶ $fh->flush if $fh && $fh->can('flush');
Branch: 1 branch
BranchTrueFalseTotal
if $fh and $fh->can("flush") 2 0 2
Condition33%1/3
$fh and $fh->can("flush")
!ll&&!rl&&r
002
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
943 }
944  
945 sub close {
946 1 ▶ my ($self) = @_;
Subroutine close: called CC=2 SCAR=7.0
947 1 $self->finish;
948 1 $self->flush;
949 1 ▶ $self->[W_FH]->close if $self->[W_DO_CLOSE] ;
Branch: 1 branch
BranchTrueFalseTotal
if $self->[6] 0 1 1
950 }
951  
952 sub _feed {
953 169 ▶ my ($self, $line) = @_;
Subroutine _feed: called CC=8 SCAR=20.9
954 # Opener
955 169 ▶ if ($line->[L_OPENER]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[7]) 47 122 169
956 my $frame = JSON::JSONFold::Frame->new(
957 kind => $line->[L_OPENER],
958 indent => $line->[L_INDENT],
959 47 depth => scalar(@{ $self->[W_STACK] }),
960 pack_limit => $self->_pack_limit($line->[L_OPENER]),
961 fold_limit => $self->_fold_limit($line->[L_OPENER]),
962 join_limit => $self->_join_limit($line->[L_OPENER]),
963 grid_limit => $self->_grid_limit($line->[L_OPENER]),
964 grid_min_items => $self->_grid_min_items($line->[L_OPENER]),
965 ) ;
966 47 $frame->add_line($line) ;
967 47 push @{ $self->[W_STACK] }, $frame ;
968  
969 47 ▶ $self->_mark_no_fold if $line->width > $self->[W_CFG][C_WIDTH] ;
Branch: 1 branch
BranchTrueFalseTotal
if $line->width > $self->[2][1] 0 47 47
970 47 return ;
971 }
972  
973 122 ▶ unless (@{ $self->[W_STACK] }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->[4];}) 2 120 122
974 2 $self->_write_line($line) ;
975 2 return ;
976 }
977  
978 120 my $frame = $self->[W_STACK][-1] ;
979  
980 120 ▶ if ($line->[L_CLOSER]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->[8]) 47 73 120
981 47 ▶ if ($frame->[F_KIND] != $line->[L_CLOSER]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[0] != $line->[8]) 0 47 47
982 0 $frame->[F_FOLD_OK] = 0 ;
983 0 $frame->[F_GRID_OK] = 0 ;
984 }
985 47 $frame->add_line($line) ;
986 47 $self->_close_frame ;
987 47 return ;
988 }
989  
990 73 ▶ $line->[L_CAN_PACK] = 0 if $line->[L_ITEMS] >= $frame->[F_PACK_LIMIT] ;
Branch: 1 branch
BranchTrueFalseTotal
if $line->[4] >= $frame->[5] 15 58 73
991 73 ▶ $line->[L_CAN_JOIN] = 0 if $line->[L_ITEMS] >= $frame->[F_JOIN_LIMIT] ;
Branch: 1 branch
BranchTrueFalseTotal
if $line->[4] >= $frame->[7] 15 58 73
992 73 $self->_add_to_frame($frame, $line) ;
993 73 return ;
994 }
995  
996 sub _emit_lines {
997 62 ▶ my ($self, $lines, $depth) = @_;
Subroutine _emit_lines: called CC=6 SCAR=17.9
998 62 ▶ return unless @$lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lines 0 62 62
999 62 ▶ $depth = @{ $self->[W_STACK] } - 1 unless defined $depth;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $depth 47 15 62
1000  
1001 62 ▶ if ($depth < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth < 0) 24 38 62
1002 24 $self->_write_line($_) for @$lines;
1003 return
1004 24 }
1005  
1006 38 my $frame = $self->[W_STACK][$depth];
1007 38 $self->_add_to_frame($frame, $_) for @$lines;
1008 return
1009 38 }
1010  
1011 sub _add_to_frame {
1012 124 ▶ my ($self, $frame, $line) = @_;
Subroutine _add_to_frame: called CC=21 SCAR=31.3
1013  
1014 124 ▶ if (!$frame->is_empty) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $frame->is_empty) { } 107 17 124
elsif (not $frame->[13] || $line->[10] || $line->[9]) { } 13 4 17
1015 107 ▶ unless ($frame->[F_GRID_OK]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($frame->[14]) 90 17 107
1016 90 my $prev = $frame->last_line ;
1017 90 ▶ return if $line->[L_CAN_PACK] && $prev->[L_CAN_PACK] && $self->_try_pack($frame, $prev, $line) ;
Branch: 1 branch
BranchTrueFalseTotal
if $line->[10] and $prev->[10] and $self->_try_pack($frame, $prev, $line) 35 55 90
Condition83%5/6
$line->[10] and $prev->[10] and $self->_try_pack($frame, $prev, $line)
!ll&&!rl&&r
55035
$line->[10] and $prev->[10]
!ll&&!rl&&r
322335
MC/DC66%2/3
$line->[10] and $prev->[10] and $self->_try_pack($frame, $prev, $line)
$line->[10] $prev->[10] $self->_try_pack($frame, $prev, $line)
Truth table75%3/4
A and B and C
A= $line->[10]B= $prev->[10]C= $self->_try_pack($frame, $prev, $line)
ABCresult
0XX0
10X0
1100
1111
1018 55 ▶ return if $line->[L_CAN_JOIN] && $prev->[L_CAN_JOIN] && $self->_try_join($frame, $prev, $line) ;
Branch: 1 branch
BranchTrueFalseTotal
if $line->[9] and $prev->[9] and $self->_try_join($frame, $prev, $line) 4 51 55
Condition83%5/6
$line->[9] and $prev->[9] and $self->_try_join($frame, $prev, $line)
!ll&&!rl&&r
5104
$line->[9] and $prev->[9]
!ll&&!rl&&r
27244
MC/DC66%2/3
$line->[9] and $prev->[9] and $self->_try_join($frame, $prev, $line)
$line->[9] $prev->[9] $self->_try_join($frame, $prev, $line)
Truth table75%3/4
A and B and C
A= $line->[9]B= $prev->[9]C= $self->_try_join($frame, $prev, $line)
ABCresult
0XX0
10X0
1100
1111
1019 }
1020 # If frame is empty, may be it's in "streaming" mode, which
1021 # mean that lines that can not be packed/joined can be sent
1022 # directly to the output:
1023 ▶ } elsif (
Condition66%4/6
$frame->[13] || $line->[10] || $line->[9]
l!l&&r!l&&!r
4013
$frame->[13] or $line->[10]
l!l&&r!l&&!r
0413
MC/DC33%1/3
$frame->[13] || $line->[10] || $line->[9]
$frame->[13] $line->[10] $line->[9]
Truth table50%2/4
A or B || C
A= $frame->[13]B= $line->[10]C= $line->[9]
ABCresult
1XX1
01X1
0011
0000
1024 !$frame->[F_FOLD_OK] && !$line->[L_CAN_PACK] && !$line->[L_CAN_JOIN]
1025 ) {
1026 13 $self->_write_line($line);
1027 13 return;
1028 }
1029  
1030 72 $frame->add_line($line) ;
1031  
1032 72 ▶ if ( $frame->[F_FOLD_OK] && $line->width > $self->[W_CFG][C_WIDTH] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[13] and $line->width > $self->[2][1]) 0 72 72
Condition66%2/3
$frame->[13] and $line->width > $self->[2][1]
!ll&&!rl&&r
13590
MC/DC0%0/2
$frame->[13] and $line->width > $self->[2][1]
$frame->[13] $line->width > $self->[2][1]
Truth table66%2/3
A and B
A= $frame->[13]B= $line->width > $self->[2][1]
ABresult
0X0
100
111
1033 0 $self->_mark_no_fold ;
1034 }
1035  
1036 72 ▶ unless ($line->[L_CLOSER]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line->[8]) 67 5 72
1037 67 ▶ if ($frame->[F_FOLD_OK] && !$frame->check_fold_limits($self->[W_CFG])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[13] and not $frame->check_fold_limits($self->[2])) 8 59 67
Condition100%3/3
$frame->[13] and not $frame->check_fold_limits($self->[2])
!ll&&!rl&&r
9508
MC/DC100%2/2
$frame->[13] and not $frame->check_fold_limits($self->[2])
$frame->[13] not $frame->check_fold_limits($self->[2])
Truth table100%3/3
A and not(B)
A= $frame->[13]B= not $frame->check_fold_limits($self->[2])
ABresult
0X0
100
111
1038 8 $self->_mark_no_fold ;
1039 }
1040  
1041 67 ▶ if ($frame->[F_GRID_OK] && !$line->[L_CAN_GRID]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[14] and not $line->[11]) 0 67 67
Condition66%2/3
$frame->[14] and not $line->[11]
!ll&&!rl&&r
50170
MC/DC0%0/2
$frame->[14] and not $line->[11]
$frame->[14] not $line->[11]
Truth table66%2/3
A and not(B)
A= $frame->[14]B= not $line->[11]
ABresult
0X0
100
111
1042 0 $self->_mark_no_grid ;
1043 0 $frame->join_lines($self->[W_CFG]) ;
1044 }
1045 }
1046  
1047 72 ▶ $self->_stream_frame($frame) unless $frame->[F_FOLD_OK] || $frame->[F_GRID_OK] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $frame->[13] or $frame->[14] 19 53 72
Condition100%3/3
$frame->[13] or $frame->[14]
l!l&&r!l&&!r
51219
MC/DC100%2/2
$frame->[13] or $frame->[14]
$frame->[13] $frame->[14]
Truth table100%3/3
A or B
A= $frame->[13]B= $frame->[14]
ABresult
1X1
011
000
1048 72 return ;
1049 }
1050  
1051 sub _merge_into_frame {
1052 39 ▶ my ($self, $frame, $prev, $line) = @_;
Subroutine _merge_into_frame: called CC=7 SCAR=20.6
1053 39 $prev->merge_line($line) ;
1054  
1055 39 ▶ $prev->[L_CAN_PACK] = 0
Branch: 1 branch
BranchTrueFalseTotal
if $prev->[4] >= $frame->[5] or $prev->[6] >= $self->[2][4] 4 35 39
Condition66%2/3
$prev->[4] >= $frame->[5] or $prev->[6] >= $self->[2][4]
l!l&&r!l&&!r
4035
MC/DC50%1/2
$prev->[4] >= $frame->[5] or $prev->[6] >= $self->[2][4]
$prev->[4] >= $frame->[5] $prev->[6] >= $self->[2][4]
Truth table66%2/3
A or B
A= $prev->[4] >= $frame->[5]B= $prev->[6] >= $self->[2][4]
ABresult
1X1
011
000
1056 if $prev->[L_ITEMS] >= $frame->[F_PACK_LIMIT]
1057 || $prev->[L_CHILD_NESTING] >= $self->[W_CFG][C_PACK_NESTING] ;
1058  
1059 39 ▶ $prev->[L_CAN_JOIN] = 0
Branch: 1 branch
BranchTrueFalseTotal
if $prev->[4] >= $frame->[7] or $prev->[6] >= $self->[2][16] 4 35 39
Condition66%2/3
$prev->[4] >= $frame->[7] or $prev->[6] >= $self->[2][16]
l!l&&r!l&&!r
4035
MC/DC50%1/2
$prev->[4] >= $frame->[7] or $prev->[6] >= $self->[2][16]
$prev->[4] >= $frame->[7] $prev->[6] >= $self->[2][16]
Truth table66%2/3
A or B
A= $prev->[4] >= $frame->[7]B= $prev->[6] >= $self->[2][16]
ABresult
1X1
011
000
1060 if $prev->[L_ITEMS] >= $frame->[F_JOIN_LIMIT]
1061 || $prev->[L_CHILD_NESTING] >= $self->[W_CFG][C_JOIN_NESTING] ;
1062  
1063 39 $frame->update_stats($line) ;
1064  
1065 39 ▶ if ($frame->[F_FOLD_OK] && !$frame->check_fold_limits($self->[W_CFG])) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[13] and not $frame->check_fold_limits($self->[2])) 0 39 39
Condition66%2/3
$frame->[13] and not $frame->check_fold_limits($self->[2])
!ll&&!rl&&r
6330
MC/DC0%0/2
$frame->[13] and not $frame->check_fold_limits($self->[2])
$frame->[13] not $frame->check_fold_limits($self->[2])
Truth table66%2/3
A and not(B)
A= $frame->[13]B= not $frame->check_fold_limits($self->[2])
ABresult
0X0
100
111
1066 0 $self->_mark_no_fold ;
1067 0 $self->_stream_frame($frame) ;
1068 }
1069 }
1070  
1071 sub _try_pack {
1072 35 ▶ my ($self, $frame, $prev, $line) = @_ ;
Subroutine _try_pack: called CC=4 SCAR=14.5
1073 35 ▶ return 0 if $frame->[F_PACK_LIMIT] <= 1 ;
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[5] <= 1 0 35 35
1074 35 ▶ return 0 unless $prev->can_merge($line, $frame->[F_PACK_LIMIT], $self->[W_CFG][C_WIDTH]) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->can_merge($line, $frame->[5], $self->[2][1]) 0 35 35
1075 35 $self->_merge_into_frame($frame, $prev, $line);
1076 35 ▶ $prev->[L_CAN_JOIN] = 0 unless $prev->[L_CAN_PACK] ;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->[10] 0 35 35
1077 35 return 1;
1078 }
1079  
1080 sub _try_grid {
1081 10 ▶ my ($self, $frame) = @_ ;
Subroutine _try_grid: called CC=21 SCAR=57.0
1082 10 ▶ return 0 if $frame->[F_KIND] != JSON::JSONFold::Kind::KIND_LIST ;
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[0] != 2 9 1 10
1083  
1084 1 my $line_count = @{ $frame->[F_LINES] } - 2 ;
1085 1 ▶ return 0 if $line_count < 2
Branch: 1 branch
BranchTrueFalseTotal
if $line_count < 2 or $line_count < $self->[2][10] or $line_count > $self->[2][11] 1 0 1
Condition33%2/6
$line_count < 2 or $line_count < $self->[2][10] or $line_count > $self->[2][11]
l!l&&r!l&&!r
100
$line_count < 2 or $line_count < $self->[2][10]
l!l&&r!l&&!r
010
MC/DC0%0/3
$line_count < 2 or $line_count < $self->[2][10] or $line_count > $self->[2][11]
$line_count < 2 $line_count < $self->[2][10] $line_count > $self->[2][11]
Truth table25%1/4
A or B or C
A= $line_count < 2B= $line_count < $self->[2][10]C= $line_count > $self->[2][11]
ABCresult
1XX1
01X1
0011
0000
1086 || $line_count < $self->[W_CFG][C_GRID_MIN_LINES]
1087 || $line_count > $self->[W_CFG][C_GRID_MAX_LINES] ;
1088  
1089 0 my @lines = @{ $frame->[F_LINES] }[1 .. @{ $frame->[F_LINES] } - 2] ;
1090 0 ▶ return 0 unless @lines ;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 0 0
1091  
1092 0 my $first = $lines[0] ;
1093 0 my $part_count = @{ $first->[L_PARTS] } ;
1094 0 ▶ return 0 if $part_count < 4 || ($part_count - 2) < $frame->[F_GRID_MIN_ITEMS] ;
Branch: 1 branch
BranchTrueFalseTotal
if $part_count < 4 or $part_count - 2 < $frame->[9] 0 0 0
Condition0%0/3
$part_count < 4 or $part_count - 2 < $frame->[9]
l!l&&r!l&&!r
000
MC/DC0%0/2
$part_count < 4 or $part_count - 2 < $frame->[9]
$part_count < 4 $part_count - 2 < $frame->[9]
Truth table0%0/3
A or B
A= $part_count < 4B= $part_count - 2 < $frame->[9]
ABresult
1X1
011
000
1095  
1096 0 for my $line (@lines) {
1097 0 ▶ return 0 if @{ $line->[L_PARTS] } != $part_count ;
Branch: 1 branch
BranchTrueFalseTotal
if @{$line->[1];} != $part_count 0 0 0
1098 }
1099  
1100 0 ▶ if ($first->[L_KIND] == JSON::JSONFold::Kind::KIND_DICT) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first->[3] == 1) 0 0 0
1101 0 my $sig = $first->dict_signature ;
1102 0 ▶ return 0 unless defined $sig ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sig 0 0 0
1103 0 for my $line (@lines) {
1104 0 my $line_sig = $line->dict_signature ;
1105 0 ▶ return 0 unless defined $line_sig && $line_sig eq $sig ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $line_sig and $line_sig eq $sig 0 0 0
Condition0%0/3
defined $line_sig and $line_sig eq $sig
!ll&&!rl&&r
000
MC/DC0%0/2
defined $line_sig and $line_sig eq $sig
defined $line_sig $line_sig eq $sig
Truth table0%0/3
A and B
A= defined $line_sigB= $line_sig eq $sig
ABresult
0X0
100
111
1106 }
1107 }
1108  
1109 0 my @widths ;
1110 0 for my $i (0 .. $part_count - 1) {
1111 0 my $max = 0 ;
1112 0 for my $line (@lines) {
1113 0 my $len = length($line->[L_PARTS][$i]) ;
1114 0 ▶ $max = $len if $len > $max ;
Branch: 1 branch
BranchTrueFalseTotal
if $len > $max 0 0 0
1115 }
1116 0 push @widths, $max ;
1117 }
1118  
1119 0 my $grided_length = -1 ;
1120 0 $grided_length += 1 + $_ for @widths ;
1121 0 ▶ return 0 if $frame->[F_LINES][0][L_INDENT] + $grided_length > $self->[W_CFG][C_WIDTH] ;
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[3][0][0] + $grided_length > $self->[2][1] 0 0 0
1122  
1123 0 for my $line (@lines) {
1124 0 $line->apply_grid(\@widths) ;
1125 0 $line->[L_CAN_PACK] = 0 ;
1126 0 $line->[L_CAN_JOIN] = 0 ;
1127 0 $line->[L_CAN_GRID] = 0 ;
1128 }
1129 0 return 1 ;
1130 }
1131  
1132 sub _try_join {
1133 4 ▶ my ($self, $frame, $prev, $line) = @_;
Subroutine _try_join: called CC=3 SCAR=11.3
1134 4 ▶ return 0 if $frame->[F_JOIN_LIMIT] <= 1 ;
Branch: 1 branch
BranchTrueFalseTotal
if $frame->[7] <= 1 0 4 4
1135 4 ▶ return 0 unless $prev->can_merge($line, $frame->[F_JOIN_LIMIT], $self->[W_CFG][C_WIDTH]) ;
Branch: 1 branch
BranchTrueFalseTotal
unless $prev->can_merge($line, $frame->[7], $self->[2][1]) 0 4 4
1136 4 $self->_merge_into_frame($frame, $prev, $line);
1137 4 return 1;
1138 }
1139  
1140  
1141  
1142 sub _close_frame {
1143 47 ▶ my ($self) = @_ ;
Subroutine _close_frame: called CC=8 SCAR=20.8
1144  
1145 47 my $frame = pop @{ $self->[W_STACK] };
1146  
1147 47 ▶ if ($frame->[F_GRID_OK]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[14]) 10 37 47
1148 10 ▶ if ($self->_try_grid($frame)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_try_grid($frame)) { } 0 10 10
1149 0 $self->_mark_no_grid ;
1150 }
1151 else {
1152 10 $self->_mark_no_grid ;
1153 10 $frame->join_lines($self->[W_CFG]) ;
1154 10 $frame->[F_FOLD_OK] = $frame->check_fold_limits($self->[W_CFG]) ;
1155 }
1156 }
1157  
1158 47 ▶ if ($frame->[F_FOLD_OK]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->[13]) 35 12 47
1159 35 ▶ if ($self->_try_fold($frame)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_try_fold($frame)) 32 3 35
1160 32 ▶ if (@{ $self->[W_STACK] } && $frame->[F_LINES][0][L_CAN_GRID]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->[4];} and $frame->[3][0][11]) 18 14 32
Condition100%3/3
@{$self->[4];} and $frame->[3][0][11]
!ll&&!rl&&r
9518
MC/DC100%2/2
@{$self->[4];} and $frame->[3][0][11]
@{$self->[4];} $frame->[3][0][11]
Truth table100%3/3
A and B
A= @{$self->[4];}B= $frame->[3][0][11]
ABresult
0X0
100
111
1161 18 my $parent = $self->[W_STACK][-1] ;
1162 18 ▶ $parent->[F_GRID_OK] = 1 if $parent->[F_CONTENT_LINES] == 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $parent->[10] == 0 11 7 18
1163 }
1164 }
1165 }
1166  
1167 47 $self->_emit_lines($frame->[F_LINES]) ;
1168 47 return ;
1169 }
1170  
1171 sub _try_fold {
1172 35 ▶ my ($self, $frame) = @_ ;
Subroutine _try_fold: called CC=5 SCAR=16.8
1173 return 0 if !$frame->[F_FOLD_OK]
1174 || $frame->[F_CONTENT_LINES] != 1
1175 35 ▶ || @{ $frame->[F_LINES] } != 3
Branch: 1 branch
BranchTrueFalseTotal
if not $frame->[13] or $frame->[10] != 1 or @{$frame->[3];} != 3 or $frame->[1] + $frame->[4] > $self->[2][1] 0 32 32
Condition66%6/9
not $frame->[13] or $frame->[10] != 1 or @{$frame->[3];} != 3 or $frame->[1] + $frame->[4] > $self->[2][1]
l!l&&r!l&&!r
3032
not $frame->[13] or $frame->[10] != 1 or @{$frame->[3];} != 3
l!l&&r!l&&!r
3032
not $frame->[13] or $frame->[10] != 1
l!l&&r!l&&!r
0332
MC/DC25%1/4
not $frame->[13] or $frame->[10] != 1 or @{$frame->[3];} != 3 or $frame->[1] + $frame->[4] > $self->[2][1]
not $frame->[13] $frame->[10] != 1 @{$frame->[3];} != 3 $frame->[1] + $frame->[4] > $self->[2][1]
Truth table40%2/5
not(A) or B or C or D
A= not $frame->[13]B= $frame->[10] != 1C= @{$frame->[3];} != 3D= $frame->[1] + $frame->[4] > $self->[2][1]
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1176 || $frame->[F_INDENT] + $frame->[F_PARTS_LENGTH] > $self->[W_CFG][C_WIDTH] ;
1177  
1178 32 $frame->fold_lines($self->[W_CFG]) ;
1179 32 return 1 ;
1180 }
1181  
1182 sub _stream_frame {
1183 19 ▶ my ($self, $frame) = @_;
Subroutine _stream_frame: called CC=6 SCAR=18.0
1184 19 my $lines = $frame->[F_LINES];
1185 19 ▶ return unless @$lines ;
Branch: 1 branch
BranchTrueFalseTotal
unless @$lines 0 19 19
1186  
1187 19 my $last = $lines->[-1] ;
1188 19 ▶ my $keep_last = $last->[L_CAN_PACK] || $last->[L_CAN_JOIN] ;
Condition66%2/3
$last->[10] || $last->[9]
l!l&&r!l&&!r
8011
MC/DC50%1/2
$last->[10] || $last->[9]
$last->[10] $last->[9]
Truth table66%2/3
A || B
A= $last->[10]B= $last->[9]
ABresult
1X1
011
000
1189 19 ▶ pop @$lines if $keep_last ;
Branch: 1 branch
BranchTrueFalseTotal
if $keep_last 8 11 19
1190 19 ▶ if ( @$lines ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$lines) 15 4 19
1191 15 $self->_emit_lines($lines, $frame->[F_DEPTH] - 1) ;
1192 15 @$lines = ();
1193 }
1194 19 ▶ push @$lines, $last if $keep_last ;
Branch: 1 branch
BranchTrueFalseTotal
if $keep_last 8 11 19
1195 return
1196 19 }
1197  
1198 sub _mark_no_fold {
1199 8 ▶ my ($self) = @_;
Subroutine _mark_no_fold: called CC=2 SCAR=6.9
1200 8 $_->[F_FOLD_OK] = 0 for @{ $self->[W_STACK] };
1201 }
1202  
1203 sub _mark_no_grid {
1204 10 ▶ my ($self) = @_ ;
Subroutine _mark_no_grid: called CC=2 SCAR=6.9
1205 10 $_->[F_GRID_OK] = 0 for @{ $self->[W_STACK] } ;
1206 10 return ;
1207 }
1208  
1209 sub _write_line {
1210 60 ▶ my ($self, $line) = @_ ;
Subroutine _write_line: called CC=1 SCAR=0.0
1211 60 $self->[W_STATS]{lines_out} ++ ;
1212 60 return $self->_write_str($line->raw) ;
1213 }
1214  
1215 sub _write_str {
1216 61 ▶ my ($self, $s) = @_;
Subroutine _write_str: called CC=1 SCAR=0.0
1217  
1218 61 $self->[W_FH]->print($s) ;
1219 61 $self->[W_STATS]{bytes_out} += length($s);
1220 61 return length($s);
1221 }
1222  
1223  
1224 sub _choose_limit {
1225 235 ▶ my ($kind, $list, $dict) = @_;
Subroutine _choose_limit: called CC=3 SCAR=11.1
1226 235 ▶ return $kind == JSON::JSONFold::Kind::KIND_LIST() ? $list
Branch: 2 branches
BranchTrueFalseTotal
$kind == 2 ? : 95 140 235
$kind == 1 ? : 140 0 140
1227 : $kind == JSON::JSONFold::Kind::KIND_DICT() ? $dict
1228 : 0;
1229 }
1230  
1231 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
1232 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
1233 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
1234 47 ▶sub _grid_limit { _choose_limit($_[1], $_[0][W_CFG][C_GRID_ARRAY_ITEMS], $_[0][W_CFG][C_GRID_OBJ_ITEMS]) }
Subroutine _grid_limit: called CC=1 SCAR=0.0
1235 47 ▶sub _grid_min_items { _choose_limit($_[1], $_[0][W_CFG][C_GRID_ARRAY_MIN], $_[0][W_CFG][C_GRID_OBJ_MIN]) }
Subroutine _grid_min_items: called CC=1 SCAR=0.0
1236 1 ▶sub _count_newlines { return ($_[0] =~ tr/\n//) }
Subroutine _count_newlines: called CC=1 SCAR=0.0
1237  
1238 # -------------------------------------------------------------------------
1239 # CLI
1240 # -------------------------------------------------------------------------
1241  
1242 package JSON::JSONFold::CLI ;
1243  
1244 5 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
1245 5 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
1246 5 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
1247  
1248 5 our @EXPORT_OK = qw(
1249 demo_data
1250 run
1251 ) ;
1252  
1253 sub setup {
1254 0 ▶ require Carp ;
Subroutine setup: not called CC=1 SCAR=6.9
1255  
1256 $SIG{__DIE__} = sub {
1257 0 ▶ return if $^S;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $^S 0 0 0
1258 0 local $SIG{__DIE__};
1259 0 Carp::confess(@_);
1260 0 };
1261  
1262 $SIG{__WARN__} = sub {
1263 0 ▶ local $SIG{__WARN__};
Subroutine __ANON__: not called CC=1 SCAR=6.9
1264 0 Carp::cluck(@_);
1265 0 };
1266  
1267 0 require Getopt::Long ;
1268  
1269 }
1270  
1271 sub demo_data {
1272 return {
1273 meta => { version => 1, ok => JSON::PP::true, name => "jsonfold demo" },
1274 ids => [ 1, 2, 3, 4, 5, 6 ],
1275 items => [ { id => 1, name => "alpha" }, { id => 2, name => "beta" }, ],
1276 matrix => [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ] ],
1277 long => [
1278 "this is a long message that may force the block to stay expanded",
1279 "second",
1280 "third",
1281 "fourth",
1282 ],
1283 "single-array" => [1],
1284 "single-obj" => { x => 2 },
1285 0 long_array => [ map { "a$_" } 1..50 ],
1286 0 wide_array => [ map { "abcdefghijklmnopqrstuvwxyz$_" } 1 .. 9 ],
1287 0 ▶ wide_object => { map { ; "abcdefghijk$_" => "lmnopqrstuvwxyz$_" } 1 .. 9 },
Subroutine demo_data: not called CC=1 SCAR=6.9
1288  
1289 };
1290 }
1291  
1292 sub parse_options {
1293 0 ▶ my %opt = (
Subroutine parse_options: not called CC=1 SCAR=6.9
1294 compact => 'default',
1295 indent => 2,
1296 sort_keys => 1,
1297 );
1298  
1299 Getopt::Long::GetOptions(
1300 'demo' => \$opt{demo},
1301 'verbose|v' => \$opt{verbose},
1302 'help|h' => \$opt{help},
1303 'input|i=s' => \$opt{input},
1304 'compact=s' => \$opt{compact},
1305 'indent=i' => \$opt{indent},
1306 'sort-keys!' => \$opt{sort_keys},
1307 0 );
1308  
1309 0 return \%opt ;
1310 }
1311  
1312 sub usage {
1313 0 ▶ my $out = shift ;
Subroutine usage: not called CC=1 SCAR=6.9
1314 0 $out->print(<<___
1315 Usage: json-jsonfold [options] < input.json
1316  
1317 --demo
1318 --compact=default|classic|none|low|med|high|max|grid|pack|fold|join|off
1319 --width=N
1320 --indent=N
1321 --sort-keys
1322 --input=FILE
1323 ___
1324 ) ;
1325 }
1326  
1327 sub read_input {
1328 0 ▶ my ($input) = @_ ;
Subroutine read_input: not called CC=4 SCAR=30.0
1329  
1330 0 my $json_text;
1331 0 ▶ if (defined $input) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $input) { } 0 0 0
1332 0 ▶ open my $fh, '<', $input or die "$input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $input or die "${input}: $!\n" 0 0 0
1333 0 local $/;
1334 0 $json_text = <$fh>;
1335 0 ▶ close $fh or die "$input: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "${input}: $!\n" 0 0 0
1336 } else {
1337 0 local $/;
1338 0 $json_text = <STDIN>;
1339 }
1340  
1341 0 return JSON::PP->new->allow_nonref->decode($json_text);
1342 }
1343  
1344 sub show_verbose {
1345 0 ▶ require Data::Dumper ;
Subroutine show_verbose: not called CC=1 SCAR=6.9
1346  
1347 0 my ($label) = shift ;
1348 0 my $dumper = new Data::Dumper([])->Terse(1)->Indent(1)->Sortkeys(1)->Pair('=')->Quotekeys(0) ;
1349  
1350 0 my $s = $dumper->Values( \@_)->Dump ;
1351 0 $s =~ s/\s+/ /gsm ;
1352  
1353 0 print STDERR "$label: $s\n" ;
1354  
1355 }
1356  
1357 sub run {
1358 0 ▶ setup() ;
Subroutine run: not called CC=5 SCAR=34.0
1359 0 my $opt = parse_options();
1360  
1361 0 ▶ if ($opt->{help}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{"help"}) 0 0 0
1362 0 usage(\*STDOUT);
1363 0 return 0;
1364 }
1365  
1366 0 ▶ my $data = $opt->{demo} ? demo_data() : read_input($opt->{input});
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'demo'} ? : 0 0 0
1367 0 my %cfg ;
1368 0 my $config = JSON::JSONFold::config($opt->{compact}, $opt->{width}, %cfg);
1369 0 my $verbose = $opt->{verbose} ;
1370  
1371 0 ▶ show_verbose("config", { $config->as_hash } ) if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
1372
1373 0 my $info = JSON::JSONFold::write_json($data, \*STDOUT, $opt->{width}, $config, sort_keys => $opt->{sort_keys});
1374  
1375 0 ▶ show_verbose("stats", { % $info }) if $verbose ;
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 0 0
1376 0 return 0;
1377 }
1378  
1379 5 ▶run() unless caller() ;
Branch: 1 branch
BranchTrueFalseTotal
unless caller() 0 5 5
1380  
1381 5 1;
1382  
1383 __END__
↑ Summary script/jsonfold.pl »