ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/bin/json_xs
Revision: 1.3
Committed: Sun Mar 30 09:27:16 2008 UTC (16 years, 2 months ago) by root
Branch: MAIN
CVS Tags: rel-2_2, rel-2_21, rel-2_22
Changes since 1.2: +2 -2 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 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 thta 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<fromformat> 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 =back
72
73 =back
74
75 =head1 EXAMPLES
76
77 json_xs -t null <isitreally.json
78
79 "JSON Lint" - tries to parse the file F<isitreally.json> as JSON - if it
80 is valid JSON, the command outputs nothing, otherwise it will print an
81 error message and exit with non-zero exit status.
82
83 <src.json json_xs >pretty.json
84
85 Prettify the JSON file F<src.json> to F<dst.json>.
86
87 json_xs -f storable-file <file
88
89 Read the serialised Storable file F<file> and print a human-readable JSON
90 version of it to STDOUT.
91
92 json_xs -f storable-file -t yaml <file
93
94 Same as above, but write YAML instead (not using JSON at all :)
95
96 =head1 AUTHOR
97
98 Copyright (C) 2008 Marc Lehmann <json@schmorp.de>
99
100 =cut
101
102 use strict;
103
104 use Getopt::Long;
105 use Storable ();
106 use Encode;
107
108 use JSON::XS;
109
110 my $opt_verbose;
111 my $opt_from = "json";
112 my $opt_to = "json-pretty";
113
114 Getopt::Long::Configure ("bundling", "no_ignore_case", "require_order");
115
116 GetOptions(
117 "v" => \$opt_verbose,
118 "f=s" => \$opt_from,
119 "t=s" => \$opt_to,
120 ) or die "Usage: $0 [-v] -f fromformat [-t toformat]\n";
121
122 my %F = (
123 "json" => sub {
124 my $enc =
125 /^\x00\x00\x00/s ? "utf-32be"
126 : /^\x00.\x00/s ? "utf-16be"
127 : /^.\x00\x00\x00/s ? "utf-32le"
128 : /^.\x00.\x00/s ? "utf-16le"
129 : "utf-8";
130 warn "input text encoding is $enc\n" if $opt_verbose;
131 JSON::XS->new->decode (decode $enc, $_)
132 },
133 "storable" => sub { Storable::thaw $_ },
134 "storable-file" => sub { open my $fh, "<", \$_; Storable::fd_retrieve $fh },
135 "clzf" => sub { require Compress::LZF; Compress::LZF::sthaw ($_) },
136 "yaml" => sub { require YAML; YAML::Load ($_) },
137 );
138
139 my %T = (
140 "null" => sub { "" },
141 "json" => sub { encode_json $_ },
142 "json-utf-8" => sub { encode_json $_ },
143 "json-pretty" => sub { JSON::XS->new->utf8->pretty->encode ($_) },
144 "json-utf-16le" => sub { encode "utf-16le", JSON::XS->new->encode ($_) },
145 "json-utf-16be" => sub { encode "utf-16be", JSON::XS->new->encode ($_) },
146 "json-utf-32le" => sub { encode "utf-32le", JSON::XS->new->encode ($_) },
147 "json-utf-32be" => sub { encode "utf-32be", JSON::XS->new->encode ($_) },
148
149 "storable" => sub { Storable::nfreeze $_ },
150 "storable-file" => sub { open my $fh, ">", \my $buf; Storable::nstore_fd $_, $fh; $buf },
151
152 "clzf" => sub { require Compress::LZF; Compress::LZF::sfreeze_cr ($_) },
153 "yaml" => sub { require YAML; YAML::Dump ($_) },
154 );
155
156 $F{$opt_from}
157 or die "$opt_from: not a valid fromformat\n";
158
159 $T{$opt_to}
160 or die "$opt_from: not a valid toformat\n";
161
162 {
163 local $/;
164 binmode STDIN; # stupid perl sometimes thinks its funny
165 $_ = <STDIN>;
166 }
167
168 $_ = $F{$opt_from}->();
169 $_ = $T{$opt_to}->();
170
171 binmode STDOUT;
172 print $_;
173
174
175