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