File Coverage

blib/lib/Data/Record/Serialize/Encode/json.pm
Criterion Covered Total %
statement 25 28 89.2
branch 2 6 33.3
condition n/a
subroutine 10 10 100.0
pod 1 2 50.0
total 38 46 82.6


line stmt bran cond sub pod time code
1             package Data::Record::Serialize::Encode::json;
2              
3             # ABSTRACT: encoded a record as JSON
4              
5 2     2   167027 use v5.12;
  2         9  
6 2     2   7 use strict;
  2         3  
  2         40  
7 2     2   6 use warnings;
  2         3  
  2         118  
8              
9 2     2   425 use Data::Record::Serialize::Error { errors => ['json_backend'] }, -all;
  2         15  
  2         21  
10              
11 2     2   1086 use Moo::Role;
  2         22481  
  2         11  
12              
13             our $VERSION = '2.03';
14              
15              
16             BEGIN {
17 2     2   1991 my $Cpanel_JSON_XS_VERSION = 3.0236;
18              
19             # Module::Version doesn't load the code, so avoids loading
20             # Cpanel::JSON::XS's version of JSON::PP::Boolean which prevents
21             # loading the version provided by JSON::PP if Cpanel::JSON::XS is
22             # too old. Symbol::delete_package could be used to remove
23             # Cpanel::JSON::XS's version, but it's better not to load it in
24             # the first place.
25 2         806 require Module::Version;
26 2 50       204794 if ( Module::Version::get_version( 'Cpanel::JSON::XS' ) >= $Cpanel_JSON_XS_VERSION ) {
    0          
27 2         3140 require Cpanel::JSON::XS;
28 2         8487 *encode_json = \&Cpanel::JSON::XS::encode_json;
29             }
30 0         0 elsif ( eval { require JSON::PP } ) {
31 0         0 *encode_json = \&JSON::PP::encode_json;
32             }
33             else {
34 0         0 error(
35             'json_backend',
36             q{can't find either Cpanel::JSON::XS (>= $Cpanel_JSON_XS_VERSION) or JSON::PP. Please install one of them.},
37             );
38             }
39             }
40              
41 2     2   640 use namespace::clean;
  2         21229  
  2         15  
42              
43             has '+numify' => ( is => 'ro', default => 1 );
44             has '+stringify' => ( is => 'ro', default => 1 );
45              
46 2     2   9 sub _needs_eol { 1 }
47              
48              
49              
50              
51              
52              
53              
54              
55              
56 1 50   1 1 5 sub to_bool { $_[1] ? \1 : \0 }
57              
58              
59              
60              
61              
62              
63 2     2 0 49 sub encode { encode_json( $_[1] ) }
64              
65             with 'Data::Record::Serialize::Role::Encode';
66              
67             1;
68              
69             #
70             # This file is part of Data-Record-Serialize
71             #
72             # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
73             #
74             # This is free software, licensed under:
75             #
76             # The GNU General Public License, Version 3, June 2007
77             #
78              
79             __END__