blib/lib/JSON/Lines.pm

filter: Statementstmt 89.5% 103 / 115 Branchbran 62.9% 34 / 54 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 94.7% 18 / 19 Podpod 100% 10 / 10 totaltotal 82.7% 168 / 203 CC 36 SCAR 15.9
CC
36
Cov
81%
encode
9.2
jsonl
7.3
_decode_line
5.6
CRAP 4.9
↑ Summary
1 package JSON::Lines;
2 8 ▶use 5.006; use strict; use warnings; our $VERSION = '1.00';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use JSON; use base 'Import::Export';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 8 our ($JSON, $LINES, %EX);
6 BEGIN {
7 8 ▶ $JSON = JSON->new;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 $LINES = qr{ ([\[\{] (?: (?> [^\[\]\{\}]+ ) | (??{ $LINES }) )* [\]\}]) }x;
9 8 %EX = (
10 jsonl => [qw/all/]
11 );
12 }
13  
14 sub jsonl {
15 4 ▶ my %args = (scalar @_ == 1 ? %{$_[0]} : @_);
Subroutine jsonl: called CC=6 SCAR=19.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 0 4 4
16 4 my $self = __PACKAGE__->new(%args);
17 return $args{file}
18 ? $self->encode_file($args{file}, $args{data})
19 : $self->encode($args{data})
20 4 ▶ if ($args{encode});
Branch: 2 branches
BranchTrueFalseTotal
if $args{"encode"} 2 2 4
$args{'file'} ? : 0 2 2
21 return $args{file}
22 ? $self->decode_file($args{file})
23 : $self->decode($args{data})
24 2 ▶ if ($args{decode});
Branch: 2 branches
BranchTrueFalseTotal
if $args{"decode"} 2 0 2
$args{'file'} ? : 0 2 2
25 }
26  
27 sub new {
28 10 ▶ my ($pkg, %args) = (shift, scalar @_ == 1 ? %{$_[0]} : @_);
Subroutine new: called CC=5 SCAR=16.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 0 10 10
29 10 my $self = bless { headers => [] }, $pkg;
30 10 ▶ exists $args{$_} && $JSON->$_($args{$_}) for qw/json pretty canonical/;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{$_} and $JSON::Lines::JSON->$_($args{$_}) 9 21 30
31 10 $self->{$_} = $args{$_} for qw/parse_headers error_cb success_cb/;
32 10 $self;
33 }
34  
35 sub encode {
36 11 ▶ my ($self, @data) = (shift, scalar @_ == 1 ? @{$_[0]} : @_);
Subroutine encode: called CC=8 SCAR=22.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ == 1 ? : 7 4 11
37 @data = $self->_parse_headers(@data)
38 11 ▶ if ($self->{parse_headers});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parse_headers"} 1 10 11
39 11 my $stream;
40 11 for (@data) {
41 28 my $json = eval { $JSON->encode($_) };
42 28 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 28 28
43 0 ▶ if ($self->{error_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error_cb'}) { } 0 0 0
44 0 $self->{error_cb}->($@, $_);
45 } else {
46 0 die $@;
47 }
48 } else {
49 28 ▶ $self->{success_cb}->($json, $_) if $self->{success_cb};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"success_cb"} 0 28 28
50 28 ▶ $stream .= $json . ($json =~ m/\n$/ ? "" : "\n");
Branch: 1 branch
BranchTrueFalseTotal
$json =~ /\n$/ ? : 6 22 28
51 }
52 }
53 11 return $self->{stream} = $stream;
54 }
55  
56 sub encode_file {
57 1 ▶ my ($self, $file) = (shift, shift);
Subroutine encode_file: called CC=1 SCAR=0.0
Pod documented
58 1 open my $fh, '>', $file;
59 1 print $fh $self->encode(@_);
60 1 close $fh;
61 1 return $file;
62 }
63  
64 sub decode {
65 9 ▶ my ($self, $string) = @_;
Subroutine decode: called CC=4 SCAR=13.9
Pod documented
66 9 my @lines;
67 push @lines, $self->_decode_line($_)
68 9 for ($string =~ m/$LINES/g);
69 @lines = $self->_deparse_headers(@lines)
70 9 ▶ if ($self->{parse_headers});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parse_headers"} 1 8 9
71 9 ▶ return wantarray ? @lines : \@lines;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 4 5 9
72 }
73  
74 sub decode_file {
75 1 ▶ my ($self, $file) = (shift, shift);
Subroutine decode_file: called CC=1 SCAR=0.0
Pod documented
76 1 open my $fh, '<', $file;
77 1 my $content = do { local $/; <$fh> };
78 1 close $fh;
79 1 return $self->decode($content);
80 }
81  
82 sub add_line {
83 2 ▶ my ($self, $line, $fh) = @_;
Subroutine add_line: called CC=2 SCAR=7.1
Pod documented
84 2 ▶ if (defined $fh) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fh) { } 0 2 2
85 0 print $fh $self->encode([$line]);
86 } else {
87 2 my $stream = $self->{stream};
88 2 my $add = $self->encode([$line]);
89 2 $self->{stream} = $stream . $add;
90 2 $self->{stream};
91 }
92 }
93  
94 sub clear_stream {
95 0 ▶ $_[0]->{stream} = '';
Subroutine clear_stream: not called CC=1 SCAR=6.9
Pod documented
96 }
97  
98 sub get_lines {
99 1 ▶ my ($self, $fh, $lines) = @_;
Subroutine get_lines: called CC=4 SCAR=13.9
Pod documented
100 1 my @lines;
101 1 for (1 .. $lines) {
102 3 my $line = $self->get_line($fh);
103 3 push @lines, $line;
104 3 ▶ last if eof($fh);
Branch: 1 branch
BranchTrueFalseTotal
if eof $fh 1 2 3
105 }
106 1 ▶ return wantarray ? @lines : \@lines;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
107 }
108  
109 sub get_line {
110 6 ▶ my ($self, $fh) = @_;
Subroutine get_line: called CC=3 SCAR=11.0
Pod documented
111 6 my $line = '';
112 6 ▶ $line .= <$fh> while ($line !~ m/^$LINES/ && !eof($fh));
Branch: 1 branch
BranchTrueFalseTotal
$line =~ /^$JSON::Lines::LINES/ or eof $fh or do { $line .= readline $fh } 58 6 64
Condition66%2/3
$line =~ /^$JSON::Lines::LINES/ or eof $fh
l!l&&r!l&&!r
6058
MC/DC50%1/2
$line =~ /^$JSON::Lines::LINES/ or eof $fh
$line =~ /^$JSON::Lines::LINES/ eof $fh
Truth table66%2/3
A or B
A= $line =~ /^$JSON::Lines::LINES/B= eof $fh
ABresult
1X1
011
000
113 6 return $self->_decode_line($line);
114 }
115  
116 sub _decode_line {
117 32 ▶ my ($self, $line) = @_;
Subroutine _decode_line: called CC=4 SCAR=17.3
118 32 my $struct = eval { $JSON->decode($line) };
119 32 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 32 32
120 0 ▶ if ($self->{error_cb}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'error_cb'}) { } 0 0 0
121 0 return $self->{error_cb}->($@, $line);
122 } else {
123 0 die $@;
124 }
125 }
126 32 ▶ return $self->{success_cb}->($struct, $line) if $self->{success_cb};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"success_cb"} 0 32 32
127 32 return $struct;
128 }
129  
130 sub _parse_headers {
131 1 ▶ my ($self, @data) = @_;
Subroutine _parse_headers: called CC=5 SCAR=16.7
132 1 my @headers = @{ $self->{headers} };
133 1 ▶ unless (@headers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@headers) 1 0 1
134 1 ▶ if (ref $data[0] eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data[0] eq 'ARRAY') { } 0 1 1
135 0 @headers = @{ shift @data };
136 } else {
137 1 @headers = sort keys %{ $data[0] };
138 }
139 1 $self->{headers} = \@headers;
140 }
141 1 my @body;
142 1 for my $d (@data) {
143 push @body, (ref $d eq 'ARRAY')
144 ? $d
145 : [
146 map {
147 2 ▶ $d->{$_}
Branch: 1 branch
BranchTrueFalseTotal
ref $d eq 'ARRAY' ? : 0 2 2
148 } @headers
149 ];
150 }
151 return (
152 1 \@headers,
153 @body
154 );
155 }
156  
157 sub _deparse_headers {
158 1 ▶ my ($self, @data) = @_;
Subroutine _deparse_headers: called CC=4 SCAR=14.0
159 1 ▶ return @data unless ref $data[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data[0] eq "ARRAY" 0 1 1
160 1 my @headers = @{ shift @data };
161 1 my @body;
162 1 for my $d (@data) {
163 2 my $i = 0;
164 push @body, (ref $d eq 'HASH')
165 ? $d
166 : {
167 map {
168 2 ▶ $_ => $d->[$i++]
Branch: 1 branch
BranchTrueFalseTotal
ref $d eq 'HASH' ? : 0 2 2
169 } @headers
170 };
171 }
172 1 return @body;
173 }
174  
175 8 1;
176  
177 __END__
↑ Summary