This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update File-Path to CPAN version 2.14
[perl5.git] / cpan / JSON-PP / bin / json_pp
CommitLineData
d5424315
DG
1#!/usr/bin/perl
2
cee96d52 3BEGIN { pop @INC if $INC[-1] eq '.' }
d5424315
DG
4use strict;
5use Getopt::Long;
6
7use JSON::PP ();
8
9my $VERSION = '1.00';
10
11# imported from JSON-XS/bin/json_xs
12
13my %allow_json_opt = map { $_ => 1 } qw(
14 ascii latin1 utf8 pretty indent space_before space_after relaxed canonical allow_nonref
15 allow_singlequote allow_barekey allow_bignum loose escape_slash
16);
17
18
19GetOptions(
20 'v' => \( my $opt_verbose ),
21 'f=s' => \( my $opt_from = 'json' ),
22 't=s' => \( my $opt_to = 'json' ),
23 'json_opt=s' => \( my $json_opt = 'pretty' ),
24 'V' => \( my $version ),
25) or die "Usage: $0 [-v] -f from_format [-t to_format]\n";
26
27
28if ( $version ) {
29 print "$VERSION\n";
30 exit;
31}
32
33
34$json_opt = '' if $json_opt eq '-';
35
a1e5c561 36my @json_opt = grep { $allow_json_opt{ $_ } or die "'$_' is not a valid json option" } split/,/, $json_opt;
d5424315
DG
37
38my %F = (
39 'json' => sub {
40 my $json = JSON::PP->new;
41 $json->$_() for @json_opt;
42 $json->decode( $_ );
43 },
44 'eval' => sub {
45 my $v = eval "no strict;\n#line 1 \"input\"\n$_";
46 die "$@" if $@;
47 return $v;
48 },
49);
50
51
52my %T = (
53 'null' => sub { "" },
54 'json' => sub {
55 my $json = JSON::PP->new;
56 $json->$_() for @json_opt;
57 $json->encode( $_ );
58 },
59 'dumper' => sub {
60 require Data::Dumper;
61 Data::Dumper::Dumper($_)
62 },
63);
64
65
66
67$F{$opt_from}
68 or die "$opt_from: not a valid fromformat\n";
69
70$T{$opt_to}
71 or die "$opt_from: not a valid toformat\n";
72
73local $/;
74$_ = <STDIN>;
75
76$_ = $F{$opt_from}->();
77$_ = $T{$opt_to}->();
78
79print $_;
80
81
82__END__
83
84=pod
85
86=encoding utf8
87
88=head1 NAME
89
90json_pp - JSON::PP command utility
91
92=head1 SYNOPSIS
93
94 json_pp [-v] [-f from_format] [-t to_format] [-json_opt options_to_json]
95
96=head1 DESCRIPTION
97
98json_pp converts between some input and output formats (one of them is JSON).
99This program was copied from L<json_xs> and modified.
100
101The default input format is json and the default output format is json with pretty option.
102
103=head1 OPTIONS
104
105=head2 -f
106
107 -f from_format
108
109Reads a data in the given format from STDIN.
110
111Format types:
112
113=over
114
115=item json
116
117as JSON
118
119=item eval
120
121as Perl code
122
123=back
124
125=head2 -t
126
127Writes a data in the given format to STDOUT.
128
129=over
130
131=item null
132
133no action.
134
135=item json
136
137as JSON
138
139=item dumper
140
141as Data::Dumper
142
143=back
144
145=head2 -json_opt
146
147options to JSON::PP
148
149Acceptable options are:
150
151 ascii latin1 utf8 pretty indent space_before space_after relaxed canonical allow_nonref
152 allow_singlequote allow_barekey allow_bignum loose escape_slash
153
154=head2 -v
155
156Verbose option, but currently no action in fact.
157
158=head2 -V
159
160Prints version and exits.
161
162
163=head1 EXAMPLES
164
165 $ perl -e'print q|{"foo":"あい","bar":1234567890000000000000000}|' |\
166 json_pp -f json -t dumper -json_opt pretty,utf8,allow_bignum
167
168 $VAR1 = {
169 'bar' => bless( {
170 'value' => [
171 '0000000',
172 '0000000',
173 '5678900',
174 '1234'
175 ],
176 'sign' => '+'
177 }, 'Math::BigInt' ),
178 'foo' => "\x{3042}\x{3044}"
179 };
180
181 $ perl -e'print q|{"foo":"あい","bar":1234567890000000000000000}|' |\
182 json_pp -f json -t dumper -json_opt pretty
183
184 $VAR1 = {
185 'bar' => '1234567890000000000000000',
186 'foo' => "\x{e3}\x{81}\x{82}\x{e3}\x{81}\x{84}"
187 };
188
189=head1 SEE ALSO
190
191L<JSON::PP>, L<json_xs>
192
193=head1 AUTHOR
194
195Makamaka Hannyaharamitu, E<lt>makamaka[at]cpan.orgE<gt>
196
197
198=head1 COPYRIGHT AND LICENSE
199
200Copyright 2010 by Makamaka Hannyaharamitu
201
202This library is free software; you can redistribute it and/or modify
203it under the same terms as Perl itself.
204
205=cut
206