ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/JSON-XS/bin/json_xs
Revision: 1.9
Committed: Thu Sep 22 19:37:24 2011 UTC (15 years ago) by root
Branch: MAIN
Changes since 1.8: +8 -2 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     =back
50    
51     =item -t toformat
52    
53     Write the file in the given format to STDOUT.
54    
55 root 1.6 C<toformat> can be one of:
56 root 1.1
57     =over 4
58    
59     =item json, json-utf-8 - json, utf-8 encoded
60    
61     =item json-pretty - as above, but pretty-printed
62    
63     =item json-utf-16le, json-utf-16be - little endian/big endian utf-16
64    
65     =item json-utf-32le, json-utf-32be - little endian/big endian utf-32
66    
67     =item storable - a Storable frozen value in network format
68    
69     =item storable-file - a Storable file in network format (Storable has two incompatible formats)
70    
71 root 1.9 =item bencode - use Convert::Bencode, if available (used by torrent files, among others)
72    
73 root 1.1 =item clzf - Compress::LZF format
74    
75     =item yaml - YAML
76    
77 root 1.6 =item dump - Data::Dump
78    
79     =item dumper - Data::Dumper
80    
81 root 1.8 Note that Data::Dumper doesn't handle self-referential data structures
82     correctly - use "dump" instead.
83    
84 root 1.1 =back
85    
86     =back
87    
88     =head1 EXAMPLES
89    
90 root 1.2 json_xs -t null <isitreally.json
91    
92     "JSON Lint" - tries to parse the file F<isitreally.json> as JSON - if it
93     is valid JSON, the command outputs nothing, otherwise it will print an
94     error message and exit with non-zero exit status.
95    
96 root 1.1 <src.json json_xs >pretty.json
97    
98     Prettify the JSON file F<src.json> to F<dst.json>.
99    
100     json_xs -f storable-file <file
101    
102     Read the serialised Storable file F<file> and print a human-readable JSON
103     version of it to STDOUT.
104    
105     json_xs -f storable-file -t yaml <file
106    
107     Same as above, but write YAML instead (not using JSON at all :)
108    
109 root 1.5 lwp-request http://cpantesters.perl.org/show/JSON-XS.json | json_xs
110    
111     Fetch the cpan-testers result summary C<JSON::XS> and pretty-print it.
112    
113 root 1.1 =head1 AUTHOR
114    
115     Copyright (C) 2008 Marc Lehmann <json@schmorp.de>
116    
117     =cut
118    
119     use strict;
120    
121     use Getopt::Long;
122 root 1.3 use Storable ();
123 root 1.1 use Encode;
124    
125     use JSON::XS;
126    
127     my $opt_verbose;
128     my $opt_from = "json";
129     my $opt_to = "json-pretty";
130    
131     Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order");
132    
133     GetOptions(
134     "v" => \$opt_verbose,
135     "f=s" => \$opt_from,
136     "t=s" => \$opt_to,
137     ) or die "Usage: $0 [-v] -f fromformat [-t toformat]\n";
138    
139     my %F = (
140     "json" => sub {
141     my $enc =
142     /^\x00\x00\x00/s ? "utf-32be"
143     : /^\x00.\x00/s ? "utf-16be"
144     : /^.\x00\x00\x00/s ? "utf-32le"
145     : /^.\x00.\x00/s ? "utf-16le"
146     : "utf-8";
147     warn "input text encoding is $enc\n" if $opt_verbose;
148     JSON::XS->new->decode (decode $enc, $_)
149     },
150 root 1.3 "storable" => sub { Storable::thaw $_ },
151 root 1.1 "storable-file" => sub { open my $fh, "<", \$_; Storable::fd_retrieve $fh },
152 root 1.9 "bencode" => sub { require Convert::Bencode; Convert::Bencode::bdecode ($_) },
153 root 1.1 "clzf" => sub { require Compress::LZF; Compress::LZF::sthaw ($_) },
154     "yaml" => sub { require YAML; YAML::Load ($_) },
155 root 1.8 "eval" => sub { my $v = eval "no strict; no warnings; no utf8;\n#line 1 \"input\"\n$_"; die "$@" if $@; $v },
156 root 1.1 );
157    
158     my %T = (
159     "null" => sub { "" },
160     "json" => sub { encode_json $_ },
161     "json-utf-8" => sub { encode_json $_ },
162     "json-pretty" => sub { JSON::XS->new->utf8->pretty->encode ($_) },
163     "json-utf-16le" => sub { encode "utf-16le", JSON::XS->new->encode ($_) },
164     "json-utf-16be" => sub { encode "utf-16be", JSON::XS->new->encode ($_) },
165     "json-utf-32le" => sub { encode "utf-32le", JSON::XS->new->encode ($_) },
166     "json-utf-32be" => sub { encode "utf-32be", JSON::XS->new->encode ($_) },
167    
168     "storable" => sub { Storable::nfreeze $_ },
169     "storable-file" => sub { open my $fh, ">", \my $buf; Storable::nstore_fd $_, $fh; $buf },
170    
171 root 1.9 "bencode" => sub { require Convert::Bencode; Convert::Bencode::bencode ($_) },
172 root 1.1 "clzf" => sub { require Compress::LZF; Compress::LZF::sfreeze_cr ($_) },
173     "yaml" => sub { require YAML; YAML::Dump ($_) },
174 root 1.6 "dumper" => sub {
175     require Data::Dumper;
176 root 1.8 #local $Data::Dumper::Purity = 1; # hopeless case
177 root 1.6 local $Data::Dumper::Terse = 1;
178     local $Data::Dumper::Indent = 1;
179     local $Data::Dumper::Useqq = 1;
180     local $Data::Dumper::Quotekeys = 0;
181     local $Data::Dumper::Sortkeys = 1;
182     Data::Dumper::Dumper($_)
183     },
184 root 1.7 "dump" => sub {
185     require Data::Dump;
186     local $Data::Dump::TRY_BASE64 = 0;
187     Data::Dump::dump ($_) . "\n"
188     },
189 root 1.1 );
190    
191     $F{$opt_from}
192     or die "$opt_from: not a valid fromformat\n";
193    
194     $T{$opt_to}
195     or die "$opt_from: not a valid toformat\n";
196    
197     {
198     local $/;
199     binmode STDIN; # stupid perl sometimes thinks its funny
200     $_ = <STDIN>;
201     }
202    
203     $_ = $F{$opt_from}->();
204     $_ = $T{$opt_to}->();
205    
206     binmode STDOUT;
207     print $_;
208    
209    
210