ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/JSON-XS/bin/json_xs
Revision: 1.10
Committed: Mon Nov 7 12:28:21 2011 UTC (14 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-2_34, rel-2_33
Changes since 1.9: +36 -5 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2    
3     =head1 NAME
4    
5     json_xs - JSON::XS commandline utility
6    
7     =head1 SYNOPSIS
8    
9     json_xs [-v] [-f inputformat] [-t outputformat]
10    
11     =head1 DESCRIPTION
12    
13     F<json_xs> converts between some input and output formats (one of them is
14     JSON).
15    
16     The default input format is C<json> and the default output format is
17     C<json-pretty>.
18    
19     =head1 OPTIONS
20    
21     =over 4
22    
23     =item -v
24    
25     Be slightly more verbose.
26    
27     =item -f fromformat
28    
29     Read a file in the given format from STDIN.
30    
31     C<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 root 1.9 =item bencode - use Convert::Bencode, if available (used by torrent files, among others)
42    
43 root 1.4 =item clzf - Compress::LZF format (requires that module to be installed)
44 root 1.1
45 root 1.9 =item eval - evaluate the given code as (non-utf-8) Perl, basically the reverse of "-t dump"
46    
47 root 1.1 =item yaml - YAML (avoid at all costs, requires the YAML module :)
48    
49 root 1.10 =item string - do not attempt to decode te file data
50    
51     =item none - nothing is read, creates an C<undef> scalar - mainly useful with C<-e>
52    
53 root 1.1 =back
54    
55     =item -t toformat
56    
57     Write the file in the given format to STDOUT.
58    
59 root 1.6 C<toformat> can be one of:
60 root 1.1
61     =over 4
62    
63     =item json, json-utf-8 - json, utf-8 encoded
64    
65     =item json-pretty - as above, but pretty-printed
66    
67     =item json-utf-16le, json-utf-16be - little endian/big endian utf-16
68    
69     =item json-utf-32le, json-utf-32be - little endian/big endian utf-32
70    
71     =item storable - a Storable frozen value in network format
72    
73     =item storable-file - a Storable file in network format (Storable has two incompatible formats)
74    
75 root 1.9 =item bencode - use Convert::Bencode, if available (used by torrent files, among others)
76    
77 root 1.1 =item clzf - Compress::LZF format
78    
79     =item yaml - YAML
80    
81 root 1.6 =item dump - Data::Dump
82    
83     =item dumper - Data::Dumper
84    
85 root 1.10 =item string - writes the data out as if it were a string
86    
87     =item none - nothing gets written, mainly useful together with C<-e>
88    
89 root 1.8 Note that Data::Dumper doesn't handle self-referential data structures
90     correctly - use "dump" instead.
91    
92 root 1.1 =back
93    
94 root 1.10 =item -e code
95    
96     Evaluate perl code after reading the data and before writing it out again
97     - can be used to filter, create or extract data. The data that has been
98     written is in C<$_>, and whatever is in there is written out afterwards.
99    
100 root 1.1 =back
101    
102     =head1 EXAMPLES
103    
104 root 1.10 json_xs -t none <isitreally.json
105 root 1.2
106     "JSON Lint" - tries to parse the file F<isitreally.json> as JSON - if it
107     is valid JSON, the command outputs nothing, otherwise it will print an
108     error message and exit with non-zero exit status.
109    
110 root 1.1 <src.json json_xs >pretty.json
111    
112     Prettify the JSON file F<src.json> to F<dst.json>.
113    
114     json_xs -f storable-file <file
115    
116     Read the serialised Storable file F<file> and print a human-readable JSON
117     version of it to STDOUT.
118    
119     json_xs -f storable-file -t yaml <file
120    
121     Same as above, but write YAML instead (not using JSON at all :)
122    
123 root 1.10 json_xs -f none -e '$_ = [1, 2, 3]'
124    
125     Dump the perl array as UTF-8 encoded JSON text.
126    
127     <torrentfile json_xs -f bencode -e '$_ = join "\n", map @$_, @{$_->{"announce-list"}}' -t string
128    
129     Print the tracker list inside a torrent file.
130    
131 root 1.5 lwp-request http://cpantesters.perl.org/show/JSON-XS.json | json_xs
132    
133     Fetch the cpan-testers result summary C<JSON::XS> and pretty-print it.
134    
135 root 1.1 =head1 AUTHOR
136    
137     Copyright (C) 2008 Marc Lehmann <json@schmorp.de>
138    
139     =cut
140    
141     use strict;
142    
143     use Getopt::Long;
144 root 1.3 use Storable ();
145 root 1.1 use Encode;
146    
147     use JSON::XS;
148    
149     my $opt_verbose;
150     my $opt_from = "json";
151     my $opt_to = "json-pretty";
152 root 1.10 my $opt_eval;
153 root 1.1
154     Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order");
155    
156     GetOptions(
157     "v" => \$opt_verbose,
158     "f=s" => \$opt_from,
159     "t=s" => \$opt_to,
160 root 1.10 "e=s" => \$opt_eval,
161     ) or die "Usage: $0 [-v] -f fromformat [-e code] [-t toformat]\n";
162 root 1.1
163     my %F = (
164 root 1.10 "none" => sub { undef },
165     "string" => sub { $_ },
166 root 1.1 "json" => sub {
167     my $enc =
168     /^\x00\x00\x00/s ? "utf-32be"
169     : /^\x00.\x00/s ? "utf-16be"
170     : /^.\x00\x00\x00/s ? "utf-32le"
171     : /^.\x00.\x00/s ? "utf-16le"
172     : "utf-8";
173     warn "input text encoding is $enc\n" if $opt_verbose;
174     JSON::XS->new->decode (decode $enc, $_)
175     },
176 root 1.3 "storable" => sub { Storable::thaw $_ },
177 root 1.1 "storable-file" => sub { open my $fh, "<", \$_; Storable::fd_retrieve $fh },
178 root 1.9 "bencode" => sub { require Convert::Bencode; Convert::Bencode::bdecode ($_) },
179 root 1.1 "clzf" => sub { require Compress::LZF; Compress::LZF::sthaw ($_) },
180     "yaml" => sub { require YAML; YAML::Load ($_) },
181 root 1.8 "eval" => sub { my $v = eval "no strict; no warnings; no utf8;\n#line 1 \"input\"\n$_"; die "$@" if $@; $v },
182 root 1.1 );
183    
184     my %T = (
185 root 1.10 "none" => sub { "" },
186     "string" => sub { $_ },
187 root 1.1 "json" => sub { encode_json $_ },
188     "json-utf-8" => sub { encode_json $_ },
189     "json-pretty" => sub { JSON::XS->new->utf8->pretty->encode ($_) },
190     "json-utf-16le" => sub { encode "utf-16le", JSON::XS->new->encode ($_) },
191     "json-utf-16be" => sub { encode "utf-16be", JSON::XS->new->encode ($_) },
192     "json-utf-32le" => sub { encode "utf-32le", JSON::XS->new->encode ($_) },
193     "json-utf-32be" => sub { encode "utf-32be", JSON::XS->new->encode ($_) },
194    
195     "storable" => sub { Storable::nfreeze $_ },
196     "storable-file" => sub { open my $fh, ">", \my $buf; Storable::nstore_fd $_, $fh; $buf },
197    
198 root 1.9 "bencode" => sub { require Convert::Bencode; Convert::Bencode::bencode ($_) },
199 root 1.1 "clzf" => sub { require Compress::LZF; Compress::LZF::sfreeze_cr ($_) },
200     "yaml" => sub { require YAML; YAML::Dump ($_) },
201 root 1.6 "dumper" => sub {
202     require Data::Dumper;
203 root 1.8 #local $Data::Dumper::Purity = 1; # hopeless case
204 root 1.6 local $Data::Dumper::Terse = 1;
205     local $Data::Dumper::Indent = 1;
206     local $Data::Dumper::Useqq = 1;
207     local $Data::Dumper::Quotekeys = 0;
208     local $Data::Dumper::Sortkeys = 1;
209     Data::Dumper::Dumper($_)
210     },
211 root 1.7 "dump" => sub {
212     require Data::Dump;
213     local $Data::Dump::TRY_BASE64 = 0;
214     Data::Dump::dump ($_) . "\n"
215     },
216 root 1.1 );
217    
218     $F{$opt_from}
219     or die "$opt_from: not a valid fromformat\n";
220    
221     $T{$opt_to}
222     or die "$opt_from: not a valid toformat\n";
223    
224 root 1.10 if ($opt_from ne "none") {
225 root 1.1 local $/;
226     binmode STDIN; # stupid perl sometimes thinks its funny
227     $_ = <STDIN>;
228     }
229    
230     $_ = $F{$opt_from}->();
231 root 1.10
232     eval $opt_eval;
233     die $@ if $@;
234    
235 root 1.1 $_ = $T{$opt_to}->();
236    
237     binmode STDOUT;
238 root 1.10 syswrite STDOUT, $_;
239 root 1.1
240    
241