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