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 |
root |
1.12 |
=item cbor - CBOR (RFC 7049, L<CBOR::XS>), a kind of binary JSON |
38 |
root |
1.1 |
|
39 |
root |
1.12 |
=item storable - a L<Storable> frozen value |
40 |
root |
1.1 |
|
41 |
root |
1.12 |
=item storable-file - a L<Storable> file (Storable has two incompatible formats) |
42 |
root |
1.9 |
|
43 |
root |
1.12 |
=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 |
root |
1.1 |
|
47 |
root |
1.9 |
=item eval - evaluate the given code as (non-utf-8) Perl, basically the reverse of "-t dump" |
48 |
|
|
|
49 |
root |
1.12 |
=item yaml - L<YAML> (avoid at all costs, requires the YAML module :) |
50 |
root |
1.1 |
|
51 |
root |
1.11 |
=item string - do not attempt to decode the file data |
52 |
root |
1.10 |
|
53 |
|
|
=item none - nothing is read, creates an C<undef> scalar - mainly useful with C<-e> |
54 |
|
|
|
55 |
root |
1.13 |
=item cbor - CBOR (via CBOR::XS) |
56 |
|
|
|
57 |
root |
1.1 |
=back |
58 |
|
|
|
59 |
|
|
=item -t toformat |
60 |
|
|
|
61 |
|
|
Write the file in the given format to STDOUT. |
62 |
|
|
|
63 |
root |
1.6 |
C<toformat> can be one of: |
64 |
root |
1.1 |
|
65 |
|
|
=over 4 |
66 |
|
|
|
67 |
|
|
=item json, json-utf-8 - json, utf-8 encoded |
68 |
|
|
|
69 |
|
|
=item json-pretty - as above, but pretty-printed |
70 |
|
|
|
71 |
|
|
=item json-utf-16le, json-utf-16be - little endian/big endian utf-16 |
72 |
|
|
|
73 |
|
|
=item json-utf-32le, json-utf-32be - little endian/big endian utf-32 |
74 |
|
|
|
75 |
root |
1.12 |
=item cbor - CBOR (RFC 7049, L<CBOR::XS>), a kind of binary JSON |
76 |
|
|
|
77 |
|
|
=item storable - a L<Storable> frozen value in network format |
78 |
root |
1.1 |
|
79 |
root |
1.12 |
=item storable-file - a L<Storable> file in network format (Storable has two incompatible formats) |
80 |
root |
1.1 |
|
81 |
root |
1.12 |
=item bencode - use L<Convert::Bencode>, if available (used by torrent files, among others) |
82 |
root |
1.9 |
|
83 |
root |
1.12 |
=item clzf - L<Compress::LZF> format |
84 |
root |
1.1 |
|
85 |
root |
1.12 |
=item yaml - L<YAML> |
86 |
root |
1.1 |
|
87 |
root |
1.12 |
=item dump - L<Data::Dump> |
88 |
root |
1.6 |
|
89 |
root |
1.12 |
=item dumper - L<Data::Dumper> |
90 |
root |
1.6 |
|
91 |
root |
1.10 |
=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 |
root |
1.8 |
Note that Data::Dumper doesn't handle self-referential data structures |
96 |
|
|
correctly - use "dump" instead. |
97 |
|
|
|
98 |
root |
1.1 |
=back |
99 |
|
|
|
100 |
root |
1.10 |
=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 |
root |
1.1 |
=back |
107 |
|
|
|
108 |
|
|
=head1 EXAMPLES |
109 |
|
|
|
110 |
root |
1.10 |
json_xs -t none <isitreally.json |
111 |
root |
1.2 |
|
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 |
root |
1.1 |
<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 |
root |
1.10 |
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 |
root |
1.5 |
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 |
root |
1.1 |
=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 |
root |
1.3 |
use Storable (); |
151 |
root |
1.1 |
use Encode; |
152 |
|
|
|
153 |
|
|
use JSON::XS; |
154 |
|
|
|
155 |
|
|
my $opt_verbose; |
156 |
|
|
my $opt_from = "json"; |
157 |
|
|
my $opt_to = "json-pretty"; |
158 |
root |
1.10 |
my $opt_eval; |
159 |
root |
1.1 |
|
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 |
root |
1.10 |
"e=s" => \$opt_eval, |
167 |
|
|
) or die "Usage: $0 [-v] -f fromformat [-e code] [-t toformat]\n"; |
168 |
root |
1.1 |
|
169 |
|
|
my %F = ( |
170 |
root |
1.10 |
"none" => sub { undef }, |
171 |
|
|
"string" => sub { $_ }, |
172 |
root |
1.1 |
"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 |
root |
1.12 |
"cbor" => sub { require CBOR::XS; CBOR::XS::decode_cbor ($_) }, |
183 |
root |
1.3 |
"storable" => sub { Storable::thaw $_ }, |
184 |
root |
1.1 |
"storable-file" => sub { open my $fh, "<", \$_; Storable::fd_retrieve $fh }, |
185 |
root |
1.9 |
"bencode" => sub { require Convert::Bencode; Convert::Bencode::bdecode ($_) }, |
186 |
root |
1.1 |
"clzf" => sub { require Compress::LZF; Compress::LZF::sthaw ($_) }, |
187 |
|
|
"yaml" => sub { require YAML; YAML::Load ($_) }, |
188 |
root |
1.13 |
"cbor" => sub { require CBOR::XS; CBOR::XS::decode_cbor ($_) }, |
189 |
root |
1.8 |
"eval" => sub { my $v = eval "no strict; no warnings; no utf8;\n#line 1 \"input\"\n$_"; die "$@" if $@; $v }, |
190 |
root |
1.1 |
); |
191 |
|
|
|
192 |
|
|
my %T = ( |
193 |
root |
1.10 |
"none" => sub { "" }, |
194 |
|
|
"string" => sub { $_ }, |
195 |
root |
1.1 |
"json" => sub { encode_json $_ }, |
196 |
|
|
"json-utf-8" => sub { encode_json $_ }, |
197 |
|
|
"json-pretty" => sub { JSON::XS->new->utf8->pretty->encode ($_) }, |
198 |
|
|
"json-utf-16le" => sub { encode "utf-16le", JSON::XS->new->encode ($_) }, |
199 |
|
|
"json-utf-16be" => sub { encode "utf-16be", JSON::XS->new->encode ($_) }, |
200 |
|
|
"json-utf-32le" => sub { encode "utf-32le", JSON::XS->new->encode ($_) }, |
201 |
|
|
"json-utf-32be" => sub { encode "utf-32be", JSON::XS->new->encode ($_) }, |
202 |
root |
1.12 |
"cbor" => sub { require CBOR::XS; CBOR::XS::encode_cbor ($_) }, |
203 |
root |
1.1 |
"storable" => sub { Storable::nfreeze $_ }, |
204 |
|
|
"storable-file" => sub { open my $fh, ">", \my $buf; Storable::nstore_fd $_, $fh; $buf }, |
205 |
root |
1.9 |
"bencode" => sub { require Convert::Bencode; Convert::Bencode::bencode ($_) }, |
206 |
root |
1.1 |
"clzf" => sub { require Compress::LZF; Compress::LZF::sfreeze_cr ($_) }, |
207 |
|
|
"yaml" => sub { require YAML; YAML::Dump ($_) }, |
208 |
root |
1.6 |
"dumper" => sub { |
209 |
|
|
require Data::Dumper; |
210 |
root |
1.8 |
#local $Data::Dumper::Purity = 1; # hopeless case |
211 |
root |
1.6 |
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 |
root |
1.7 |
"dump" => sub { |
219 |
|
|
require Data::Dump; |
220 |
|
|
local $Data::Dump::TRY_BASE64 = 0; |
221 |
|
|
Data::Dump::dump ($_) . "\n" |
222 |
|
|
}, |
223 |
root |
1.1 |
); |
224 |
|
|
|
225 |
|
|
$F{$opt_from} |
226 |
|
|
or die "$opt_from: not a valid fromformat\n"; |
227 |
|
|
|
228 |
|
|
$T{$opt_to} |
229 |
|
|
or die "$opt_from: not a valid toformat\n"; |
230 |
|
|
|
231 |
root |
1.10 |
if ($opt_from ne "none") { |
232 |
root |
1.1 |
local $/; |
233 |
|
|
binmode STDIN; # stupid perl sometimes thinks its funny |
234 |
|
|
$_ = <STDIN>; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
$_ = $F{$opt_from}->(); |
238 |
root |
1.10 |
|
239 |
|
|
eval $opt_eval; |
240 |
|
|
die $@ if $@; |
241 |
|
|
|
242 |
root |
1.1 |
$_ = $T{$opt_to}->(); |
243 |
|
|
|
244 |
|
|
binmode STDOUT; |
245 |
root |
1.10 |
syswrite STDOUT, $_; |
246 |
root |
1.1 |
|
247 |
|
|
|
248 |
|
|
|