script/json2yaml.pl

filter: Statementstmt 73.3% 33 / 45 Branchbran 42.8% 6 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 62.5% 5 / 8 Podpod n/a totaltotal 65.6% 44 / 67 CC 1 SCAR 3.2
CC
1
Cov
66%
usage
2.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.4
« script/yaml2perl.pl ↑ Summary script/json2json.pl »
1 #!/usr/bin/env perl
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.31';
9  
10 2 binmode STDERR, ':encoding(UTF-8)';
11 2 binmode STDOUT, ':encoding(UTF-8)';
12 2 binmode STDIN, ':encoding(UTF-8)';
13  
14 2 ▶use Getopt::Long qw(:config no_ignore_case);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Data::Roundtrip;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 2 my $INPUT_STRING = undef;
19 2 my $INPUT_FILE = undef;
20 2 my $OUTPUT_FILE = undef;
21 2 my %params = (
22 'escape-unicode' => 0,
23 # 'pretty' => 0, # not supported
24 );
25  
26 0 ▶sub usage { return
Subroutine usage: not called CC=1 SCAR=6.9
27 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 ▶if( ! Getopt::Long::GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
unless (&Getopt::Long::GetOptions("i=s", \$INPUT_FILE, "I=s", sub { $INPUT_STRING = &Encode::decode_utf8($_[1]); } , "o=s", \$OUTPUT_FILE, "pretty|p!", sub { warn "--pretty has no effect for converting to YAML and will be ignored.\n"; } , "escape-unicode|e!", \$params{"escape-unicode"})) 0 2 2
34 'i=s' => \$INPUT_FILE,
35 0 ▶ 'I=s' => sub { $INPUT_STRING = Encode::decode_utf8($_[1]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
36 'o=s' => \$OUTPUT_FILE,
37 0 ▶ 'pretty|p!' => sub { warn "--pretty has no effect for converting to YAML and will be ignored.\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 'escape-unicode|e!' => \$params{'escape-unicode'},
39 0 ) ){ die usage() }
40  
41 2 ▶if( defined $INPUT_FILE ){
Branch: 2 branches
BranchTrueFalseTotal
if (defined $INPUT_FILE) { } 2 0 2
elsif (not defined $INPUT_STRING) { } 0 0 0
42 2 $INPUT_STRING = Data::Roundtrip::read_from_file($INPUT_FILE);
43 0 ▶ if( ! defined $INPUT_STRING ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::read_from_file()'." has failed.\n"; exit(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $INPUT_STRING) 0 2 2
44 } elsif( ! defined $INPUT_STRING ){
45 # read from STDIN
46 0 $INPUT_STRING = do { local $/; <STDIN> }
47 }
48  
49 2 my $result = Data::Roundtrip::json2yaml($INPUT_STRING, \%params);
50 2 ▶if( ! defined $result ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::json2yaml()'." has failed.\n"; exit(1) }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 1 1 2
51  
52 1 ▶if( defined $OUTPUT_FILE ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $OUTPUT_FILE) { } 0 1 1
53 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) }
Branch: 1 branch
BranchTrueFalseTotal
unless (Data::Roundtrip::write_to_file($OUTPUT_FILE, $result)) 0 0 0
54 } else {
55 1 print STDOUT $result
56 }
57 1;
58 __END__
« script/yaml2perl.pl ↑ Summary script/json2json.pl »