l::JSON_TYPE;
use strict;
use warnings;
use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
use MOP4Import::Util qw/globref define_constant terse_dump/;
use MOP4Import::Opts qw/Opts/;
use Cpanel::JSON::
XS::Type;
our %JSON_TYPES; # It is too early to hide this.
BEGIN {
my $CLS = 'Cpanel::JSON::XS::Type';
foreach my $origTypeName (qw(int float string bool null)) {
foreach my $suffix ('', '_
'';
my $typeName = $origTypeName . $suffix;
my $lowerName = "JSON_TYPE_".$typeName;
my $upperName = "JSON_TYPE_".uc($typeName);
my $value = $CLS->$upperName;
define_con
#!/usr/bin/env perl
package MOP4Import::Base::CLI_JSON;
use strict;
use warnings;
use Carp ();
use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
BEGIN {
print STDERR "Using (file '" . __FILE__ . "')\n"
is help message", json_type => 'string']
, ['quiet' => doc => 'to be (somewhat) quiet', json_type => 'int']
, ['scalar' => doc => "evaluate methods in scalar context", json_type => 'bool']
['output' => default => 'ndjson'
, doc => "choose output serializer (ndjson/json/tsv/dump)"
, json_type => 'string'
]
, ['flatten'
, json_type => 'bool'
]
t::Base::JSON;
use strict;
use warnings;
use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
use MOP4Import::Base::Configure -as_base;
use JSON::MaybeXS;
use constant USING_CPANEL_JSON_XS => JSON()->isa("
ly works with Cpanel::JSON::XS. JSON::XS prohibits use of restricted hash.
sub TO_JSON {
my ($self) = @_;
+{map(($_ => $self->{$_}), grep {!/^_/} keys %$self)}
}
sub cli_json_type {
(my MY $sel
f->cli_json_type_of($self);
}
sub cli_json_type_of {
(my MY $self, my $objOrTypeName) = @_;
$self->JSON_TYPE_HANDLER->lookup_json_type(ref $objOrTypeName || $objOrTypeName);
}
sub cli_json_encod
end of MOP4Import handling.
, 'delayed_tasks'
# default value for json_type. 'string' if not specified
, 'default_json_type'
# Cache to store and keep heavy computation results between p
in C<undef>, C<[..]>, C<{..}>...
we must use some kind of serializer such as L<Data::Dumper> or L<JSON>.
Following is a minimum starting point of such subcommand dispatcher:
use Data::Dumper;
is falsy.
=item *
Change output serializer to L<JSON>.
=item *
Change argument parser to convert C<[..]>, C<{...}> automatically
by L<JSON/decode_json> too.
This enables you to compose your favorite
h takes/returns structured objects/arrays.
=back
This is a backstory of L<MOP4Import::Base::CLI_JSON>. Thank you for reading!
=head1 APPENDIX
=head2 Sample implementation of parse_opts()
sub
);
my $fields_array = fields_array($opts->{objpkg});
my $isFirst = not $myPack->JSON_TYPE_HANDLER->lookup_json_type($opts->{objpkg});
# Import all fields from super class
foreach my $super_c
gin->clone : $origin;
};
push @$fields_array, $name;
}
$myPack->JSON_TYPE_HANDLER->inherit_json_type($opts->{objpkg}, $super_class) if $isFirst;
}
{
my %dup;
foreach
c;
}
};
print STDERR " FieldSpec is: $field_class\n" if DEBUG;
$myPack->JSON_TYPE_HANDLER->declare_json_type_record($opts->{objpkg});
$myPack->declare___field($opts, $field_class, ref
fields
('name'
, 'doc' # documentation
, 'default'
, 'no_getter'
, 'package'
, 'json_type'
, 'isa'
, 'validator'
, 'zsh_completer'
, 'extra'
# file? line? package?
);
esult : $result;
}
#
# posix_style long option with JSON support.
#
sub parse_json_opts {
my ($pack, $list, $result, $alias) = @_;
require JSON::MaybeXS;
parse_opts($pack, $list, $result, $alia
d.
my $copy = $_[0];
Encode::_utf8_off($copy) if Encode::is_utf8($copy);
JSON::MaybeXS::JSON()->new->utf8->relaxed->decode($copy);
} elsif (not Encode::is_utf8($_[0]) and $_[0] =
ates set hook (onconfigure_FIELDNAME) wrapped with
L<Scalar::Util/weaken>.
=head2 json_type => STRING | Cpanel::JSON::XS::Type
To be documented...
=head1 SEE ALSO
L<MOP4Import::Declare>
=head1 AU
ase::CLI_JSON - Base class for rapidly building testable CLI modules with JSON I/O
=head1 SYNOPSIS
=for code perl
#!/usr/bin/env perl
package MyScript;
use MOP4Import::Base::CLI_JSON -as_bas
[fields =>
[verbose => doc => "Enable verbose output", json_type => 'bool'],
[config => doc => "Config file path", json_type => 'string']
];
MY->cli_run(\@ARGV) unless calle
K\n";
}
# Regular method - output handled by CLI_JSON
sub query {
my ($self, $sql) = @_;
# Returns data structure that will be JSON-encoded
return {result => "data from $sql"};
}
t STDERR "Using (file '" . __FILE__ . "')\n" if DEBUG and DEBUG >= 2
}
use MOP4Import::Base::CLI_JSON -as_base
, [fields =>
[lib =>
doc => "library directory list. SCALAR, ARRAY or ':' s
ackage MOP4Import::Util::CommentedJson;
use strict;
use warnings FATAL => 'all';
use Exporter qw/import/;
use JSON::MaybeXS;
use bytes;
our @EXPORT_OK = qw/strip_json_comments/;
our $re_atom = qr{tr
tring
}xs;
sub strip_json_comments {
my ($pack, $bytes) = @_;
$bytes =~ s/\A\xef\xbb\xbf//; # Strip BOM of utf-8.
my @region = reverse list_json_comments($pack, $bytes);
foreach
($startPos, $len) = @$region;
substr($bytes, $startPos, $len, '');
}
$bytes;
}
sub list_json_comments {
my @pos;
while ($_[1] =~ m{\G(?<ws>\s*+)$re_ALL}g) {
if (defined $+{comment} a
i_run(\@ARGV) unless caller;
1;
=head1 DESCRIPTION
Now superceded by L<MOP4Import::Base::CLI_JSON>.
MOP4Import::Base::CLI is a general purpose base class to make
L<your module also runnable from