File Coverage

script/json2json.pl
Criterion Covered Total %
statement 36 47 76.6
branch 6 14 42.8
condition n/a
subroutine 6 8 75.0
pod n/a
total 48 69 69.5


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3 2     2   8648 use strict;
  2         3  
  2         69  
4 2     2   7 use warnings;
  2         2  
  2         96  
5              
6 2     2   905 use utf8;
  2         469  
  2         10  
7              
8 2         321783 our $VERSION = '0.31';
9              
10 2     2   89 binmode STDERR, ':encoding(UTF-8)';
  2         1559  
  2         44  
  2         15  
11 2         2164 binmode STDOUT, ':encoding(UTF-8)';
12 2         90 binmode STDIN, ':encoding(UTF-8)';
13              
14 2     2   1428 use Getopt::Long qw(:config no_ignore_case);
  2         29672  
  2         14  
15              
16 2     2   1718 use Data::Roundtrip;
  2         12  
  2         17  
17              
18 2         70 my $INPUT_STRING = undef;
19 2         20 my $INPUT_FILE = undef;
20 2         16 my $OUTPUT_FILE = undef;
21 2         9 my %params = (
22             'escape-unicode' => 0,
23             'pretty' => 0,
24             );
25              
26             sub usage { return
27 0     0     "Usage : $0 [--I 'a-json-string' | --i 'afile.json'] [--o afile] [--(no-)escape-unicode|-e] [--(no-)pretty|-p]\n"
28             ."\nIt will read a JSON string from command line (-I), or from a file (-i)\n"
29             ."\nor from STDIN (beware 4K limit on linux terminal, see CAVEATS for workaround).\n"
30             ."It will print its contents as JSON to STDOUT or to a file (--o).\n"
31             ."It can escape/un-escape unicode characters (--escape-unicode) and/or do pretty-printing (--pretty).\n"
32             }
33 2 50       24 if( ! Getopt::Long::GetOptions(
34             'i=s' => \$INPUT_FILE,
35 0     0   0 'I=s' => sub { $INPUT_STRING = Encode::decode_utf8($_[1]) },
36             'o=s' => \$OUTPUT_FILE,
37             'pretty|p!' => \$params{'pretty'},
38             'escape-unicode|e!' => \$params{'escape-unicode'},
39 0         0 ) ){ die usage() }
40              
41 2 50       2078 if( defined $INPUT_FILE ){
    0          
42 2         13 $INPUT_STRING = Data::Roundtrip::read_from_file($INPUT_FILE);
43 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  
44             } elsif( ! defined $INPUT_STRING ){
45             # read from STDIN
46 0         0 $INPUT_STRING = do { local $/; }
  0         0  
  0         0  
47             }
48              
49 2         9 my $result = Data::Roundtrip::json2json($INPUT_STRING, \%params);
50 2 100       5 if( ! defined $result ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::json2jsonl()'." has failed.\n"; exit(1) }
  1         4  
  1         0  
51              
52 1 50       4 if( defined $OUTPUT_FILE ){
53 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  
54             } else {
55 1         5 print STDOUT $result
56             }
57             1;
58             __END__