Catmandu/Importer/JSON.pm

filter: Statementstmt 96.3% 26 / 27 Branchbran 50.0% 1 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 92.3% 36 / 39 CC 2 SCAR 2.1
CC
2
Cov
93%
generator
2.1
BEGIN
1.0
BEGIN
1.0
CRAP 1.2
« Catmandu/Fix/Condition/is_string.pm ↑ Summary Catmandu/Fix/Condition/all_match.pm »
1 package Catmandu::Importer::JSON;
2  
3 5 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 5 our $VERSION = '1.2025';
6  
7 5 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 5 with 'Catmandu::Importer';
12  
13 8 ▶has line_delimited => (is => 'ro', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
14 8 ▶has byte_buffer_size => (is => 'ro', default => sub {256});
Subroutine __ANON__: called CC=1 SCAR=0.0
15 5 has json => (is => 'lazy');
16  
17 8 ▶sub _build_json {
Subroutine _build_json: called CC=1 SCAR=0.0
18 8 my ($self) = @_;
19 8 Cpanel::JSON::XS->new->utf8($self->encoding eq ':raw');
20 }
21  
22 8 ▶sub _build_encoding {':raw'}
Subroutine _build_encoding: called CC=1 SCAR=0.0
23  
24 8 ▶sub generator {
Subroutine generator: called CC=2 SCAR=7.6
Pod undocumented
25 8 my ($self) = @_;
26  
27 8 ▶ if ($self->line_delimited) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->line_delimited) 0 8 8
28 return sub {
29 state $json = $self->json;
30 state $fh = $self->fh;
31 if (defined(my $line = <$fh>)) {
32 return $json->decode($line);
33 }
34 return;
35 0 };
36 }
37  
38 # switch to incremental parser
39 sub {
40 state $json = $self->json;
41 state $fh = $self->fh;
42 state $buf_size = $self->byte_buffer_size;
43  
44 for (;;) {
45 my $res = sysread($fh, my $buf, $buf_size);
46 $res // Catmandu::Error->throw($!);
47 $json->incr_parse($buf); # void context, so no parsing
48 $json->incr_text =~ s/^[^{]+//;
49 return if $json->incr_text =~ /^$/;
50 last if $json->incr_text =~ /^{/;
51 }
52  
53 # read data until we get a single json object
54 for (;;) {
55 if (my $data = $json->incr_parse) {
56 return $data;
57 }
58  
59 my $res = sysread($fh, my $buf, $buf_size);
60 $res // Catmandu::Error->throw($!);
61 $res
62 || Catmandu::Error->throw(
63 "JSON syntax error: unexpected end of object");
64 $json->incr_parse($buf);
65 }
66  
67 return;
68  
69 8 };
70 }
71  
72 5 1;
73  
74 __END__
« Catmandu/Fix/Condition/is_string.pm ↑ Summary Catmandu/Fix/Condition/all_match.pm »