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   337316 use v5.12;
  2         9  
6 2     2   13 use strict;
  2         4  
  2         58  
7 2     2   10 use warnings;
  2         5  
  2         158  
8              
9 2     2   607 use Data::Record::Serialize::Error { errors => ['json_backend'] }, -all;
  2         6  
  2         25  
10              
11 2     2   1599 use Moo::Role;
  2         35145  
  2         15  
12              
13             our $VERSION = '2.02';
14              
15              
16             BEGIN {
17 2     2   2839 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         1146 require Module::Version;
26 2 50       263729 if ( Module::Version::get_version( 'Cpanel::JSON::XS' ) >= $Cpanel_JSON_XS_VERSION ) {
    0          
27 2         3004 require Cpanel::JSON::XS;
28 2         8506 *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   502 use namespace::clean;
  2         20334  
  2         18  
42              
43             has '+numify' => ( is => 'ro', default => 1 );
44             has '+stringify' => ( is => 'ro', default => 1 );
45              
46 2     2   14 sub _needs_eol { 1 }
47              
48              
49              
50              
51              
52              
53              
54              
55              
56 1 50   1 1 9 sub to_bool { $_[1] ? \1 : \0 }
57              
58              
59              
60              
61              
62              
63 2     2 0 81 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__