package Catmandu::Exporter::JSON;
use namespace::clean;
use Catmandu::Sane;
use JSON ();
use Moo;
with 'Catmandu::Exporter';
has pretty => (is => 'ro', default => sub { 0 });
has indent
=> (is => 'ro', default => sub { 0 });
has json => (is => 'ro', lazy => 1, builder => '_build_json');
sub _build_json {
my ($self) = @_;
JSON->new
->utf8(0)
->allow_no
my ($self, $data) = @_;
my $fh = $self->fh;
my $json = $self->json->encode($data);
if ($self->pretty) {
chomp $json;
}
if ($self->array) {
if ($self->count) {
package Catmandu::Fix::to_json;
use Catmandu::Sane;
use JSON ();
use Moo;
use Catmandu::Fix::Has;
with 'Catmandu::Fix::Base';
has path => (fix_arg => 1);
sub emit {
my ($self, $fixer) = @_;
my $path = $fixer->split_path($self->path);
my $key = pop @$path;
my $json_var = $fixer->capture(JSON->new->utf8(0)->pretty(0)->allow_nonref(1));
$fixer->emit_walk_path($fixer->var, $p
"${var} = ${json_var}->encode(${var});" .
"}";
});
});
}
=head1 NAME
Catmandu::Fix::to_json - convert the value of a field to json
=head1 SYNOPSIS
to_json(my.field)
=
tmandu::Importer::JSON;
use namespace::clean;
use Catmandu::Sane;
use JSON ();
use Moo;
with 'Catmandu::Importer';
has json => (is => 'ro', lazy => 1, builder => '_build_json');
has multiline
_json {
my ($self) = @_;
JSON->new->utf8($self->encoding eq ':raw');
}
sub default_encoding { ':raw' }
sub generator {
my ($self) = @_;
$self->multiline ? sub {
state $json
= $self->json;
state $fh = $self->fh;
for (;;) {
my $res = sysread($fh, my $buf, 512);
$res // Catmandu::Error->throw($!);
$json->incr_parse($buf
package Catmandu::Serializer::json;
use Catmandu::Sane;
use JSON ();
use Moo;
sub serialize {
JSON::encode_json($_[1]);
}
sub deserialize {
JSON::decode_json($_[1]);
}
1;
package Catmandu::Fix::from_json;
use Catmandu::Sane;
use JSON ();
use Moo;
use Catmandu::Fix::Has;
with 'Catmandu::Fix::Base';
has path => (fix_arg => 1);
sub emit {
my ($self, $fixer) = @_;
my $path = $fixer->split_path($self->path);
my $key = pop @$path;
my $json_var = $fixer->capture(JSON->new->utf8(0)->pretty(0)->allow_nonref(1));
$fixer->emit_walk_path($fixer->var,
var} = ${json_var}->decode(${var});" .
"}";
});
});
}
=head1 NAME
Catmandu::Fix::from_json - replace a json field with the parsed value
=head1 SYNOPSIS
from_json(my.fiel
the root directory of
your programming project. The file can be YAML, JSON or Perl and is called
C<catmandu.yml>, C<catmandu.json> or C<catmandu.pl>. In this file you can set
the default Catmandu sto
gging methods for the
defined log levels, such as C<debug> or C<error>.
package MyApp::View::JSON;
extends 'MyApp::View';
with 'Catmandu::Logger';
sub bar {
$self->log->info
}
Your package automatically has a logging category of MyApp::View::JSON. Use lines like:
log4perl.logger.MyApp::View::JSON=DEBUG,STDOUT
or
log4perl.logger.MyApp::View=DEBUG,STDOUT
or
andu::Iterable> for all inherited methods.
=head1 SEE ALSO
L<Catmandu::Iterable> , L<Catmandu::Fix> ,
L<Catmandu::Importer::CSV>, L<Catmandu::Importer::JSON> , L<Catmandu::Importer::YAML>
=cut
1;
Catmandu::Env;
use Catmandu::Sane;
use Catmandu::Util qw(require_package use_lib read_yaml read_json :is :check);
use Catmandu::Fix;
use Config::Onion;
use File::Spec;
use Moo;
require Catmandu;
use
f
grep { -f File::Spec->catfile($path, $_) }
grep /^catmandu.+(?:yaml|yml|json|pl)$/,
readdir $dh;
}
Catmandu->default_load_path;
}
has load_paths => (
as default_importer_package => (is => 'ro', default => sub { 'JSON' });
has default_exporter_package => (is => 'ro', default => sub { 'JSON' });
has store_namespace => (is => 'ro', default => sub { '
andu::Util qw(data_at);
use Catmandu;
sub description {
<<EOS;
examples:
# export config to JSON
catmandu config
# or any other Catmandu::Exporter
catmandu config to YAML --fix 'delete_field(pas
Catmandu->exporter($into_args->[0], $into_opts);
} else {
$into = Catmandu->exporter('JSON', pretty => 1);
}
$into->add(defined $path ?
data_at($path, Catmandu->config
data:
plugins:
- Datestamps
$ echo '{"hello":"world"}' | catmandu import JSON to test
$ catmandu export test to YAML
---
_id: ADA305D8-697D-11E3-B0C3-97AD572FA7E3
date_cre
store
$ echo '{"_id":"001",hello":"world"}' | catmandu import JSON to test
$ echo '{"_id":"001",hello":"world2"}' | catmandu import JSON to test
# In the store we see only the latest version
$ c
Handle::Util ();
use File::Spec;
use YAML::XS ();
use JSON ();
our %EXPORT_TAGS = (
io => [qw(io read_file write_file read_yaml read_json join_path
normalize_path segmented_path)],
# dies on error
YAML::XS::LoadFile($_[0]);
}
sub read_json {
my $text = read_file($_[0]);
# dies on error
JSON::decode_json(read_file($_[0]));
}
sub join_path {
my $path = File:
ng the YAML.
=item read_json($path);
Reads the JSON file at C<$path> into a Perl hash.
my $cfg = read_json($path);
Dies on failure reading the file or parsing the JSON.
=item join_path(@path)
d_serializer',
handles => [qw(serialize deserialize)]
);
sub default_serialization_format { 'json' }
sub _build_serializer {
my ($self) = @_;
my $pkg = require_package($self->serializati
orter('Foo', file => "/tmp/output.txt");
# Or on the command line
$ catmandu convert JSON to Foo < /tmp/something.txt >/tmp/output.txt
=head1 DESCRIPTION
A Catmandu::Exporter is a Perl
atmandu::Exporter.
=head2 log
Returns the current logger.
=head1 SEE ALSO
L<Catmandu::Addable>, L<Catmandu::Fix>,L<Catmandu::JSON>,
L<Catmandu::YAML>, L<Catmandu::CSV>, L<Catmandu::RIS>
=cut
1;
[ "verbose|v", "" ],
);
}
sub description {
<<EOS;
examples:
cat books.json | catmandu convert JSON to CSV --fields id,title
options:
EOS
}
sub command {
my ($self, $opts, $args)