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