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