File Coverage

script/json2perl.pl
Criterion Covered Total %
statement 36 48 75.0
branch 6 16 37.5
condition n/a
subroutine 6 9 66.6
pod n/a
total 48 73 65.7


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3 2     2   12957 use strict;
  2         4  
  2         87  
4 2     2   12 use warnings;
  2         3  
  2         138  
5              
6 2     2   1145 use utf8;
  2         691  
  2         14  
7              
8 2         386241 our $VERSION = '0.31';
9              
10 2     2   121 binmode STDERR, ':encoding(UTF-8)';
  2         1873  
  2         35  
  2         12  
11 2         2416 binmode STDOUT, ':encoding(UTF-8)';
12 2         171 binmode STDIN, ':encoding(UTF-8)';
13              
14 2     2   1877 use Getopt::Long qw(:config no_ignore_case);
  2         42356  
  2         21  
15              
16 2     2   2001 use Data::Roundtrip;
  2         9  
  2         15  
17              
18 2         75 my $INPUT_STRING = undef;
19 2         40 my $INPUT_FILE = undef;
20 2         3 my $OUTPUT_FILE = undef;
21 2         13 my %params = (
22             'dont-bloody-escape-unicode' => 1,
23             'terse' => 0,
24             'indent' => 1,
25             );
26              
27             sub usage { return
28 0     0     "Usage : $0 [--I 'a-json-string' | --i 'afile.json'] [--o afile] [--(no-)escape-unicode|-e] [--(no-)pretty] [--(no-)terse] [--(no-)indent]\n"
29             ."\nIt will read a JSON string from command line (-I), or from a file (-i)\n"
30             ."\nor from STDIN (beware 4K limit on linux terminal, see CAVEATS for workaround).\n"
31             ."It will print its contents as a Perl variable (dump) to STDOUT or to a file (--o).\n"
32             ."It can escape/un-escape unicode characters (--escape-unicode) and/or --terse and/or --indent.\n"
33             }
34 2 50       34 if( ! Getopt::Long::GetOptions(
35             'i=s' => \$INPUT_FILE,
36 0     0   0 'I=s' => sub { $INPUT_STRING = Encode::decode_utf8($_[1]) },
37             'o=s' => \$OUTPUT_FILE,
38             'terse|r!' => \$params{'terse'},
39             'indent|d!' => \$params{'indent'},
40 0 0   0   0 'escape-unicode|e!' => sub { $params{'dont-bloody-escape-unicode'} = $_[1] ? 0 : 1 },
41 0         0 ) ){ die usage() }
42              
43 2 50       2480 if( defined $INPUT_FILE ){
    0          
44 2         16 $INPUT_STRING = Data::Roundtrip::read_from_file($INPUT_FILE);
45 2 50       9 if( ! defined $INPUT_STRING ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::read_from_file()'." has failed.\n"; exit(1) }
  0         0  
  0         0  
46             } elsif( ! defined $INPUT_STRING ){
47             # read from STDIN
48 0         0 $INPUT_STRING = do { local $/; }
  0         0  
  0         0  
49             }
50              
51 2         11 my $result = Data::Roundtrip::json2dump($INPUT_STRING, \%params);
52 2 100       8 if( ! defined $result ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::json2dump()'." has failed.\n"; exit(1) }
  1         6  
  1         0  
53              
54 1 50       3 if( defined $OUTPUT_FILE ){
55 0 0       0 if( ! Data::Roundtrip::write_to_file($OUTPUT_FILE, $result) ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::write_to_file()'." has failed for '$OUTPUT_FILE'.\n"; exit(1) }
  0         0  
  0         0  
56             } else {
57 1         6 print STDOUT $result
58             }
59             1;
60             __END__