File Coverage

blib/lib/Data/CleanJSON.pm
Criterion Covered Total %
statement 75 132 56.8
branch 49 88 55.6
condition 8 15 53.3
subroutine 10 10 100.0
pod 2 2 100.0
total 144 247 58.3


line stmt bran cond sub pod time code
1             package Data::CleanJSON;
2              
3             our $DATE = '2017-01-03'; # DATE
4             our $VERSION = '0.002'; # VERSION
5              
6 1     1   15193 use 5.010001;
  1         4  
7 1     1   6 use strict;
  1         1  
  1         34  
8 1     1   103 use warnings;
  1         4  
  1         47  
9              
10 1     1   5 use Exporter qw(import);
  1         2  
  1         1546  
11             our @EXPORT_OK = qw(
12             clean_json_in_place
13             clone_and_clean_json
14             );
15              
16             sub _clone {
17 3 50   3   4 if (eval { require Data::Clone; 1 }) {
  3         594  
  3         1227  
18 3         17 Data::Clone::clone(@_);
19             } else {
20 0         0 require Clone::PP;
21 0         0 Clone::PP::clone(@_);
22             }
23             }
24              
25             # generated with Data::Clean version 0.44, Data::Clean::JSON version 0.37
26             sub clean_json_in_place {
27 4     4 1 2683 require Scalar::Util;
28 3     3   4 state $sub_unbless = sub { my $ref = shift;
29            
30 3         3 my $r = ref($ref);
31             # not a reference
32 3 50       5 return $ref unless $r;
33            
34             # return if not a blessed ref
35 3 50       25 my ($r2, $r3) = "$ref" =~ /(.+)=(.+?)\(/
36             or return $ref;
37            
38 3 50       6 if ($r3 eq 'HASH') {
    0          
    0          
39 3         9 return { %$ref };
40             } elsif ($r3 eq 'ARRAY') {
41 0         0 return [ @$ref ];
42             } elsif ($r3 eq 'SCALAR') {
43 0         0 return \( my $copy = ${$ref} );
  0         0  
44             } else {
45 0         0 die "Can't handle $ref";
46             }
47 4         8 };
48 4         5 my $data = shift;
49 4         4 state %refs;
50 4         1 state $ctr_circ;
51 4         4 state $process_array;
52 4         4 state $process_hash;
53 4 100   3   8 if (!$process_array) { $process_array = sub { my $a = shift; for my $e (@$a) { my $ref=ref($e);
  3         4  
  3         8  
  1         2  
54 1 0 33     14 if ($ref && $refs{ $e }++) { if (++$ctr_circ <= 1) { $e = _clone($e); redo } else { $e = 'CIRCULAR'; $ref = '' } }
  0 50       0  
  0 50       0  
  0 50       0  
  0 50       0  
  0 50       0  
    50          
    50          
    50          
55 0         0 elsif ($ref eq 'DateTime') { $e = $e->epoch; $ref = ref($e) }
  0         0  
56 0         0 elsif ($ref eq 'Math::BigInt') { $e = $e->bstr; $ref = ref($e) }
  0         0  
57 0         0 elsif ($ref eq 'Regexp') { $e = "$e"; $ref = "" }
  0         0  
58 0         0 elsif ($ref eq 'SCALAR') { $e = ${ $e }; $ref = ref($e) }
  0         0  
  0         0  
59 0         0 elsif ($ref eq 'Time::Moment') { $e = $e->epoch; $ref = ref($e) }
  0         0  
60 0         0 elsif ($ref eq 'version') { $e = "$e"; $ref = "" }
  0         0  
61 1         4 elsif (Scalar::Util::blessed($e)) { $e = $sub_unbless->($e); $ref = ref($e) }
  1         3  
62 1   50     4 my $reftype=Scalar::Util::reftype($e)//"";
63 1 50       10 if ($reftype eq "ARRAY") { $process_array->($e) }
  0 50       0  
    0          
64 1         2 elsif ($reftype eq "HASH") { $process_hash->($e) }
65 0         0 elsif ($ref) { $e = $ref; $ref = "" }
  0         0  
66 1         4 } } }
67 4 100   5   8 if (!$process_hash) { $process_hash = sub { my $h = shift; for my $k (keys %$h) { my $ref=ref($h->{$k});
  5         6  
  5         16  
  7         9  
68 7 50 66     52 if ($ref && $refs{ $h->{$k} }++) { if (++$ctr_circ <= 1) { $h->{$k} = _clone($h->{$k}); redo } else { $h->{$k} = 'CIRCULAR'; $ref = '' } }
  1 100       2  
  1 50       3  
  1 50       2  
  0 50       0  
  0 100       0  
    50          
    100          
    100          
69 0         0 elsif ($ref eq 'DateTime') { $h->{$k} = $h->{$k}->epoch; $ref = ref($h->{$k}) }
  0         0  
70 0         0 elsif ($ref eq 'Math::BigInt') { $h->{$k} = $h->{$k}->bstr; $ref = ref($h->{$k}) }
  0         0  
71 0         0 elsif ($ref eq 'Regexp') { $h->{$k} = "$h->{$k}"; $ref = "" }
  0         0  
72 1         1 elsif ($ref eq 'SCALAR') { $h->{$k} = ${ $h->{$k} }; $ref = ref($h->{$k}) }
  1         3  
  1         2  
73 0         0 elsif ($ref eq 'Time::Moment') { $h->{$k} = $h->{$k}->epoch; $ref = ref($h->{$k}) }
  0         0  
74 1         3 elsif ($ref eq 'version') { $h->{$k} = "$h->{$k}"; $ref = "" }
  1         3  
75 1         3 elsif (Scalar::Util::blessed($h->{$k})) { $h->{$k} = $sub_unbless->($h->{$k}); $ref = ref($h->{$k}) }
  1         2  
76 6   100     16 my $reftype=Scalar::Util::reftype($h->{$k})//"";
77 6 100       14 if ($reftype eq "ARRAY") { $process_array->($h->{$k}) }
  2 100       3  
    100          
78 1         3 elsif ($reftype eq "HASH") { $process_hash->($h->{$k}) }
79 1         1 elsif ($ref) { $h->{$k} = $ref; $ref = "" }
  1         3  
80 1         5 } } }
81 4         9 %refs = (); $ctr_circ=0;
  4         4  
82 4         6 for ($data) { my $ref=ref($_);
  4         6  
83 4 0 33     53 if ($ref && $refs{ $_ }++) { if (++$ctr_circ <= 1) { $_ = _clone($_); redo } else { $_ = 'CIRCULAR'; $ref = '' } }
  0 50       0  
  0 50       0  
  0 50       0  
  0 50       0  
  0 50       0  
    50          
    50          
    100          
84 0         0 elsif ($ref eq 'DateTime') { $_ = $_->epoch; $ref = ref($_) }
  0         0  
85 0         0 elsif ($ref eq 'Math::BigInt') { $_ = $_->bstr; $ref = ref($_) }
  0         0  
86 0         0 elsif ($ref eq 'Regexp') { $_ = "$_"; $ref = "" }
  0         0  
87 0         0 elsif ($ref eq 'SCALAR') { $_ = ${ $_ }; $ref = ref($_) }
  0         0  
  0         0  
88 0         0 elsif ($ref eq 'Time::Moment') { $_ = $_->epoch; $ref = ref($_) }
  0         0  
89 0         0 elsif ($ref eq 'version') { $_ = "$_"; $ref = "" }
  0         0  
90 1         2 elsif (Scalar::Util::blessed($_)) { $_ = $sub_unbless->($_); $ref = ref($_) }
  1         2  
91 4   50     11 my $reftype=Scalar::Util::reftype($_)//"";
92 4 100       8 if ($reftype eq "ARRAY") { $process_array->($_) }
  1 50       2  
    0          
93 3         24 elsif ($reftype eq "HASH") { $process_hash->($_) }
94 0         0 elsif ($ref) { $_ = $ref; $ref = "" }
  0         0  
95             }
96             $data
97 4         5 }
98              
99              
100             sub clone_and_clean_json {
101 2     2 1 1660 my $data = _clone(shift);
102 2         3 clean_json_in_place($data);
103             }
104              
105             1;
106             # ABSTRACT: Clean data so it is safe to output to JSON
107              
108             __END__