ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/bin/json_xs
Revision: 1.6
Committed: Fri Aug 13 21:00:02 2010 UTC (13 years, 9 months ago) by root
Branch: MAIN
Changes since 1.5: +15 -1 lines
Log Message:
dumper, dump

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