Subject: | Add JSON::XS support to MaybeXS |
This patch adds fallback support for JSON::XS. It first checks to see whether Cpanel::JSON::XS or JSON::XS is already loaded and if so, uses that module. Failing that, it tries to load Cpanel::JSON::XS, then JSON::XS then JSON::PP.
Currently this module requires Cpanel::JSON::XS and JSON::PP. This should be changed to requiring JSON::PP and recommending Cpanel::JSON::XS and JSON::XS instead (imo).
Subject: | with_JSON_XS.patch |
diff -ruN JSON-MaybeXS-1.001000/lib/JSON/MaybeXS.pm JSON-MaybeXS-with-JSON-XS/lib/JSON/MaybeXS.pm
--- JSON-MaybeXS-1.001000/lib/JSON/MaybeXS.pm 2013-12-11 02:56:45.000000000 +0100
+++ JSON-MaybeXS-with-JSON-XS/lib/JSON/MaybeXS.pm 2014-04-20 13:10:16.000000000 +0200
@@ -6,25 +6,28 @@
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);
@@ -43,7 +46,7 @@
=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
@@ -59,9 +62,10 @@
=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
@@ -110,8 +114,8 @@
=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);
diff -ruN JSON-MaybeXS-1.001000/t/cpanel.t JSON-MaybeXS-with-JSON-XS/t/cpanel.t
--- JSON-MaybeXS-1.001000/t/cpanel.t 1970-01-01 01:00:00.000000000 +0100
+++ JSON-MaybeXS-with-JSON-XS/t/cpanel.t 2014-04-20 13:28:10.000000000 +0200
@@ -0,0 +1,24 @@
+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;
diff -ruN JSON-MaybeXS-1.001000/t/neither.t JSON-MaybeXS-with-JSON-XS/t/neither.t
--- JSON-MaybeXS-1.001000/t/neither.t 2013-12-11 02:44:43.000000000 +0100
+++ JSON-MaybeXS-with-JSON-XS/t/neither.t 1970-01-01 01:00:00.000000000 +0100
@@ -1,16 +0,0 @@
-use strict;
-use warnings FATAL => 'all';
-use Test::Without::Module 'Cpanel::JSON::XS';
-use Test::Without::Module 'JSON::PP';
-use Test::More;
-
-ok(!eval { require JSON::MaybeXS; 1 }, 'Class failed to load');
-
-# 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'
-);
-
-done_testing;
diff -ruN JSON-MaybeXS-1.001000/t/none.t JSON-MaybeXS-with-JSON-XS/t/none.t
--- JSON-MaybeXS-1.001000/t/none.t 1970-01-01 01:00:00.000000000 +0100
+++ JSON-MaybeXS-with-JSON-XS/t/none.t 2014-04-20 13:30:50.000000000 +0200
@@ -0,0 +1,17 @@
+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;
+
+ok(!eval { require JSON::MaybeXS; 1 }, 'Class failed to load');
+
+# Test::Without::Module always causes 'did not return a true value' errors
+
+like(
+ $@, qr{Cpanel/JSON/XS.pm did not.*JSON/XS.pm did not.*JSON/PP.pm did not}s,
+ 'All errors reported'
+);
+
+done_testing;
diff -ruN JSON-MaybeXS-1.001000/t/pp.t JSON-MaybeXS-with-JSON-XS/t/pp.t
--- JSON-MaybeXS-1.001000/t/pp.t 2013-12-11 02:44:43.000000000 +0100
+++ JSON-MaybeXS-with-JSON-XS/t/pp.t 2014-04-20 13:19:29.000000000 +0200
@@ -1,6 +1,6 @@
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;
diff -ruN JSON-MaybeXS-1.001000/t/preload_cpanel.t JSON-MaybeXS-with-JSON-XS/t/preload_cpanel.t
--- JSON-MaybeXS-1.001000/t/preload_cpanel.t 1970-01-01 01:00:00.000000000 +0100
+++ JSON-MaybeXS-with-JSON-XS/t/preload_cpanel.t 2013-12-11 02:44:43.000000000 +0100
@@ -0,0 +1,19 @@
+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;
diff -ruN JSON-MaybeXS-1.001000/t/preload_xs.t JSON-MaybeXS-with-JSON-XS/t/preload_xs.t
--- JSON-MaybeXS-1.001000/t/preload_xs.t 1970-01-01 01:00:00.000000000 +0100
+++ JSON-MaybeXS-with-JSON-XS/t/preload_xs.t 2014-04-20 13:27:56.000000000 +0200
@@ -0,0 +1,12 @@
+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;
diff -ruN JSON-MaybeXS-1.001000/t/xs.t JSON-MaybeXS-with-JSON-XS/t/xs.t
--- JSON-MaybeXS-1.001000/t/xs.t 2013-12-11 02:44:43.000000000 +0100
+++ JSON-MaybeXS-with-JSON-XS/t/xs.t 2014-04-20 13:24:48.000000000 +0200
@@ -1,19 +1,19 @@
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;