line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Catmandu::Exporter::JSON; |
2
|
|
|
|
|
|
|
|
3
|
14
|
|
|
14
|
|
228148
|
use Catmandu::Sane; |
|
14
|
|
|
|
|
42
|
|
|
14
|
|
|
|
|
113
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = '1.2020'; |
6
|
|
|
|
|
|
|
|
7
|
14
|
|
|
14
|
|
104
|
use Cpanel::JSON::XS (); |
|
14
|
|
|
|
|
33
|
|
|
14
|
|
|
|
|
284
|
|
8
|
14
|
|
|
14
|
|
85
|
use Moo; |
|
14
|
|
|
|
|
145
|
|
|
14
|
|
|
|
|
111
|
|
9
|
14
|
|
|
14
|
|
5823
|
use namespace::clean; |
|
14
|
|
|
|
|
51
|
|
|
14
|
|
|
|
|
126
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
with 'Catmandu::Exporter'; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
has line_delimited => (is => 'ro', default => sub {0}); |
14
|
|
|
|
|
|
|
has array => (is => 'ro', default => sub {1}); |
15
|
|
|
|
|
|
|
has pretty => (is => 'ro', default => sub {0}); |
16
|
|
|
|
|
|
|
has indent => (is => 'ro', default => sub {0}); |
17
|
|
|
|
|
|
|
has space_before => (is => 'ro', default => sub {0}); |
18
|
|
|
|
|
|
|
has space_after => (is => 'ro', default => sub {0}); |
19
|
|
|
|
|
|
|
has canonical => (is => 'ro', default => sub {0}); |
20
|
|
|
|
|
|
|
has json => (is => 'lazy'); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
sub _build_json { |
23
|
25
|
|
|
25
|
|
270
|
my ($self) = @_; |
24
|
25
|
100
|
33
|
|
|
1178
|
Cpanel::JSON::XS->new->utf8(0) |
|
|
100
|
33
|
|
|
|
|
|
|
100
|
33
|
|
|
|
|
|
|
100
|
|
|
|
|
|
25
|
|
|
|
|
|
|
->allow_nonref->pretty($self->line_delimited ? 0 : $self->pretty) |
26
|
|
|
|
|
|
|
->indent($self->line_delimited ? 0 : $self->pretty || $self->indent) |
27
|
|
|
|
|
|
|
->space_before($self->line_delimited ? 0 : $self->pretty |
28
|
|
|
|
|
|
|
|| $self->space_before) |
29
|
|
|
|
|
|
|
->space_after($self->line_delimited ? 0 : $self->pretty |
30
|
|
|
|
|
|
|
|| $self->space_after)->canonical($self->canonical); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub add { |
34
|
|
|
|
|
|
|
my ($self, $data) = @_; |
35
|
|
|
|
|
|
|
my $fh = $self->fh; |
36
|
|
|
|
|
|
|
my $json = $self->json->encode($data); |
37
|
|
|
|
|
|
|
if ($self->line_delimited) { |
38
|
|
|
|
|
|
|
print $fh $json; |
39
|
|
|
|
|
|
|
print $fh "\n"; |
40
|
|
|
|
|
|
|
return; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
if ($self->pretty) { |
44
|
|
|
|
|
|
|
chomp $json; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
if ($self->array) { |
47
|
|
|
|
|
|
|
if ($self->count) { |
48
|
|
|
|
|
|
|
print $fh ","; |
49
|
|
|
|
|
|
|
print $fh "\n" if $self->pretty; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
else { |
52
|
|
|
|
|
|
|
print $fh "["; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
print $fh $json; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub commit { |
59
|
|
|
|
|
|
|
my ($self, $data) = @_; |
60
|
|
|
|
|
|
|
if (!$self->line_delimited && $self->array) { |
61
|
|
|
|
|
|
|
my $fh = $self->fh; |
62
|
|
|
|
|
|
|
unless ($self->count) { |
63
|
|
|
|
|
|
|
print $fh "["; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
print $fh "]\n"; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
1; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
__END__ |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=pod |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head1 NAME |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
Catmandu::Exporter::JSON - a JSON exporter |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=head1 SYNOPSIS |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
# From the command line |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
catmandu convert YAML to JSON --pretty 1 < input.yml |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Export in the line-delimited format |
86
|
|
|
|
|
|
|
catmandu convert YAML to JSON --line_delimited 1 < input.yml |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
# In a Perl script |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
use Catmandu; |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
my $exporter = Catmandu->exporter('JSON', fix => 'myfix.txt'); |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
$exporter->add_many($arrayref); |
95
|
|
|
|
|
|
|
$exporter->add_many($iterator); |
96
|
|
|
|
|
|
|
$exporter->add_many(sub { }); |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
$exporter->add($hashref); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
printf "exported %d items\n" , $exporter->count; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
=head1 DESCRIPTION |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
This L<Catmandu::Exporter> exports items serialized in JSON format. By default |
105
|
|
|
|
|
|
|
each item is printed condensed on one line. |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 CONFIGURATION |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=over |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=item file |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
Write output to a local file given by its path or file handle. Alternatively a |
114
|
|
|
|
|
|
|
scalar reference can be passed to write to a string and a code reference can be |
115
|
|
|
|
|
|
|
used to write to a callback function. |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=item fh |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Write the output to an L<IO::Handle>. If not specified, |
120
|
|
|
|
|
|
|
L<Catmandu::Util::io|Catmandu::Util/IO-functions> is used to create the output |
121
|
|
|
|
|
|
|
handle from the C<file> argument or by using STDOUT. |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=item fix |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
An ARRAY of one or more fixes or file scripts to be applied to exported items. |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item encoding |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Binmode of the output stream C<fh>. Set to "C<:utf8>" by default. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item pretty |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Pretty-print JSON |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=item indent |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=item space_before |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=item space_after |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=item canonical |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
L<JSON> serialization options |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
=item array |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
Structure the data as a JSON array. Default is C<1>. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item line_delimited |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
Export items 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>. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=back |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 SEE ALSO |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
L<Catmandu::Exporter::YAML> |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |