Jmx4Perl/Util.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 6 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 95 CC 9 SCAR 26.7
CC
9
Cov
0%
dump_value
20.0
_canonicalize_value
20.0
dump_scalar
12.0
CRAP 14.4
1 #!/usr/bin/perl
2  
3 =head1 NAME
4  
5 JMX::Jmx4Perl::Util - Utility methods for Jmx4Perl
6  
7 =head1 DESCRIPTION
8  
9 This class contains utility methods mostly for tools like C<jmx4perl> or
10 C<j4psh> for things like formatting data output. All methods are 'static'
11 methods which needs to be called like in
12  
13 JMX::Jmx4Perl::Util->dump_value(...);
14  
15 There is no constructor.
16  
17 =over
18  
19 =cut
20  
21 package JMX::Jmx4Perl::Util;
22  
23 use Data::Dumper;
24 use JSON;
25  
26 =item $is_object = JMX::Jmx4Perl::Util->is_object_to_dump($val)
27  
28 For dumping out, checks whether C<$val> is an object (i.e. it is a ref but not a
29 JSON::XS::Boolean) or not.
30  
31 =cut
32  
33 sub is_object_to_dump {
34 my $self = shift;
35 my $val = shift;
36 return ref($val) && !JSON::is_bool($val);
37 }
38  
39 =item $text = JMX::Jmx4Perl::Util->dump_value($value,{ format => "json", boolean_string =>1})
40  
41 Return a formatted text representation useful for tools printing out complex
42 response values. Two modes are available: C<data> which is the default and uses
43 L<Data::Dumper> for creating a textual description and C<json> which return the
44 result as JSON value. When C<data> is used as format, booleans are returned as 0
45 for false and 1 for true exception when the option C<boolean_string> is given in
46 which case it returns C<true> or C<false>.
47  
48 =cut
49  
50 sub dump_value {
51 my $self = shift;
52 my $value = shift;
53 my $opts = shift || {};
54 if ($opts && ref($opts) ne "HASH") {
55 $opts = { $opts,@_ };
56 }
57 my $format = $opts->{format} || "data";
58 my $ret;
59 if ($format eq "json") {
60 # Return a JSON representation of the data structure
61 my $json = JSON->new->allow_nonref;
62 $ret = $json->pretty->encode($value);
63 } else {
64 # Use data dumper, but resolve all JSON::XS::Booleans to either 0/1 or
65 # true/false
66 local $Data::Dumper::Terse = 1;
67 local $Data::Dumper::Indent = 1;
68 # local $Data::Dumper::Useqq = 1;
69 local $Data::Dumper::Deparse = 0;
70 local $Data::Dumper::Quotekeys = 0;
71 local $Data::Dumper::Sortkeys = 1;
72 $ret = Dumper($self->_canonicalize_value($value,$opts->{booleans}));
73 }
74 my $indent = $opts->{indent} ? " " x $opts->{indent} : " ";
75 $ret =~ s/^/$indent/gm;
76 return $ret;
77 }
78  
79 =item $dump = JMX::Jmx4Perl::Util->dump_scalar($val,$format)
80  
81 Dumps a scalar value with special handling for booleans. If C<$val> is a
82 L<JSON::XS::Boolean> it is returned as string formatted according to
83 C<$format>. C<$format> must contain two values separated bu C</>. The first
84 part is taken for a C<true> value, the second for a C<false> value. If no
85 C<$format> is given, C<[true]/[false]> is used.
86  
87 =cut
88  
89 sub dump_scalar {
90 my $self = shift;
91 my $value = shift;
92 my $format = shift || "[true]/[false]";
93  
94 if (JSON::is_bool($value)) {
95 my ($true,$false) = split /\//,$format;
96 if ($value eq JSON::true) {
97 return $true;
98 } else {
99 return $false;
100 }
101 } else {
102 return $value;
103 }
104 }
105  
106 # Replace all boolean values in
107 sub _canonicalize_value {
108 my $self = shift;
109 my $value = shift;
110 my $booleans = shift;
111 if (ref($value) eq "HASH") {
112 for my $k (keys %$value) {
113 $value->{$k} = $self->_canonicalize_value($value->{$k},$booleans);
114 }
115 return $value;
116 } elsif (ref($value) eq "ARRAY") {
117 for my $i (0 .. $#$value) {
118 $value->[$i] = $self->_canonicalize_value($value->[$i],$booleans);
119 }
120 return $value;
121 } elsif (JSON::is_bool($value)) {
122 $self->dump_scalar($value,$booleans);
123 } else {
124 return $value;
125 }
126 }
127  
128 =back
129  
130 =cut
131  
132 1;