1#!/opt/bin/perl 2 3=head1 NAME 4 5json_xs - JSON::XS commandline utility 6 7=head1 SYNOPSIS 8 9 json_xs [-v] [-f inputformat] [-t outputformat] 10 11=head1 DESCRIPTION 12 13F<json_xs> converts between some input and output formats (one of them is 14JSON). 15 16The default input format is C<json> and the default output format is 17C<json-pretty>. 18 19=head1 OPTIONS 20 21=over 4 22 23=item -v 24 25Be slightly more verbose. 26 27=item -f fromformat 28 29Read a file in the given format from STDIN. 30 31C<fromformat> can be one of: 32 33=over 4 34 35=item json - a json text encoded, either utf-8, utf16-be/le, utf32-be/le 36 37=item storable - a Storable frozen value 38 39=item storable-file - a Storable file (Storable has two incompatible formats) 40 41=item clzf - Compress::LZF format (requires that module to be installed) 42 43=item yaml - YAML (avoid at all costs, requires the YAML module :) 44 45=back 46 47=item -t toformat 48 49Write the file in the given format to STDOUT. 50 51C<fromformat> can be one of: 52 53=over 4 54 55=item json, json-utf-8 - json, utf-8 encoded 56 57=item json-pretty - as above, but pretty-printed 58 59=item json-utf-16le, json-utf-16be - little endian/big endian utf-16 60 61=item json-utf-32le, json-utf-32be - little endian/big endian utf-32 62 63=item storable - a Storable frozen value in network format 64 65=item storable-file - a Storable file in network format (Storable has two incompatible formats) 66 67=item clzf - Compress::LZF format 68 69=item yaml - YAML 70 71=back 72 73=back 74 75=head1 EXAMPLES 76 77 json_xs -t null <isitreally.json 78 79"JSON Lint" - tries to parse the file F<isitreally.json> as JSON - if it 80is valid JSON, the command outputs nothing, otherwise it will print an 81error message and exit with non-zero exit status. 82 83 <src.json json_xs >pretty.json 84 85Prettify the JSON file F<src.json> to F<dst.json>. 86 87 json_xs -f storable-file <file 88 89Read the serialised Storable file F<file> and print a human-readable JSON 90version of it to STDOUT. 91 92 json_xs -f storable-file -t yaml <file 93 94Same as above, but write YAML instead (not using JSON at all :) 95 96 lwp-request http://cpantesters.perl.org/show/JSON-XS.json | json_xs 97 98Fetch the cpan-testers result summary C<JSON::XS> and pretty-print it. 99 100=head1 AUTHOR 101 102Copyright (C) 2008 Marc Lehmann <json@schmorp.de> 103 104=cut 105 106use strict; 107 108use Getopt::Long; 109use Storable (); 110use Encode; 111 112use JSON::XS; 113 114my $opt_verbose; 115my $opt_from = "json"; 116my $opt_to = "json-pretty"; 117 118Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order"); 119 120GetOptions( 121 "v" => \$opt_verbose, 122 "f=s" => \$opt_from, 123 "t=s" => \$opt_to, 124) or die "Usage: $0 [-v] -f fromformat [-t toformat]\n"; 125 126my %F = ( 127 "json" => sub { 128 my $enc = 129 /^\x00\x00\x00/s ? "utf-32be" 130 : /^\x00.\x00/s ? "utf-16be" 131 : /^.\x00\x00\x00/s ? "utf-32le" 132 : /^.\x00.\x00/s ? "utf-16le" 133 : "utf-8"; 134 warn "input text encoding is $enc\n" if $opt_verbose; 135 JSON::XS->new->decode (decode $enc, $_) 136 }, 137 "storable" => sub { Storable::thaw $_ }, 138 "storable-file" => sub { open my $fh, "<", \$_; Storable::fd_retrieve $fh }, 139 "clzf" => sub { require Compress::LZF; Compress::LZF::sthaw ($_) }, 140 "yaml" => sub { require YAML; YAML::Load ($_) }, 141); 142 143my %T = ( 144 "null" => sub { "" }, 145 "json" => sub { encode_json $_ }, 146 "json-utf-8" => sub { encode_json $_ }, 147 "json-pretty" => sub { JSON::XS->new->utf8->pretty->encode ($_) }, 148 "json-utf-16le" => sub { encode "utf-16le", JSON::XS->new->encode ($_) }, 149 "json-utf-16be" => sub { encode "utf-16be", JSON::XS->new->encode ($_) }, 150 "json-utf-32le" => sub { encode "utf-32le", JSON::XS->new->encode ($_) }, 151 "json-utf-32be" => sub { encode "utf-32be", JSON::XS->new->encode ($_) }, 152 153 "storable" => sub { Storable::nfreeze $_ }, 154 "storable-file" => sub { open my $fh, ">", \my $buf; Storable::nstore_fd $_, $fh; $buf }, 155 156 "clzf" => sub { require Compress::LZF; Compress::LZF::sfreeze_cr ($_) }, 157 "yaml" => sub { require YAML; YAML::Dump ($_) }, 158); 159 160$F{$opt_from} 161 or die "$opt_from: not a valid fromformat\n"; 162 163$T{$opt_to} 164 or die "$opt_from: not a valid toformat\n"; 165 166{ 167 local $/; 168 binmode STDIN; # stupid perl sometimes thinks its funny 169 $_ = <STDIN>; 170} 171 172$_ = $F{$opt_from}->(); 173$_ = $T{$opt_to}->(); 174 175binmode STDOUT; 176print $_; 177 178 179 180