Permalink
Browse files

JSON now uses JSON::PP or JSON::backportPP as the backend module

  • Loading branch information...
1 parent 8a4afb9 commit c58e724c07fd1d8c9864930789d3355c3f7a2a93 @makamaka committed Nov 27, 2010
Showing with 52 additions and 21 deletions.
  1. +36 −9 lib/JSON.pm
  2. +9 −8 lib/JSON/backportPP.pm
  3. +1 −1 lib/JSON/backportPP/Boolean.pm
  4. +2 −1 lib/JSON/backportPP5005.pm
  5. +2 −1 lib/JSON/backportPP56.pm
  6. +2 −1 lib/JSON/backportPP58.pm
View
@@ -7,12 +7,15 @@ use base qw(Exporter);
@JSON::EXPORT = qw(from_json to_json jsonToObj objToJson encode_json decode_json);
BEGIN {
- $JSON::VERSION = '2.27';
+ $JSON::VERSION = '2.49_01';
$JSON::DEBUG = 0 unless (defined $JSON::DEBUG);
+ $JSON::DEBUG = $ENV{ PERL_JSON_DEBUG } if exists $ENV{ PERL_JSON_DEBUG };
}
my $Module_XS = 'JSON::XS';
my $Module_PP = 'JSON::PP';
+my $Module_bp = 'JSON::backportPP'; # included in JSON distribution
+my $PP_Version = '2.27008';
my $XS_Version = '2.27';
@@ -42,6 +45,7 @@ my $_INSTALL_DONT_DIE = 1; # When _load_xs fails to load XS, don't die.
my $_INSTALL_ONLY = 2; # Don't call _set_methods()
my $_ALLOW_UNSUPPORTED = 0;
my $_UNIV_CONV_BLESSED = 0;
+my $_USSING_bpPP = 0;
# Check the environment variable to decide worker module.
@@ -60,6 +64,10 @@ unless ($JSON::Backend) {
elsif ($backend eq '2' or $backend eq 'JSON::XS') {
_load_xs();
}
+ elsif ($backend eq 'JSON::backportPP') {
+ $_USSING_bpPP = 1;
+ _load_pp();
+ }
else {
Carp::croak "The value of environmental variable 'PERL_JSON_BACKEND' is invalid.";
}
@@ -184,7 +192,7 @@ sub is_xs {
sub is_pp {
- return $_[0]->module eq $Module_PP;
+ return not $_[0]->xs;
}
@@ -264,26 +272,44 @@ sub _load_xs {
sub _load_pp {
my $opt = shift;
+ my $backend = $_USSING_bpPP ? $Module_bp : $Module_PP;
- $JSON::DEBUG and Carp::carp "Load $Module_PP.";
+ $JSON::DEBUG and Carp::carp "Load $backend.";
# if called after install module, overload is disable.... why?
JSON::Boolean::_overrride_overload($Module_XS);
- JSON::Boolean::_overrride_overload($Module_PP);
+# JSON::Boolean::_overrride_overload($Module_PP);
+ JSON::Boolean::_overrride_overload($backend);
+
+ if ( $_USSING_bpPP ) {
+ eval qq| require $backend |;
+ }
+ else {
+ eval qq| use $backend $PP_Version () |;
+ }
- eval qq| require $Module_PP |;
if ($@) {
- Carp::croak $@;
+ if ( $backend eq $Module_PP ) {
+ $JSON::DEBUG and Carp::carp "Can't load $Module_PP ($@), so try to load $Module_bp";
+ $_USSING_bpPP++;
+ $backend = $Module_bp;
+ JSON::Boolean::_overrride_overload($backend);
+ local $^W; # if PP installed but invalid version, backportPP redifines methods.
+ eval qq| require $Module_bp |;
+ }
+ Carp::croak $@ if $@;
}
unless (defined $opt and $opt & $_INSTALL_ONLY) {
- _set_module( $JSON::Backend = $Module_PP );
+ _set_module( $JSON::Backend = $Module_PP ); # even if backportPP, set $Backend with 'JSON::PP'
JSON::Backend::PP->init;
}
};
sub _set_module {
+ return if defined $JSON::true;
+
my $module = shift;
local $^W;
@@ -352,7 +378,7 @@ package JSON::Backend::PP;
sub init {
local $^W;
- no strict qw(refs);
+ no strict qw(refs); # this routine may be called after JSON::Backend::XS init was called.
*{"JSON::decode_json"} = \&{"JSON::PP::decode_json"};
*{"JSON::encode_json"} = \&{"JSON::PP::encode_json"};
*{"JSON::PP::is_xs"} = sub { 0 };
@@ -487,7 +513,8 @@ sub _make_unsupported_method {
sub _set_for_pp {
- require JSON::PP;
+ JSON::_load_pp( $_INSTALL_ONLY );
+
my $type = shift;
my $pp = new JSON::PP;
my $prop = $_[0]->property;
View
@@ -1,4 +1,5 @@
-package JSON::PP;
+package # This is JSON::backportPP
+ JSON::PP;
# JSON-2.0
@@ -55,9 +56,9 @@ BEGIN {
# Perl version check, Unicode handling is enable?
# Helper module sets @JSON::PP::_properties.
- my $helper = $] >= 5.008 ? 'JSON::PP58'
- : $] >= 5.006 ? 'JSON::PP56'
- : 'JSON::PP5005'
+ my $helper = $] >= 5.008 ? 'JSON::backportPP58'
+ : $] >= 5.006 ? 'JSON::backportPP56'
+ : 'JSON::backportPP5005'
;
eval qq| require $helper |;
@@ -1317,8 +1318,8 @@ BEGIN {
# shamely copied and modified from JSON::XS code.
-$JSON::PP::true = do { bless \(my $dummy = 1), "JSON::PP::Boolean" };
-$JSON::PP::false = do { bless \(my $dummy = 0), "JSON::PP::Boolean" };
+$JSON::PP::true = do { bless \(my $dummy = 1), "JSON::backportPP::Boolean" };
+$JSON::PP::false = do { bless \(my $dummy = 0), "JSON::backportPP::Boolean" };
sub is_bool { defined $_[0] and UNIVERSAL::isa($_[0], "JSON::PP::Boolean"); }
@@ -1328,9 +1329,9 @@ sub null { undef; }
###############################
-package JSON::PP::Boolean;
-
+package JSON::backportPP::Boolean;
+@JSON::backportPP::Boolean::ISA = ('JSON::PP::Boolean');
use overload (
"0+" => sub { ${$_[0]} },
"++" => sub { $_[0] = ${$_[0]} + 1 },
@@ -13,7 +13,7 @@ L<JSON::PP> for more info about this class.
=cut
-use JSON::PP ();
+use JSON::backportPP ();
use strict;
1;
@@ -1,4 +1,5 @@
-package JSON::PP5005;
+package # This is JSON::backportPP
+ JSON::backportPP5005;
use 5.005;
use strict;
View
@@ -1,4 +1,5 @@
-package JSON::PP56;
+package # This is JSON::backportPP
+ JSON::backportPP56;
use 5.006;
use strict;
View
@@ -1,4 +1,5 @@
-package JSON::PP58;
+package # This is JSON::backportPP
+ JSON::PP58;
use 5.008;
use strict;

0 comments on commit c58e724

Please sign in to comment.