our $VERSION = '1.001000';
-BEGIN {
- our $JSON_Class;
+sub _choose_json_module {
+ return 'Cpanel::JSON::XS' if $INC{'Cpanel/JSON/XS.pm'};
+ return 'JSON::XS' if $INC{'JSON/XS.pm'};
- our @err;
+ my @err;
- if (eval { require Cpanel::JSON::XS; 1; }) {
- $JSON_Class = 'Cpanel::JSON::XS';
- } else {
+ return 'Cpanel::JSON::XS' if eval { require Cpanel::JSON::XS; 1; };
push @err, "Error loading Cpanel::JSON::XS: $@";
- if (eval { require JSON::PP; 1; }) {
- $JSON_Class = 'JSON::PP';
- } else {
- push @err, "Error loading JSON::PP: $@";
- }
- }
- unless ($JSON_Class) {
- die join("\n", "Couldn't load a JSON module:", @err);
- }
- $JSON_Class->import(qw(encode_json decode_json));
+
+ return 'JSON::XS' if eval { require JSON::XS; 1; };
+ push @err, "Error loading JSON::XS: $@";
+
+ return 'JSON::PP' if eval { require JSON::PP; 1 };
+ push @err, "Error loading JSON::PP: $@";
+
+ die join( "\n", "Couldn't load a JSON module:", @err );
+
+}
+
+BEGIN {
+ our $JSON_Class = _choose_json_module();
+ $JSON_Class->import(qw(encode_json decode_json));
}
our @EXPORT = qw(encode_json decode_json JSON);
=head1 NAME
-JSON::MaybeXS - use L<Cpanel::JSON::XS> with a fallback to L<JSON::PP>
+JSON::MaybeXS - use L<Cpanel::JSON::XS> with a fallback to L<JSON::XS> and L<JSON::PP>
=head1 SYNOPSIS
=head1 DESCRIPTION
-This module tries to load L<Cpanel::JSON::XS>, and if that fails instead
-tries to load L<JSON::PP>. If neither is available, an exception will be
-thrown.
+This module first checks to see if either L<Cpanel::JSON::XS> or
+L<JSON::XS> is already loaded, in which case it uses that module. Otherwise
+it tries to load L<Cpanel::JSON::XS>, then L<JSON::XS>, then L<JSON::PP>
+in order, and either uses the first module it finds or throws an error.
It then exports the C<encode_json> and C<decode_json> functions from the
loaded module, along with a C<JSON> constant that returns the class name
=head2 new
-With L<JSON::PP> and L<Cpanel::JSON::XS> you are required to call mutators
-to set options, i.e.
+With L<JSON::PP>, L<JSON::XS> and L<Cpanel::JSON::XS> you are required to call
+mutators to set options, i.e.
my $json = $class->new->utf8(1)->pretty(1);
--- /dev/null
+use strict;
+use warnings FATAL => 'all';
+use Test::More;
+use JSON::MaybeXS;
+
+unless ( eval { require Cpanel::JSON::XS; 1 } ) {
+ plan skip_all => 'Cpanel::JSON::XS not installed';
+ done_testing;
+ exit;
+}
+
+is( JSON, 'Cpanel::JSON::XS', 'Correct JSON class' );
+
+is( \&encode_json,
+ \&Cpanel::JSON::XS::encode_json,
+ 'Correct encode_json function'
+);
+
+is( \&decode_json,
+ \&Cpanel::JSON::XS::decode_json,
+ 'Correct encode_json function'
+);
+
+done_testing;
use strict;
use warnings FATAL => 'all';
use Test::Without::Module 'Cpanel::JSON::XS';
+use Test::Without::Module 'JSON::XS';
use Test::Without::Module 'JSON::PP';
use Test::More;
# Test::Without::Module always causes 'did not return a true value' errors
like(
- $@, qr{Cpanel/JSON/XS.pm did not.*JSON/PP.pm did not}s,
- 'Both errors reported'
+ $@, qr{Cpanel/JSON/XS.pm did not.*JSON/XS.pm did not.*JSON/PP.pm did not}s,
+ 'All errors reported'
);
done_testing;
use strict;
use warnings FATAL => 'all';
-use Test::Without::Module 'Cpanel::JSON::XS';
+use Test::Without::Module 'Cpanel::JSON::XS', 'JSON::XS';
use if !do { require JSON::PP; 1; }, 'Test::More', skip_all => 'No JSON::PP';
use Test::More;
use JSON::MaybeXS;
--- /dev/null
+use strict;
+use warnings FATAL => 'all';
+use if !do { require Cpanel::JSON::XS; 1; }, 'Test::More', skip_all => 'No Cpanel::JSON::XS';
+use Test::More;
+use JSON::MaybeXS;
+
+is(JSON, 'Cpanel::JSON::XS', 'Correct JSON class');
+
+is(
+ \&encode_json, \&Cpanel::JSON::XS::encode_json,
+ 'Correct encode_json function'
+);
+
+is(
+ \&decode_json, \&Cpanel::JSON::XS::decode_json,
+ 'Correct encode_json function'
+);
+
+done_testing;
--- /dev/null
+use strict;
+use warnings FATAL => 'all';
+use if !do { require JSON::XS; 1; }, 'Test::More', skip_all => 'No JSON::XS';
+use Test::More;
+use JSON::MaybeXS;
+
+is( JSON, 'JSON::XS', 'Correct JSON class' );
+
+is( \&encode_json, \&JSON::XS::encode_json, 'Correct encode_json function' );
+is( \&decode_json, \&JSON::XS::decode_json, 'Correct encode_json function' );
+
+done_testing;
use strict;
use warnings FATAL => 'all';
-use if !do { require Cpanel::JSON::XS; 1; }, 'Test::More', skip_all => 'No Cpanel::JSON::XS';
+
+use Test::Without::Module 'Cpanel::JSON::XS';
use Test::More;
use JSON::MaybeXS;
-is(JSON, 'Cpanel::JSON::XS', 'Correct JSON class');
+unless ( eval { require JSON::XS; 1 } ) {
+ plan skip_all => 'JSON::XS not installed';
+ done_testing;
+ exit;
+}
-is(
- \&encode_json, \&Cpanel::JSON::XS::encode_json,
- 'Correct encode_json function'
-);
+is( JSON, 'JSON::XS', 'Correct JSON class' );
-is(
- \&decode_json, \&Cpanel::JSON::XS::decode_json,
- 'Correct encode_json function'
-);
+is( \&encode_json, \&JSON::XS::encode_json, 'Correct encode_json function' );
+is( \&decode_json, \&JSON::XS::decode_json, 'Correct encode_json function' );
done_testing;