File Coverage

blib/lib/Data/Record/Serialize/Encode/json.pm
Criterion Covered Total %
statement 23 26 88.4
branch 2 6 33.3
condition n/a
subroutine 9 9 100.0
pod 1 2 50.0
total 35 43 81.4


line stmt bran cond sub pod time code
1              
2             # ABSTRACT: encoded a record as JSON
3              
4             use strict;
5 1     1   531 use warnings;
  1         2  
  1         26  
6 1     1   4  
  1         2  
  1         35  
7             use Data::Record::Serialize::Error { errors => [ 'json_backend' ] }, -all;
8 1     1   5  
  1         2  
  1         10  
9             use Moo::Role;
10 1     1   534  
  1         11189  
  1         6  
11             our $VERSION = '1.03'; # TRIAL
12              
13              
14             BEGIN {
15             my $Cpanel_JSON_XS_VERSION = 3.0236;
16 1     1   611  
17             # Module::Version doesn't load the code, so avoids loading
18             # Cpanel::JSON::XS's version of JSON::PP::Boolean which prevents
19             # loading the version provided by JSON::PP if Cpanel::JSON::XS is
20             # too old. Symbol::delete_package could be used to remove
21             # Cpanel::JSON::XS's version, but it's better not to load it in
22             # the first place.
23             require Module::Version;
24 1         375 if ( Module::Version::get_version( 'Cpanel::JSON::XS' ) >= $Cpanel_JSON_XS_VERSION ) {
25 1 50       87841 require Cpanel::JSON::XS;
    0          
26 1         1259 *encode_json = \&Cpanel::JSON::XS::encode_json;
27 1         3124 }
28             elsif ( eval { require JSON::PP } ) {
29 0         0 *encode_json = \&JSON::PP::encode_json;
30 0         0 }
31             else {
32             error( 'json_backend', "can't find either Cpanel::JSON::XS (>= $Cpanel_JSON_XS_VERSION) or JSON::PP. Please install one of them." );
33 0         0 }
34             };
35              
36             use namespace::clean;
37 1     1   8  
  1         1  
  1         9  
38             has '+numify' => ( is => 'ro', default => 1 );
39             has '+stringify' => ( is => 'ro', default => 1 );
40              
41              
42 2     2   19  
43              
44              
45              
46              
47              
48              
49              
50              
51              
52 1 50   1 1 38  
53              
54              
55              
56              
57             with 'Data::Record::Serialize::Role::Encode';
58              
59 2     2 0 89 1;
60              
61             #
62             # This file is part of Data-Record-Serialize
63             #
64             # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
65             #
66             # This is free software, licensed under:
67             #
68             # The GNU General Public License, Version 3, June 2007
69             #
70              
71              
72             =pod
73              
74             =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory
75              
76             =head1 NAME
77              
78             Data::Record::Serialize::Encode::json - encoded a record as JSON
79              
80             =head1 VERSION
81              
82             version 1.03
83              
84             =head1 SYNOPSIS
85              
86             use Data::Record::Serialize;
87              
88             my $s = Data::Record::Serialize->new( encode => 'json', ... );
89              
90             $s->send( \%record );
91              
92             =head1 DESCRIPTION
93              
94             B<Data::Record::Serialize::Encode::json> encodes a record as JSON.
95              
96             If a field's type is C<N> or C<I>, it will be properly encoded by JSON
97             as a number. Field's with type C<S> are force to be strings.
98              
99             Boolean fields (type C<B>) are transformed into values recognized by
100             the back-end encoder.
101              
102             The output consists of I<concatenated> JSON objects, and is mostly easily
103             read by an incremental decoder, e.g.
104              
105             use JSON::MaybeXS;
106              
107             @data = JSON->new->incr_parse( $json );
108              
109             It performs the L<Data::Record::Serialize::Role::Encode> role.
110              
111             =head1 METHODS
112              
113             =head2 to_bool
114              
115             $bool = $self->to_bool( $truthy );
116              
117             Convert a truthy value to something that the JSON encoders will recognize as a boolean.
118              
119             =for Pod::Coverage encode
120              
121             =for Pod::Coverage numify
122             stringify
123             encode_json
124              
125             =head1 INTERFACE
126              
127             There are no additional attributes which may be passed to
128             L<< Data::Record::Serialize::new|Data::Record::Serialize/new >>.
129              
130             =head1 SUPPORT
131              
132             =head2 Bugs
133              
134             Please report any bugs or feature requests to bug-data-record-serialize@rt.cpan.org or through the web interface at: https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Record-Serialize
135              
136             =head2 Source
137              
138             Source is available at
139              
140             https://gitlab.com/djerius/data-record-serialize
141              
142             and may be cloned from
143              
144             https://gitlab.com/djerius/data-record-serialize.git
145              
146             =head1 SEE ALSO
147              
148             Please see those modules/websites for more information related to this module.
149              
150             =over 4
151              
152             =item *
153              
154             L<Data::Record::Serialize|Data::Record::Serialize>
155              
156             =back
157              
158             =head1 AUTHOR
159              
160             Diab Jerius <djerius@cpan.org>
161              
162             =head1 COPYRIGHT AND LICENSE
163              
164             This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
165              
166             This is free software, licensed under:
167              
168             The GNU General Public License, Version 3, June 2007
169              
170             =cut