1#!/usr/bin/perl 2 3BEGIN { pop @INC if $INC[-1] eq '.' } 4use strict; 5use Getopt::Long; 6 7use JSON::PP (); 8 9# imported from JSON-XS/bin/json_xs 10 11my %allow_json_opt = map { $_ => 1 } qw( 12 ascii latin1 utf8 pretty indent space_before space_after relaxed canonical allow_nonref 13 allow_singlequote allow_barekey allow_bignum loose escape_slash indent_length 14); 15 16 17GetOptions( 18 'v' => \( my $opt_verbose ), 19 'f=s' => \( my $opt_from = 'json' ), 20 't=s' => \( my $opt_to = 'json' ), 21 'json_opt=s' => \( my $json_opt = 'pretty' ), 22 'V' => \( my $version ), 23) or die "Usage: $0 [-V] [-f from_format] [-t to_format] [-json_opt options_to_json1[,options_to_json2[,...]]]\n"; 24 25 26if ( $version ) { 27 print "$JSON::PP::VERSION\n"; 28 exit; 29} 30 31 32$json_opt = '' if $json_opt eq '-'; 33 34my %json_opt; 35for my $opt (split /,/, $json_opt) { 36 my ($key, $value) = split /=/, $opt, 2; 37 $value = 1 unless defined $value; 38 die "'$_' is not a valid json option" unless $allow_json_opt{$key}; 39 $json_opt{$key} = $value; 40} 41 42my %F = ( 43 'json' => sub { 44 my $json = JSON::PP->new; 45 for my $key (keys %json_opt) { 46 $json->$key($json_opt{$key}); 47 } 48 $json->decode( $_ ); 49 }, 50 'eval' => sub { 51 my $v = eval "no strict;\n#line 1 \"input\"\n$_"; 52 die "$@" if $@; 53 return $v; 54 }, 55); 56 57 58my %T = ( 59 'null' => sub { "" }, 60 'json' => sub { 61 my $json = JSON::PP->new->utf8; 62 for my $key (keys %json_opt) { 63 $json->$key($json_opt{$key}); 64 } 65 $json->canonical if $json_opt{pretty}; 66 $json->encode( $_ ); 67 }, 68 'dumper' => sub { 69 require Data::Dumper; 70 local $Data::Dumper::Terse = 1; 71 local $Data::Dumper::Indent = 1; 72 local $Data::Dumper::Useqq = 1; 73 local $Data::Dumper::Quotekeys = 0; 74 local $Data::Dumper::Sortkeys = 1; 75 Data::Dumper::Dumper($_) 76 }, 77); 78 79 80 81$F{$opt_from} 82 or die "$opt_from: not a valid fromformat\n"; 83 84$T{$opt_to} 85 or die "$opt_from: not a valid toformat\n"; 86 87local $/; 88$_ = <STDIN>; 89 90$_ = $F{$opt_from}->(); 91$_ = $T{$opt_to}->(); 92 93print $_; 94 95 96__END__ 97 98=pod 99 100=encoding utf8 101 102=head1 NAME 103 104json_pp - JSON::PP command utility 105 106=head1 SYNOPSIS 107 108 json_pp [-v] [-f from_format] [-t to_format] [-json_opt options_to_json1[,options_to_json2[,...]]] 109 110=head1 DESCRIPTION 111 112json_pp converts between some input and output formats (one of them is JSON). 113This program was copied from L<json_xs> and modified. 114 115The default input format is json and the default output format is json with pretty option. 116 117=head1 OPTIONS 118 119=head2 -f 120 121 -f from_format 122 123Reads a data in the given format from STDIN. 124 125Format types: 126 127=over 128 129=item json 130 131as JSON 132 133=item eval 134 135as Perl code 136 137=back 138 139=head2 -t 140 141Writes a data in the given format to STDOUT. 142 143=over 144 145=item null 146 147no action. 148 149=item json 150 151as JSON 152 153=item dumper 154 155as Data::Dumper 156 157=back 158 159=head2 -json_opt 160 161options to JSON::PP 162 163Acceptable options are: 164 165 ascii latin1 utf8 pretty indent space_before space_after relaxed canonical allow_nonref 166 allow_singlequote allow_barekey allow_bignum loose escape_slash 167 168Multiple options must be separated by commas: 169 170 Right: -json_opt pretty,canonical 171 172 Wrong: -json_opt pretty -json_opt canonical 173 174=head2 -v 175 176Verbose option, but currently no action in fact. 177 178=head2 -V 179 180Prints version and exits. 181 182 183=head1 EXAMPLES 184 185 $ perl -e'print q|{"foo":"あい","bar":1234567890000000000000000}|' |\ 186 json_pp -f json -t dumper -json_opt pretty,utf8,allow_bignum 187 188 $VAR1 = { 189 'bar' => bless( { 190 'value' => [ 191 '0000000', 192 '0000000', 193 '5678900', 194 '1234' 195 ], 196 'sign' => '+' 197 }, 'Math::BigInt' ), 198 'foo' => "\x{3042}\x{3044}" 199 }; 200 201 $ perl -e'print q|{"foo":"あい","bar":1234567890000000000000000}|' |\ 202 json_pp -f json -t dumper -json_opt pretty 203 204 $VAR1 = { 205 'bar' => '1234567890000000000000000', 206 'foo' => "\x{e3}\x{81}\x{82}\x{e3}\x{81}\x{84}" 207 }; 208 209=head1 SEE ALSO 210 211L<JSON::PP>, L<json_xs> 212 213=head1 AUTHOR 214 215Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt> 216 217 218=head1 COPYRIGHT AND LICENSE 219 220Copyright 2010 by Makamaka Hannyaharamitu 221 222This library is free software; you can redistribute it and/or modify 223it under the same terms as Perl itself. 224 225=cut 226 227