File Coverage

blib/lib/Data/Format/Pretty/JSON.pm
Criterion Covered Total %
statement 23 28 82.1
branch 4 6 66.6
condition 8 18 44.4
subroutine 4 5 80.0
pod 2 2 100.0
total 41 59 69.4


line stmt bran cond sub pod time code
1             package Data::Format::Pretty::JSON;
2              
3 1     1   581 use 5.010001;
  1         3  
  1         50  
4 1     1   7 use strict;
  1         1  
  1         36  
5 1     1   12 use warnings;
  1         2  
  1         368  
6              
7             require Exporter;
8             our @ISA = qw(Exporter);
9             our @EXPORT_OK = qw(format_pretty);
10              
11             our $VERSION = '0.11'; # VERSION
12              
13 0     0 1 0 sub content_type { "application/json" }
14              
15             sub format_pretty {
16 2     2 1 2658 my ($data, $opts) = @_;
17 2   50     11 $opts //= {};
18              
19 2         3 state $json;
20 2         14 my $interactive = (-t STDOUT);
21 2   100     12 my $pretty = $opts->{pretty} // 1;
22 2   33     27 my $color = $opts->{color} // $ENV{COLOR} // $interactive //
      33        
      33        
23             $opts->{pretty};
24 2   33     19 my $linum = $opts->{linum} // $ENV{LINUM} // 0;
      50        
25 2 50       7 if ($color) {
26 0         0 require JSON::Color;
27 0         0 JSON::Color::encode_json($data, {pretty=>$pretty, linum=>$linum})."\n";
28             } else {
29 2 100       17 if (!$json) {
30 1         19 require JSON;
31 1         32 $json = JSON->new->utf8->allow_nonref;
32             }
33 2         13 $json->pretty($pretty);
34 2 50       6 if ($linum) {
35 0         0 require String::LineNumber;
36 0         0 String::LineNumber::linenum($json->encode($data));
37             } else {
38 2         33 $json->encode($data);
39             }
40             }
41             }
42              
43             1;
44             # ABSTRACT: Pretty-print data structure as JSON
45              
46             __END__