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