| 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 | 'dont-bloody-escape-unicode' => 1, | ||||||||||||||
| 23 | 'terse' => 0, | ||||||||||||||
| 24 | 'indent' => 1, | ||||||||||||||
| 25 | ); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 0 | ▶ | sub usage { return | ||||||||||||
|
Subroutine
usage: not called CC=1 SCAR=6.9
| |||||||||||||||
| 28 | 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 | ▶ | if( ! Getopt::Long::GetOptions( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | 'i=s' => \$INPUT_FILE, | ||||||||||||||
| 36 | 0 | ▶ | 'I=s' => sub { $INPUT_STRING = Encode::decode_utf8($_[1]) }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 37 | 'o=s' => \$OUTPUT_FILE, | ||||||||||||||
| 38 | 'terse|r!' => \$params{'terse'}, | ||||||||||||||
| 39 | 'indent|d!' => \$params{'indent'}, | ||||||||||||||
| 40 | 0 | ▶ | 'escape-unicode|e!' => sub { $params{'dont-bloody-escape-unicode'} = $_[1] ? 0 : 1 }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||
| 41 | 0 | ) ){ die usage() } | |||||||||||||
| 42 | |||||||||||||||
| 43 | 2 | ▶ | if( defined $INPUT_FILE ){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 44 | 2 | $INPUT_STRING = Data::Roundtrip::read_from_file($INPUT_FILE); | |||||||||||||
| 45 | 0 | ▶ | if( ! defined $INPUT_STRING ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::read_from_file()'." has failed.\n"; exit(1) } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | } elsif( ! defined $INPUT_STRING ){ | ||||||||||||||
| 47 | # read from STDIN | ||||||||||||||
| 48 | 0 | $INPUT_STRING = do { local $/; <STDIN> } | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | 2 | my $result = Data::Roundtrip::json2dump($INPUT_STRING, \%params); | |||||||||||||
| 52 | 2 | ▶ | if( ! defined $result ){ print STDERR "$0 : error, call to ".'Data::Roundtrip::json2dump()'." has failed.\n"; exit(1) } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | |||||||||||||||
| 54 | 1 | ▶ | if( defined $OUTPUT_FILE ){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 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
| |||||||||||||||
| 56 | } else { | ||||||||||||||
| 57 | 1 | print STDOUT $result | |||||||||||||
| 58 | } | ||||||||||||||
| 59 | 1; | ||||||||||||||
| 60 | __END__ | ||||||||||||||