ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/bin/json_xs
Revision: 1.20
Committed: Thu Nov 15 20:49:12 2018 UTC (5 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-4_0, rel-4_01, rel-4_03, rel-4_02, rel-4_0_00, HEAD
Changes since 1.19: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

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