package Catmandu::Exporter::JSON;
use Catmandu::Sane;
our $VERSION = '1.2025';
use Cpanel::JSON::XS ();
use Moo;
use namespace::clean;
with 'Catmandu::Exporter';
has line_delimited => (is => 'ro'
anonical => (is => 'ro', default => sub {0});
has json => (is => 'lazy');
sub _build_json {
my ($self) = @_;
Cpanel::JSON::XS->new->utf8(0)
->allow_nonref->pretty($self
my $json = $self->json->encode($data);
if ($self->line_delimited) {
print $fh $json;
print $fh "\n";
return;
}
if ($self->pretty) {
chomp $json;
}
package Catmandu::Fix::to_json;
use Catmandu::Sane;
our $VERSION = '1.2025';
use Cpanel::JSON::XS ();
use Catmandu::Util::Path qw(as_path);
use Moo;
use namespace::clean;
use Catmandu::Fix::Has
$json = Cpanel::JSON::XS->new->utf8(0)->pretty(0)->allow_nonref(1);
as_path($self->path)->updater(
if => [
[qw(maybe_value array_ref hash_ref)] => sub {
$json-
}
]
);
}
1;
__END__
=pod
=head1 NAME
Catmandu::Fix::to_json - convert the value of a field to json
=head1 SYNOPSIS
to_json(my.field)
=head1 SEE ALSO
L<Catmandu::Fix>
=cut
package Catmandu::Importer::JSON;
use Catmandu::Sane;
our $VERSION = '1.2025';
use Cpanel::JSON::XS ();
use Moo;
use namespace::clean;
with 'Catmandu::Importer';
has line_delimited => (is => 'r
uffer_size => (is => 'ro', default => sub {256});
has json => (is => 'lazy');
sub _build_json {
my ($self) = @_;
Cpanel::JSON::XS->new->utf8($self->encoding eq ':raw');
}
sub _bu
return sub {
state $json = $self->json;
state $fh = $self->fh;
if (defined(my $line = <$fh>)) {
return $json->decode($line);
}
tmandu::Serializer::json;
use Catmandu::Sane;
our $VERSION = '1.2025';
use Cpanel::JSON::XS ();
use Moo;
use namespace::clean;
sub serialize {
Cpanel::JSON::XS::encode_json($_[1]);
}
sub dese
rialize {
Cpanel::JSON::XS::decode_json($_[1]);
}
1;
__END__
=pod
=head1 NAME
Catmandu::Serializer - A (de)serializer from and to json
=head1 SYNOPSIS
package MyPackage;
use Moo;
my $obj = MyPackage->new;
my $obj = MyPackage->new(serializer => 'json');
$obj->serialize( { foo => 'bar' } ); # JSON
$obj->deserialize( "{'foo':'bar'}" ); # Perl
=head1 SEE ALSO
L
package Catmandu::Fix::from_json;
use Catmandu::Sane;
our $VERSION = '1.2025';
use Cpanel::JSON::XS ();
use Moo;
use Catmandu::Util::Path qw(as_path);
use namespace::clean;
use Catmandu::Fix::Has;
_fixer {
my ($self) = @_;
my $json = Cpanel::JSON::XS->new->utf8(0)->pretty(0)->allow_nonref(1);
as_path($self->path)->updater(if_string => sub {$json->decode($_[0])});
}
1;
__END__
=po
d
=head1 NAME
Catmandu::Fix::from_json - replace a json field with the parsed value
=head1 SYNOPSIS
from_json(my.field)
=head1 SEE ALSO
L<Catmandu::Fix>
=cut
ld_serializer',
handles => [qw(serialize deserialize)]
);
sub default_serialization_format {'json'}
sub _build_serializer {
my ($self) = @_;
my $pkg = require_package($self->serializatio
Deserialize bytes into a perl data structure.
=head1 SEE ALSO
L<Catmandu::Store::DBI>,
L<Catmandu::Serializer::json>,
L<Catmandu::Serializer::storabe>,
L<Catmandu::Serializer::messagepack>
=cut
my $json = qq([{"name":"Nicolas"}]);
{
my $record = Catmandu->import_from_string( $json, "JSON" );
}
# is the same as
{
my $record = Catmandu->importer('JSON', file
IS
# From the command line
# JSON is an importer and YAML an exporter
$ catmandu convert JSON to YAML < data.json
# OAI is an importer and JSON an exporter
$ catmandu convert OA
/oai to JSON
# From Perl
use Catmandu;
my $importer = Catmandu->importer('JSON', file => 'data.json');
my $exporter = Catmandu->exporter('YAML');
$exporter->add({ record => "one
n up memory
=head1 DESCRIPTION
A Catmandu::Exporter is a Perl package that can export data into JSON, YAML, XML
or many other formats. By default, data is to STDOUT. Optionally provide a C<file>
or
om the JSON
# input. By default the TSV exporter will export the fields that are
# found in the first JSON record.
$ catmandu convert JSON to TSV --fields "id,title,year" < data.json
::JSON::XS ();
use Hash::Merge::Simple ();
use MIME::Types;
use POSIX ();
use Time::HiRes ();
our %EXPORT_TAGS = (
io => [
qw(io read_file read_io write_file read_yaml read_json
# dies on error
YAML::XS::LoadFile($_[0]);
}
sub read_json {
my $text = read_file($_[0]);
# dies on error
Cpanel::JSON::XS->new->decode($text);
}
##
# Split a path on . or /, but
Serialiser::Boolean')
|| $obj->isa('JSON::XS::Boolean')
|| $obj->isa('Cpanel::JSON::XS::Boolean')
|| $obj->isa('JSON::PP::Boolean');
} // 0;
}
sub is_integer {
du::Sane;
our $VERSION = '1.2025';
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
if grep {-r File::Spec->catfile($path, $_)}
grep /^catmandu.+(?:yaml|yml|json|pl)$/, readdir $dh;
}
Catmandu->default_load_path;
}
has load_paths => (
is =>
as default_importer_package => (is => 'ro', default => sub {'JSON'});
has default_exporter_package => (is => 'ro', default => sub {'JSON'});
has default_validator_package => (is => 'ro');
has store
and line
# Write all field values as a line of Text
$ catmandu convert JSON to Text --field_sep "," < data.json
# In a Perl script
use Catmandu;
# Print to STDOUT
my $expor
Null --fix 'add_field(hello,world)'
[{"hello":"world"}]
The example above generates the JSON output C<[{"hello":"world"}]>
on the standard output. We asked the Catmandu processor to convert
d add one property C<hello> with value
C<world>.
We can ask Catmandu not to generate the default JSON output but
convert to a YAML output:
$ catmandu convert Null --fix 'add_field(hello,world)'
mmand line:
$ cat file.yaml
---
hello: world
...
$ catmandu convert YAML to JSON < file.yaml
[{"hello":"world"}]
The left part of the C<to> keyword is called the C<Importe
IS
# From the command line
# JSON is an importer and YAML an exporter
$ catmandu convert JSON to YAML < data.json
# OAI is an importer and JSON an exporter
$ catmandu convert OA
u convert JSON --file http://example.com/data.json to YAML
# From Perl
use Catmandu;
use Data::Dumper;
my $importer = Catmandu->importer('JSON', file => 'data.json');
$
->first;
# Convert OAI to JSON in Perl
my $importer = Catmandu->importer('OAI', url => 'http://biblio.ugent.be/oai');
my $exporter = Catmandu->exporter('JSON');
$exporter->add_many($
be activated using the
-D option on all Catmandu commands:
$ catmandu -D convert JSON to YAML < data.json
$ catmandu -D export MongoDB --database-name items --bag
The log4perl configurati
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
clone($_[0]);
}
package main;
my $data = Catmandu->importer('JSON',
file => File::Spec->catfile($FindBin::Bin, 'data.json'))->first;
my $nothing_fixer = Catmandu::Fix->new(fixes => [(Nothin
=head1 SYNOPSIS
# From the command line
$ catmandu import JSON into MongoDB --database_name 'bibliography' < data.json
$ catmandu export MongoDB --database_name 'bibliography' to YAML
vert JSON to CSV < data.json
$ catmandu convert CSV to YAML < data.csv
$ catmandu convert MARC to YAML < data.mrc
# Fix data, add, delete, change fields
$ catmandu convert JSON --fi
d(title,my_title)' < data.json
$ catmandu convert JSON --fix all_my_fixes.txt < data.json
# Use a moustache preprocessor on the fix script
$ catmandu convert JSON --fix all_my_fixes.txt -
-var opt1=foo --var opt2=bar < data.json
# run a fix script
$ catmandu run myfixes.fix
# or, create an executable fix script
$ cat myfixes.fix
#!/usr/local/bin/catmandu run
d
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
L exporter
=head1 SYNOPSIS
# From the commandline
$ catmandu convert JSON --fix myfixes to YAML < /tmp/data.json
# From Perl
use Catmandu;
# Print to STDOUT
my $exporter =