File Coverage

script/json2yaml.pl
Criterion Covered Total %
statement 36 48 75.0
branch 6 14 42.8
condition n/a
subroutine 6 9 66.6
pod n/a
total 48 71 67.6


line stmt bran cond sub pod time code
1             #!/usr/bin/env perl
2              
3 2     2   9296 use strict;
  2         4  
  2         72  
4 2     2   54 use warnings;
  2         11  
  2         122  
5              
6 2     2   920 use utf8;
  2         532  
  2         9  
7              
8 2         336199 our $VERSION = '0.31';
9              
10 2     2   93 binmode STDERR, ':encoding(UTF-8)';
  2         1586  
  2         32  
  2         12  
11 2         2226 binmode STDOUT, ':encoding(UTF-8)';
12 2         89 binmode STDIN, ':encoding(UTF-8)';
13              
14 2     2   1570 use Getopt::Long qw(:config no_ignore_case);
  2         31360  
  2         13  
15              
16 2     2   1635 use Data::Roundtrip;
  2         8  
  2         12  
17              
18 2         71 my $INPUT_STRING = undef;
19 2         20 my $INPUT_FILE = undef;
20 2         16 my $OUTPUT_FILE = undef;
21 2         6 my %params = (
22             'escape-unicode' => 0,
23             # 'pretty' => 0, # not supported
24             );
25              
26             sub usage { return
27 0     0     "Usage : $0 [--I 'a-json-string' | --i 'afile.json'] [--o afile] [--(no-)escape-unicode|-e]\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 YAML to STDOUT or to a file (--o).\n"
31             ."It can escape/un-escape unicode characters (--escape-unicode). Pretty-printing is not supported.\n"
32             }
33 2 50       27 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 0     0   0 'pretty|p!' => sub { warn "--pretty has no effect for converting to YAML and will be ignored.\n" },
38             'escape-unicode|e!' => \$params{'escape-unicode'},
39 0         0 ) ){ die usage() }
40              
41 2 50       2103 if( defined $INPUT_FILE ){
    0          
42 2         13 $INPUT_STRING = Data::Roundtrip::read_from_file($INPUT_FILE);
43 2 50       8 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         11 my $result = Data::Roundtrip::json2yaml($INPUT_STRING, \%params);
50 2 100       9 if( ! defined $result ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::json2yaml()'." has failed.\n"; exit(1) }
  1         5  
  1         0  
51              
52 1 50       5 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         7 print STDOUT $result
56             }
57             1;
58             __END__