blib/lib/JSON/MaybeXS.pm

filter: Statementstmt 89.8% 62 / 69 Branchbran 79.1% 19 / 24 Conditioncond 45.8% 11 / 24 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 76.3% 113 / 148 CC 24 SCAR 17.8
CC
24
Cov
79%
to_json
10.9
from_json
8.1
is_bool
6.6
CRAP 5.9
↑ Summary
1 package JSON::MaybeXS;
2  
3 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 our $VERSION = '1.004000';
8 6 $VERSION = eval $VERSION;
9  
10 sub _choose_json_module {
11 7 ▶ return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'};
Subroutine _choose_json_module: called CC=6 SCAR=18.0
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"Cpanel/JSON/XS.pm"} 1 6 7
12 6 ▶ return 'JSON::XS' if $INC{'JSON/XS.pm'};
Branch: 1 branch
BranchTrueFalseTotal
if $INC{"JSON/XS.pm"} 0 6 6
13  
14 6 my @err;
15  
16 6 ▶ return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1; };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require Cpanel::JSON::XS; 1 } } 2 4 6
17 4 push @err, "Error loading Cpanel::JSON::XS: $@";
18  
19 4 ▶ return 'JSON::XS' if eval { require JSON::XS; 1; };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require JSON::XS; 1 } } 0 4 4
20 4 push @err, "Error loading JSON::XS: $@";
21  
22 4 ▶ return 'JSON::PP' if eval { require JSON::PP; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require JSON::PP; 1 } } 3 1 4
23 1 push @err, "Error loading JSON::PP: $@";
24  
25 1 die join( "\n", "Couldn't load a JSON module:", @err );
26  
27 }
28  
29 BEGIN {
30 7 ▶ our $JSON_Class = _choose_json_module();
Subroutine BEGIN: called CC=2 SCAR=6.9
31 6 $JSON_Class->import(qw(encode_json decode_json));
32 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 *$_ = $JSON_Class->can($_)
34 6 for qw(true false);
35 }
36  
37 6 our @EXPORT = qw(encode_json decode_json JSON);
38 6 my @EXPORT_ALL = qw(is_bool);
39 6 our @EXPORT_OK = qw(is_bool to_json from_json);
40 6 our %EXPORT_TAGS = ( all => [ @EXPORT, @EXPORT_ALL ],
41 legacy => [ @EXPORT, @EXPORT_OK ],
42 );
43  
44 26 ▶sub JSON () { our $JSON_Class }
Subroutine JSON: called CC=1 SCAR=0.0
Pod documented
45  
46 sub new {
47 5 ▶ shift;
Subroutine new: called CC=3 SCAR=11.0
Pod documented
48 5 ▶ my %args = @_ == 1 ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 1 4 5
49 5 my $new = (our $JSON_Class)->new;
50 5 $new->$_($args{$_}) for keys %args;
51 5 return $new;
52 }
53  
54 6 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 sub is_bool {
57 9 ▶ die 'is_bool is not a method' if $_[1];
Subroutine is_bool: called CC=5 SCAR=18.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 0 9 9
58  
59 9 ▶ Scalar::Util::blessed($_[0])
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) and $_[0]->isa("JSON::XS::Boolean") || $_[0]->isa("Cpanel::JSON::XS::Boolean") || $_[0]->isa("JSON::PP::Boolean") 6 3 9
Condition44%4/9
builtin::blessed($_[0]) && ($_[0]->isa('JSON::XS::Boolean') || $_[0]->isa('Cpanel::JSON::XS::Boolean') || $_[0]->isa('JSON::PP::Boolean'))
!ll&&!rl&&r
306
$_[0]->isa("JSON::XS::Boolean") or $_[0]->isa("Cpanel::JSON::XS::Boolean") or $_[0]->isa("JSON::PP::Boolean")
l!l&&r!l&&!r
060
$_[0]->isa("JSON::XS::Boolean") or $_[0]->isa("Cpanel::JSON::XS::Boolean")
l!l&&r!l&&!r
006
MC/DC25%1/4
builtin::blessed($_[0]) && ($_[0]->isa('JSON::XS::Boolean') || $_[0]->isa('Cpanel::JSON::XS::Boolean') || $_[0]->isa('JSON::PP::Boolean'))
builtin::blessed($_[0]) $_[0]->isa("JSON::XS::Boolean") $_[0]->isa("Cpanel::JSON::XS::Boolean") $_[0]->isa("JSON::PP::Boolean")
Truth table40%2/5
A && B or C or D
A= builtin::blessed($_[0])B= $_[0]->isa("JSON::XS::Boolean")C= $_[0]->isa("Cpanel::JSON::XS::Boolean")D= $_[0]->isa("JSON::PP::Boolean")
ABCDresult
0XXX0
10000
11XX1
101X1
10011
60 and ($_[0]->isa('JSON::XS::Boolean')
61 or $_[0]->isa('Cpanel::JSON::XS::Boolean')
62 or $_[0]->isa('JSON::PP::Boolean'));
63 }
64  
65 # (mostly) CopyPasta from JSON.pm version 2.90
66 6 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
67  
68 sub from_json ($@) {
69 2 ▶ if ( ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/ ) {
Subroutine from_json: called CC=6 SCAR=20.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/) 1 1 2
Condition66%2/3
ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/
l!l&&r!l&&!r
011
MC/DC50%1/2
ref($_[0]) =~ /^JSON/ or $_[0] =~ /^JSON/
ref($_[0]) =~ /^JSON/ $_[0] =~ /^JSON/
Truth table66%2/3
A or B
A= ref($_[0]) =~ /^JSON/B= $_[0] =~ /^JSON/
ABresult
1X1
011
000
70 1 Carp::croak "from_json should not be called as a method.";
71 }
72 1 my $json = JSON()->new;
73  
74 1 ▶ if (@_ == 2 and ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and ref $_[1] eq "HASH") 0 1 1
Condition33%1/3
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
75 0 my $opt = $_[1];
76 0 for my $method (keys %$opt) {
77 0 $json->$method( $opt->{$method} );
78 }
79 }
80  
81 1 return $json->decode( $_[0] );
82 }
83  
84 sub to_json ($@) {
85 2 ▶ if (
Subroutine to_json: called CC=7 SCAR=23.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if (ref($_[0]) =~ /^JSON/ or @_ > 2 and $_[0] =~ /^JSON/) 1 1 2
Condition50%3/6
ref($_[0]) =~ /^JSON/ or @_ > 2 and $_[0] =~ /^JSON/
l!l&&r!l&&!r
020
@_ > 2 and $_[0] =~ /^JSON/
!ll&&!rl&&r
101
MC/DC33%1/3
ref($_[0]) =~ /^JSON/ or @_ > 2 and $_[0] =~ /^JSON/
ref($_[0]) =~ /^JSON/ @_ > 2 $_[0] =~ /^JSON/
Truth table50%2/4
A or B and C
A= ref($_[0]) =~ /^JSON/B= @_ > 2C= $_[0] =~ /^JSON/
ABCresult
1XX1
0111
00X0
0100
86 ref($_[0]) =~ /^JSON/
87 or (@_ > 2 and $_[0] =~ /^JSON/)
88 ) {
89 1 Carp::croak "to_json should not be called as a method.";
90 }
91 1 my $json = JSON()->new;
92  
93 1 ▶ if (@_ == 2 and ref $_[1] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2 and ref $_[1] eq "HASH") 0 1 1
Condition33%1/3
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/2
@_ == 2 and ref $_[1] eq "HASH"
@_ == 2 ref $_[1] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 2B= ref $_[1] eq "HASH"
ABresult
0X0
100
111
94 0 my $opt = $_[1];
95 0 for my $method (keys %$opt) {
96 0 $json->$method( $opt->{$method} );
97 }
98 }
99  
100 1 $json->encode($_[0]);
101 }
102  
103 6 1;
104  
105 =head1 NAME
106  
107 JSON::MaybeXS - Use L<Cpanel::JSON::XS> with a fallback to L<JSON::XS> and L<JSON::PP>
108  
109 =head1 SYNOPSIS
110  
111 use JSON::MaybeXS;
112  
113 my $data_structure = decode_json($json_input);
114  
115 my $json_output = encode_json($data_structure);
116  
117 my $json = JSON()->new;
118  
119 my $json_with_args = JSON::MaybeXS->new(utf8 => 1); # or { utf8 => 1 }
120  
121 =head1 DESCRIPTION
122  
123 This module first checks to see if either L<Cpanel::JSON::XS> or
124 L<JSON::XS> is already loaded, in which case it uses that module. Otherwise
125 it tries to load L<Cpanel::JSON::XS>, then L<JSON::XS>, then L<JSON::PP>
126 in order, and either uses the first module it finds or throws an error.
127  
128 It then exports the C<encode_json> and C<decode_json> functions from the
129 loaded module, along with a C<JSON> constant that returns the class name
130 for calling C<new> on.
131  
132 If you're writing fresh code rather than replacing L<JSON.pm|JSON> usage, you might
133 want to pass options as constructor args rather than calling mutators, so
134 we provide our own C<new> method that supports that.
135  
136 =head1 EXPORTS
137  
138 C<encode_json>, C<decode_json> and C<JSON> are exported by default; C<is_bool>
139 is exported on request.
140  
141 To import only some symbols, specify them on the C<use> line:
142  
143 use JSON::MaybeXS qw(encode_json decode_json is_bool); # functions only
144  
145 use JSON::MaybeXS qw(JSON); # JSON constant only
146  
147 To import all available sensible symbols (C<encode_json>, C<decode_json>, and
148 C<is_bool>), use C<:all>:
149  
150 use JSON::MaybeXS ':all';
151  
152 To import all symbols including those needed by legacy apps that use L<JSON::PP>:
153  
154 use JSON::MaybeXS ':legacy';
155  
156 This imports the C<to_json> and C<from_json> symbols as well as everything in
157 C<:all>. NOTE: This is to support legacy code that makes extensive
158 use of C<to_json> and C<from_json> which you are not yet in a position to
159 refactor. DO NOT use this import tag in new code, in order to avoid
160 the crawling horrors of getting UTF-8 support subtly wrong. See the
161 documentation for L<JSON> for further details.
162  
163 =head2 encode_json
164  
165 This is the C<encode_json> function provided by the selected implementation
166 module, and takes a perl data structure which is serialised to JSON text.
167  
168 my $json_text = encode_json($data_structure);
169  
170 =head2 decode_json
171  
172 This is the C<decode_json> function provided by the selected implementation
173 module, and takes a string of JSON text to deserialise to a perl data structure.
174  
175 my $data_structure = decode_json($json_text);
176  
177 =head2 to_json, from_json
178  
179 See L<JSON> for details. These are included to support legacy code
180 B<only>.
181  
182 =head2 JSON
183  
184 The C<JSON> constant returns the selected implementation module's name for
185 use as a class name - so:
186  
187 my $json_obj = JSON()->new; # returns a Cpanel::JSON::XS or JSON::PP object
188  
189 and that object can then be used normally:
190  
191 my $data_structure = $json_obj->decode($json_text); # etc.
192  
193 The use of parentheses here is optional, and only used as a hint to the reader
194 that this use of C<JSON> is a I<subroutine> call, I<not> a class name.
195  
196 =head2 is_bool
197  
198 $is_boolean = is_bool($scalar)
199  
200 Returns true if the passed scalar represents either C<true> or
201 C<false>, two constants that act like C<1> and C<0>, respectively
202 and are used to represent JSON C<true> and C<false> values in Perl.
203  
204 Since this is a bare sub in the various backend classes, it cannot be called as
205 a class method like the other interfaces; it must be called as a function, with
206 no invocant. It supports the representation used in all JSON backends.
207  
208 =head1 CONSTRUCTOR
209  
210 =head2 new
211  
212 With L<JSON::PP>, L<JSON::XS> and L<Cpanel::JSON::XS> you are required to call
213 mutators to set options, such as:
214  
215 my $json = $class->new->utf8(1)->pretty(1);
216  
217 Since this is a trifle irritating and noticeably un-perlish, we also offer:
218  
219 my $json = JSON::MaybeXS->new(utf8 => 1, pretty => 1);
220  
221 which works equivalently to the above (and in the usual tradition will accept
222 a hashref instead of a hash, should you so desire).
223  
224 The resulting object is blessed into the underlying backend, which offers (at
225 least) the methods C<encode> and C<decode>.
226  
227 =head1 BOOLEANS
228  
229 To include JSON-aware booleans (C<true>, C<false>) in your data, just do:
230  
231 use JSON::MaybeXS;
232 my $true = JSON()->true;
233 my $false = JSON()->false;
234  
235 The booleans are also available as subs or methods on JSON::MaybeXS.
236  
237 use JSON::MaybeXS ();
238 my $true = JSON::MaybeXS::true;
239 my $true = JSON::MaybeXS->true;
240 my $false = JSON::MaybeXS::false;
241 my $false = JSON::MaybeXS->false;
242  
243 =head1 CONVERTING FROM JSON::Any
244  
245 L<JSON::Any> used to be the favoured compatibility layer above the various
246 JSON backends, but over time has grown a lot of extra code to deal with legacy
247 backends (e.g. L<JSON::Syck>) that are no longer needed. This is a rough guide of translating such code:
248  
249 Change code from:
250  
251 use JSON::Any;
252 my $json = JSON::Any->new->objToJson($data); # or to_json($data), or Dump($data)
253  
254 to:
255  
256 use JSON::MaybeXS;
257 my $json = encode_json($data);
258  
259  
260 Change code from:
261  
262 use JSON::Any;
263 my $data = JSON::Any->new->jsonToObj($json); # or from_json($json), or Load($json)
264  
265 to:
266  
267 use JSON::MaybeXS;
268 my $json = decode_json($data);
269  
270 =head1 CAVEATS
271  
272 The C<new()> method in this module is technically a factory, not a
273 constructor, because the objects it returns will I<NOT> be blessed into the
274 C<JSON::MaybeXS> class.
275  
276 If you are using an object returned by this module as a Moo(se) attribute,
277 this type constraint code:
278  
279 is 'json' => ( isa => 'JSON::MaybeXS' );
280  
281 will I<NOT> do what you expect. Instead, either rely on the C<JSON> class
282 constant described above, as so:
283  
284 is 'json' => ( isa => JSON::MaybeXS::JSON() );
285  
286 Alternatively, you can use duck typing:
287  
288 use Moose::Util::TypeConstraints 'duck_type';
289 is 'json' => ( isa => Object , duck_type([qw/ encode decode /]));
290  
291 =head1 INSTALLATION
292  
293 At installation time, F<Makefile.PL> will attempt to determine if you have a
294 working compiler available, and therefore whether you are able to run XS code.
295 If so, L<Cpanel::JSON::XS> will be added to the prerequisite list, unless
296 L<JSON::XS> is already installed at a high enough version. L<JSON::XS> may
297 also be upgraded to fix any incompatibility issues.
298  
299 Because running XS code is not mandatory and L<JSON::PP> (which is in perl
300 core) is used as a fallback backend, this module is safe to be used in a suite
301 of code that is fatpacked or installed into a restricted-resource environment.
302  
303 You can also prevent any XS dependencies from being installed by setting
304 C<PUREPERL_ONLY=1> in F<Makefile.PL> options (or in the C<PERL_MM_OPT>
305 environment variable), or using the C<--pp> or C<--pureperl> flags with the
306 L<cpanminus client|cpanm>.
307  
308 =head1 AUTHOR
309  
310 mst - Matt S. Trout (cpan:MSTROUT) <mst@shadowcat.co.uk>
311  
312 =head1 CONTRIBUTORS
313  
314 =over 4
315  
316 =item * Clinton Gormley <drtech@cpan.org>
317  
318 =item * Karen Etheridge <ether@cpan.org>
319  
320 =item * Kieren Diment <diment@gmail.com>
321  
322 =back
323  
324 =head1 COPYRIGHT
325  
326 Copyright (c) 2013 the C<JSON::MaybeXS> L</AUTHOR> and L</CONTRIBUTORS>
327 as listed above.
328  
329 =head1 LICENSE
330  
331 This library is free software and may be distributed under the same terms
332 as perl itself.
333  
334 =cut
↑ Summary