File Coverage

blib/lib/Data/Clean/FromJSON.pm
Criterion Covered Total %
statement 22 23 95.6
branch 1 2 50.0
condition 2 5 40.0
subroutine 7 7 100.0
pod 2 2 100.0
total 34 39 87.1


line stmt bran cond sub pod time code
1             package Data::Clean::FromJSON;
2              
3             our $DATE = '2019-11-26'; # DATE
4             our $VERSION = '0.395'; # VERSION
5              
6 1     1   79013 use 5.010001;
  1         10  
7 1     1   4 use strict;
  1         2  
  1         15  
8 1     1   6 use warnings;
  1         1  
  1         23  
9              
10 1     1   362 use parent qw(Data::Clean);
  1         268  
  1         5  
11 1     1   4018 use vars qw($creating_singleton);
  1         3  
  1         134  
12              
13             sub new {
14 1     1 1 3 my ($class, %opts) = @_;
15 1 50 33     7 if (!%opts && !$creating_singleton) {
16 0         0 warn "You are creating a new ".__PACKAGE__." object without customizing options. ".
17             "You probably want to call get_cleanser() yet to get a singleton instead?";
18             }
19              
20 1   50     7 $opts{"JSON::PP::Boolean"} //= ['deref_scalar_one_or_zero'];
21              
22 1         10 $class->SUPER::new(%opts);
23             }
24              
25             sub get_cleanser {
26 1     1 1 110 my $class = shift;
27 1         3 local $creating_singleton = 1;
28 1         4 state $singleton = $class->new;
29 1         1623 $singleton;
30             }
31              
32             1;
33             # ABSTRACT: Clean data from JSON decoder
34              
35             __END__
36              
37             =pod
38              
39             =encoding UTF-8
40              
41             =head1 NAME
42              
43             Data::Clean::FromJSON - Clean data from JSON decoder
44              
45             =head1 VERSION
46              
47             This document describes version 0.395 of Data::Clean::FromJSON (from Perl distribution Data-Clean-ForJSON), released on 2019-11-26.
48              
49             =head1 SYNOPSIS
50              
51             use Data::Clean::FromJSON;
52             use JSON;
53             my $cleanser = Data::Clean::FromJSON->get_cleanser;
54             my $data = JSON->new->decode('[true]'); # -> [bless(do{\(my $o=1)},"JSON::XS::Boolean")]
55             my $cleaned = $cleanser->clean_in_place($data); # -> [1]
56              
57             =head1 DESCRIPTION
58              
59             This class can "clean" data that comes from a JSON encoder. Currently what it
60             does is:
61              
62             =over
63              
64             =item * Convert boolean objects to simple Perl values
65              
66             =back
67              
68             =head1 METHODS
69              
70             =head2 CLASS->get_cleanser => $obj
71              
72             Return a singleton instance, with default options. Use C<new()> if you want to
73             customize options.
74              
75             =head2 CLASS->new() => $obj
76              
77             =head2 $obj->clean_in_place($data) => $cleaned
78              
79             Clean $data. Modify data in-place.
80              
81             =head2 $obj->clone_and_clean($data) => $cleaned
82              
83             Clean $data. Clone $data first.
84              
85             =head1 FAQ
86              
87             =head2 Why am I getting 'Modification of a read-only value attempted at lib/Data/Clean.pm line xxx'?
88              
89             [2013-10-15 ] This is also from Data::Clone::clone() when it encounters
90             JSON::{PP,XS}::Boolean objects. You can use clean_in_place() instead of
91             clone_and_clean(), or clone your data using other cloner like L<Sereal>.
92              
93             =head1 ENVIRONMENT
94              
95             LOG_CLEANSER_CODE
96              
97             =head1 HOMEPAGE
98              
99             Please visit the project's homepage at L<https://metacpan.org/release/Data-Clean-ForJSON>.
100              
101             =head1 SOURCE
102              
103             Source repository is at L<https://github.com/perlancar/perl-Data-Clean-ForJSON>.
104              
105             =head1 BUGS
106              
107             Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Clean-ForJSON>
108              
109             When submitting a bug or request, please include a test-file or a
110             patch to an existing test-file that illustrates the bug or desired
111             feature.
112              
113             =head1 AUTHOR
114              
115             perlancar <perlancar@cpan.org>
116              
117             =head1 COPYRIGHT AND LICENSE
118              
119             This software is copyright (c) 2019, 2018, 2017, 2016, 2015, 2014, 2013, 2012 by perlancar@cpan.org.
120              
121             This is free software; you can redistribute it and/or modify it under
122             the same terms as the Perl 5 programming language system itself.
123              
124             =cut