ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/JSON-XS/bin/json_xs
Revision: 1.8
Committed: Tue Aug 17 23:27:36 2010 UTC (16 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-2_3, rel-2_32, rel-2_31
Changes since 1.7: +7 -0 lines
Log Message:
2.3

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