blib/lib/Catmandu/Exporter/JSON.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 81.8% 18 / 22 Conditioncond 41.6% 5 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 2 totaltotal 81.7% 94 / 115 CC 16 SCAR 15.8
CC
16
Cov
87%
_build_json
10.0
add
6.1
commit
4.2
CRAP 4.8
« blib/lib/Catmandu/Fix/sort_field.pm ↑ Summary blib/lib/Catmandu/Exporter/Text.pm »
1 package Catmandu::Exporter::JSON;
2  
3 11 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 10 our $VERSION = '1.0201_02';
6  
7 11 ▶use Cpanel::JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use MooX::Aliases;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 with 'Catmandu::Exporter';
13  
14 6 ▶has line_delimited => (is => 'ro', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
15 25 ▶has array => (is => 'ro', default => sub {1});
Subroutine __ANON__: called CC=1 SCAR=0.0
16 26 ▶has pretty => (is => 'ro', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
17 26 ▶has indent => (is => 'ro', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
18 26 ▶has space_before => (is => 'ro', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
19 26 ▶has space_after => (is => 'ro', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
20 25 ▶has canonical => (is => 'ro', default => sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
21 10 has json => (is => 'lazy');
22  
23 sub _build_json {
24 17 ▶ my ($self) = @_;
Subroutine _build_json: called CC=8 SCAR=23.0
25 17 ▶ Cpanel::JSON::XS->new->utf8(0)
Branch: 4 branches
BranchTrueFalseTotal
$self->line_delimited ? : 11 6 17
$self->line_delimited ? : 11 6 17
$self->line_delimited ? : 11 6 17
$self->line_delimited ? : 11 6 17
Condition33%3/9
$self->pretty or $self->indent
l!l&&r!l&&!r
006
$self->pretty or $self->space_before
l!l&&r!l&&!r
006
$self->pretty or $self->space_after
l!l&&r!l&&!r
006
MC/DC0%0/2
$self->pretty or $self->indent
$self->pretty $self->indent
MC/DC0%0/2
$self->pretty or $self->space_before
$self->pretty $self->space_before
MC/DC0%0/2
$self->pretty or $self->space_after
$self->pretty $self->space_after
Truth table33%1/3
A or B
A= $self->prettyB= $self->indent
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $self->prettyB= $self->space_before
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $self->prettyB= $self->space_after
ABresult
1X1
011
000
26 ->allow_nonref->pretty($self->line_delimited ? 0 : $self->pretty)
27 ->indent($self->line_delimited ? 0 : $self->pretty || $self->indent)
28 ->space_before($self->line_delimited ? 0 : $self->pretty
29 || $self->space_before)
30 ->space_after($self->line_delimited ? 0 : $self->pretty
31 || $self->space_after)->canonical($self->canonical);
32 }
33  
34 sub add {
35 200 ▶ my ($self, $data) = @_;
Subroutine add: called CC=6 SCAR=18.0
Pod undocumented
36 200 my $fh = $self->fh;
37 200 my $json = $self->json->encode($data);
38 200 ▶ if ($self->line_delimited) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->line_delimited) 17 182 199
39 18 print $fh $json;
40 18 print $fh "\n";
41 18 return;
42 }
43  
44 183 ▶ if ($self->pretty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pretty) 0 182 182
45 1 chomp $json;
46 }
47 183 ▶ if ($self->array) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->array) 182 0 182
48 183 ▶ if ($self->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->count) { } 176 6 182
49 176 print $fh ",";
50 176 ▶ print $fh "\n" if $self->pretty;
Branch: 1 branch
BranchTrueFalseTotal
if $self->pretty 0 176 176
51 }
52 else {
53 6 print $fh "[";
54 }
55 }
56 182 print $fh $json;
57 }
58  
59 sub commit {
60 25 ▶ my ($self, $data) = @_;
Subroutine commit: called CC=4 SCAR=14.3
Pod undocumented
61 25 ▶ if (!$self->line_delimited && $self->array) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->line_delimited and $self->array) 6 19 25
Condition66%2/3
not $self->line_delimited and $self->array
!ll&&!rl&&r
1906
MC/DC50%1/2
not $self->line_delimited and $self->array
not $self->line_delimited $self->array
Truth table66%2/3
not(A) and B
A= not $self->line_delimitedB= $self->array
ABresult
0X0
100
111
62 6 my $fh = $self->fh;
63 6 ▶ unless ($self->count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->count) 0 6 6
64 0 print $fh "[";
65 }
66 6 print $fh "]\n";
67 }
68 }
69  
70 10 1;
71  
72 __END__
73  
74 =pod
75  
76 =head1 NAME
77  
78 Catmandu::Exporter::JSON - a JSON exporter
79  
80 =head1 SYNOPSIS
81  
82 # From the command line
83  
84 catmandu convert YAML to JSON --pretty 1 < input.yml
85
86 # Export in the line-delimited format
87 catmandu convert YAML to JSON --line_delimited 1 < input.yml
88  
89 # In a Perl script
90  
91 use Catmandu -all;
92  
93 my $exporter = exporter('JSON', fix => 'myfix.txt');
94  
95 $exporter->add_many($arrayref);
96 $exporter->add_many($iterator);
97 $exporter->add_many(sub { });
98  
99 $exporter->add($hashref);
100  
101 printf "exported %d objects\n" , $exporter->count;
102  
103 =head1 DESCRIPTION
104  
105 This L<Catmandu::Exporter> exports items serialized in JSON format. By default
106 each item is printed condensed on one line.
107  
108 =head1 CONFIGURATION
109  
110 =over
111  
112 =item file
113  
114 Write output to a local file given by its path or file handle. Alternatively a
115 scalar reference can be passed to write to a string and a code reference can be
116 used to write to a callback function.
117  
118 =item fh
119  
120 Write the output to an L<IO::Handle>. If not specified,
121 L<Catmandu::Util::io|Catmandu::Util/IO-functions> is used to create the output
122 handle from the C<file> argument or by using STDOUT.
123  
124 =item fix
125  
126 An ARRAY of one or more fixes or file scripts to be applied to exported items.
127  
128 =item encoding
129  
130 Binmode of the output stream C<fh>. Set to "C<:utf8>" by default.
131  
132 =item pretty
133  
134 Pretty-print JSON
135  
136 =item indent
137  
138 =item space_before
139  
140 =item space_after
141  
142 =item canonical
143  
144 L<JSON> serialization options
145  
146 =item array
147  
148 Structure the data as a JSON array. Default is C<1>.
149  
150 =item line_delimited
151  
152 Export objects as newline delimited JSON. Default is C<0>. The C<array>, C<pretty>, C<indent>, C<space_before> and C<space_after> options will be ignored if C<line_delimited> is C<1>.
153  
154 =back
155  
156 =head1 SEE ALSO
157  
158 L<Catmandu::Exporter::YAML>
159  
160 =cut
« blib/lib/Catmandu/Fix/sort_field.pm ↑ Summary blib/lib/Catmandu/Exporter/Text.pm »