From: Clinton Gormley Date: Mon, 21 Apr 2014 11:50:23 +0000 (-0700) Subject: apply with_JSON_XS_v2.patch from RT#94892 X-Git-Tag: v1.002000~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=16205c4a0e5da3d749a0f405bcbc6b11a5b2dec5;p=p5sagit%2FJSON-MaybeXS.git apply with_JSON_XS_v2.patch from RT#94892 --- diff --git a/lib/JSON/MaybeXS.pm b/lib/JSON/MaybeXS.pm index b0b2f59..ca10676 100644 --- a/lib/JSON/MaybeXS.pm +++ b/lib/JSON/MaybeXS.pm @@ -6,25 +6,28 @@ use base qw(Exporter); 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 @@ sub new { =head1 NAME -JSON::MaybeXS - use L with a fallback to L +JSON::MaybeXS - use L with a fallback to L and L =head1 SYNOPSIS @@ -59,9 +62,10 @@ JSON::MaybeXS - use L with a fallback to L =head1 DESCRIPTION -This module tries to load L, and if that fails instead -tries to load L. If neither is available, an exception will be -thrown. +This module first checks to see if either L or +L is already loaded, in which case it uses that module. Otherwise +it tries to load L, then L, then L +in order, and either uses the first module it finds or throws an error. It then exports the C and C functions from the loaded module, along with a C constant that returns the class name @@ -110,8 +114,8 @@ and that object can then be used normally: =head2 new -With L and L you are required to call mutators -to set options, i.e. +With L, L and L you are required to call +mutators to set options, i.e. my $json = $class->new->utf8(1)->pretty(1); diff --git a/t/cpanel.t b/t/cpanel.t new file mode 100644 index 0000000..5ffa55f --- /dev/null +++ b/t/cpanel.t @@ -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 --git a/t/neither.t b/t/none.t similarity index 68% rename from t/neither.t rename to t/none.t index 7070daf..4b8aae5 100644 --- a/t/neither.t +++ b/t/none.t @@ -1,6 +1,7 @@ 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; @@ -9,8 +10,8 @@ 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' + $@, qr{Cpanel/JSON/XS.pm did not.*JSON/XS.pm did not.*JSON/PP.pm did not}s, + 'All errors reported' ); done_testing; diff --git a/t/pp.t b/t/pp.t index b52f27e..f75fa20 100644 --- a/t/pp.t +++ b/t/pp.t @@ -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 --git a/t/preload_cpanel.t b/t/preload_cpanel.t new file mode 100644 index 0000000..2c9243a --- /dev/null +++ b/t/preload_cpanel.t @@ -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 --git a/t/preload_xs.t b/t/preload_xs.t new file mode 100644 index 0000000..aced987 --- /dev/null +++ b/t/preload_xs.t @@ -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 --git a/t/xs.t b/t/xs.t index 2c9243a..51b56dd 100644 --- a/t/xs.t +++ b/t/xs.t @@ -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;