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