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 1     1   590 use v5.12;
  1         4  
6 1     1   17 use strict;
  1         2  
  1         20  
7 1     1   5 use warnings;
  1         2  
  1         37  
8              
9 1     1   6 use Data::Record::Serialize::Error { errors => ['json_backend'] }, -all;
  1         1  
  1         10  
10              
11 1     1   645 use Moo::Role;
  1         13387  
  1         5  
12              
13             our $VERSION = '1.05';
14              
15              
16             BEGIN {
17 1     1   734 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 1         473 require Module::Version;
26 1 50       105768 if ( Module::Version::get_version( 'Cpanel::JSON::XS' ) >= $Cpanel_JSON_XS_VERSION ) {
    0          
27 1         1498 require Cpanel::JSON::XS;
28 1         3879 *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 1     1   7 use namespace::clean;
  1         3  
  1         14  
42              
43             has '+numify' => ( is => 'ro', default => 1 );
44             has '+stringify' => ( is => 'ro', default => 1 );
45              
46 2     2   20 sub _needs_eol { 1 }
47              
48              
49              
50              
51              
52              
53              
54              
55              
56 1 50   1 1 53 sub to_bool { $_[1] ? \1 : \0 }
57              
58              
59              
60              
61              
62              
63 2     2 0 56 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__
80              
81             =pod
82              
83             =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory
84              
85             =head1 NAME
86              
87             Data::Record::Serialize::Encode::json - encoded a record as JSON
88              
89             =head1 VERSION
90              
91             version 1.05
92              
93             =head1 SYNOPSIS
94              
95             use Data::Record::Serialize;
96              
97             my $s = Data::Record::Serialize->new( encode => 'json', ... );
98              
99             $s->send( \%record );
100              
101             =head1 DESCRIPTION
102              
103             B<Data::Record::Serialize::Encode::json> encodes a record as JSON.
104              
105             If a field's type is C<N> or C<I>, it will be properly encoded by JSON
106             as a number. Field's with type C<S> are force to be strings.
107              
108             Boolean fields (type C<B>) are transformed into values recognized by
109             the back-end encoder.
110              
111             The output consists of I<concatenated> JSON objects, and is mostly easily
112             read by an incremental decoder, e.g.
113              
114             use JSON::MaybeXS;
115              
116             @data = JSON->new->incr_parse( $json );
117              
118             It performs the L<Data::Record::Serialize::Role::Encode> role.
119              
120             =head1 METHODS
121              
122             =head2 to_bool
123              
124             $bool = $self->to_bool( $truthy );
125              
126             Convert a truthy value to something that the JSON encoders will recognize as a boolean.
127              
128             =head1 INTERNALS
129              
130             =for Pod::Coverage encode
131              
132             =for Pod::Coverage numify
133             stringify
134             encode_json
135              
136             =head1 INTERFACE
137              
138             There are no additional attributes which may be passed to
139             L<< Data::Record::Serialize::new|Data::Record::Serialize/new >>.
140              
141             =head1 SUPPORT
142              
143             =head2 Bugs
144              
145             Please report any bugs or feature requests to bug-data-record-serialize@rt.cpan.org or through the web interface at: L<https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Record-Serialize>
146              
147             =head2 Source
148              
149             Source is available at
150              
151             https://gitlab.com/djerius/data-record-serialize
152              
153             and may be cloned from
154              
155             https://gitlab.com/djerius/data-record-serialize.git
156              
157             =head1 SEE ALSO
158              
159             Please see those modules/websites for more information related to this module.
160              
161             =over 4
162              
163             =item *
164              
165             L<Data::Record::Serialize|Data::Record::Serialize>
166              
167             =back
168              
169             =head1 AUTHOR
170              
171             Diab Jerius <djerius@cpan.org>
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
176              
177             This is free software, licensed under:
178              
179             The GNU General Public License, Version 3, June 2007
180              
181             =cut