Skip to content

Commit

Permalink
Allow to pass indent_length to json_pp (fix #46)
Browse files Browse the repository at this point in the history
  • Loading branch information
charsbar committed Feb 21, 2019
1 parent 6f52c65 commit 95ef594
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions bin/json_pp
Expand Up @@ -10,7 +10,7 @@ use JSON::PP ();

my %allow_json_opt = map { $_ => 1 } qw(
ascii latin1 utf8 pretty indent space_before space_after relaxed canonical allow_nonref
allow_singlequote allow_barekey allow_bignum loose escape_slash
allow_singlequote allow_barekey allow_bignum loose escape_slash indent_length
);


Expand All @@ -31,12 +31,20 @@ if ( $version ) {

$json_opt = '' if $json_opt eq '-';

my @json_opt = grep { $allow_json_opt{ $_ } or die "'$_' is not a valid json option" } split/,/, $json_opt;
my %json_opt;
for my $opt (split /,/, $json_opt) {
my ($key, $value) = split /=/, $opt, 2;
$value = 1 unless defined $value;
die "'$_' is not a valid json option" unless $allow_json_opt{$key};
$json_opt{$key} = $value;
}

my %F = (
'json' => sub {
my $json = JSON::PP->new;
$json->$_() for @json_opt;
for my $key (keys %json_opt) {
$json->$key($json_opt{$key});
}
$json->decode( $_ );
},
'eval' => sub {
Expand All @@ -51,8 +59,10 @@ my %T = (
'null' => sub { "" },
'json' => sub {
my $json = JSON::PP->new->utf8;
$json->$_() for @json_opt;
$json->canonical if grep {$_ eq 'pretty'} @json_opt;
for my $key (keys %json_opt) {
$json->$key($json_opt{$key});
}
$json->canonical if $json_opt{pretty};
$json->encode( $_ );
},
'dumper' => sub {
Expand Down

0 comments on commit 95ef594

Please sign in to comment.