Serialize/Encode/json.pm

filter: Statementstmt 92.3% 48 / 52 Branchbran 50.0% 5 / 10 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 13 / 13 Podpod 33.3% 1 / 3 totaltotal 83.9% 68 / 81 CC 8 SCAR 10.3
CC
8
Cov
84%
BEGIN
6.4
setup
4.0
to_bool
2.1
CRAP 2.8
« Serialize/Role/Base.pm ↑ Summary Serialize.pm »
1 package Data::Record::Serialize::Encode::json;
2  
3 # ABSTRACT: encoded a record as JSON
4  
5 2 ▶use v5.12;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Data::Record::Serialize::Error { errors => ['json_backend'] }, -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Module::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '2.05';
15 2 our $JSON;
16  
17 2 ▶BEGIN {
Subroutine BEGIN: called CC=4 SCAR=18.6
18 2 my $Cpanel_JSON_XS_VERSION = 3.0236;
19  
20 # Module::Version doesn't load the code, so avoids loading
21 # Cpanel::JSON::XS's version of JSON::PP::Boolean which prevents
22 # loading the version provided by JSON::PP if Cpanel::JSON::XS is
23 # too old. Symbol::delete_package could be used to remove
24 # Cpanel::JSON::XS's version, but it's better not to load it in
25 # the first place.
26 2 $JSON = do {
27 2 ▶ if ( ( Module::Version::get_version( 'Cpanel::JSON::XS' ) // 0 >= $Cpanel_JSON_XS_VERSION ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (Module::Version::get_version('Cpanel::JSON::XS') // 0 >= $Cpanel_JSON_XS_VERSION) { } 2 0 2
elsif (eval { do { require JSON::PP; 1 } }) { } 0 0 0
Condition50%1/2
Module::Version::get_version("Cpanel::JSON::XS") // 0 >= $Cpanel_JSON_XS_VERSION
l!l
20
MC/DC0%0/1
Module::Version::get_version("Cpanel::JSON::XS") // 0 >= $Cpanel_JSON_XS_VERSION
Module::Version::get_version("Cpanel::JSON::XS")
Truth table50%1/2
A
A= Module::Version::get_version("Cpanel::JSON::XS")
Aresult
11
00
28 2 require Cpanel::JSON::XS;
29 2 'Cpanel::JSON::XS';
30 }
31 0 elsif ( eval { require JSON::PP; 1; } ) {
32 0 'JSON::PP';
33 }
34 else {
35 0 error(
36 'json_backend',
37 q{can't find either Cpanel::JSON::XS (>= $Cpanel_JSON_XS_VERSION) or JSON::PP. Please install one of them.},
38 );
39 }
40 };
41  
42 }
43  
44 2 ▶use constant ENCODER_OPTIONS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
45 ascii latin1 utf8 pretty indent space_before space_after canonical
46 allow_blessed convert_blessed
47 2 );
48  
49 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
50  
51  
52 2 has [ grep { $_ ne 'utf8' } ENCODER_OPTIONS ] => ( is => 'ro', default => !!0 );
53 2 has utf8 => ( is => 'ro', default => !!1 );
54  
55 2 has _encoder => (
56 is => 'rwp',
57 init_arg => undef,
58 clearer => 1,
59 predicate => 1,
60 );
61  
62 2 has '+numify' => ( is => 'ro', default => 1 );
63 2 has '+stringify' => ( is => 'ro', default => 1 );
64  
65 24 ▶sub _needs_eol { 1 }
Subroutine _needs_eol: called CC=1 SCAR=0.0
66  
67  
68  
69  
70  
71  
72  
73  
74  
75 1 ▶sub to_bool { $_[1] ? \1 : \0 }
Subroutine to_bool: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 1 0 1
76  
77  
78  
79  
80  
81  
82 23 ▶sub setup {
Subroutine setup: called CC=4 SCAR=13.9
Pod undocumented
83  
84 23 my $self = shift;
85 23 ▶ return if $self->_has_encoder;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_encoder 0 23 23
86  
87 23 my $json = $JSON->new;
88  
89 23 for my $option ( ENCODER_OPTIONS ) {
90 230 ▶ next unless $self->$option;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->$option 189 41 230
91 41 $json = $json->$option( $self->$option );
92 }
93  
94 23 $self->_set__encoder( $json );
95 }
96  
97  
98  
99  
100  
101  
102  
103 24 ▶sub encode { $_[0]->_encoder->encode( $_[1] ) }
Subroutine encode: called CC=1 SCAR=0.0
Pod undocumented
104  
105 2 with 'Data::Record::Serialize::Role::Encode';
106  
107 2 1;
108  
109 #
110 # This file is part of Data-Record-Serialize
111 #
112 # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
113 #
114 # This is free software, licensed under:
115 #
116 # The GNU General Public License, Version 3, June 2007
117 #
118  
119 __END__
« Serialize/Role/Base.pm ↑ Summary Serialize.pm »