Handle new JSON::XS boolean representations user/jeff/fix_json_xs_booleans_option2
authorJeff Godin <jgodin@tadl.org>
Tue, 3 Dec 2013 11:44:54 +0000 (06:44 -0500)
committerJeff Godin <jgodin@tadl.org>
Tue, 3 Dec 2013 11:44:54 +0000 (06:44 -0500)
JSON::XS version 3 and up represents boolean values differently.
This is one option for handling the new representation.

Signed-off-by: Jeff Godin <jgodin@tadl.org>
src/perl/lib/OpenSRF/Utils/JSON.pm
src/perl/t/09-Utils-JSON.t

index 58a7a02..eff7803 100644 (file)
@@ -160,13 +160,13 @@ sub JSONObject2Perl {
         # is a hash, but no class marker; simply revivify innards
         for my $k (keys %$obj) {
             $obj->{$k} = $pkg->JSONObject2Perl($obj->{$k})
-              unless ref $obj->{$k} eq 'JSON::XS::Boolean';
+              unless ref $obj->{$k} eq 'JSON::XS::Boolean' || eval {Types::Serialiser::is_bool( ref $obj->{$k} )};
         }
     } elsif ( ref $obj eq 'ARRAY' ) {
         # not a hash; an array. revivify.
         for my $i (0..scalar(@$obj) - 1) {
             $obj->[$i] = $pkg->JSONObject2Perl($obj->[$i])
-              unless (ref $obj->[$i] eq 'JSON::XS::Boolean');
+              unless (ref $obj->[$i] eq 'JSON::XS::Boolean' || eval {Types::Serialiser::is_bool( ref $obj->[$i] )});
               # FIXME? This does nothing except leave any Booleans in
               # place, without recursively calling this sub on
               # them. I'm not sure if that's what's supposed to
@@ -206,7 +206,7 @@ sub perl2JSONObject {
     my $ref = ref $obj;
 
     return $obj unless $ref;
-    return $obj if $ref eq 'JSON::XS::Boolean';
+    return $obj if $ref eq 'JSON::XS::Boolean' || eval{ Types::Serialiser::is_bool($ref) };
 
     my $jsonobj;
 
index 7b5908b..af37b67 100644 (file)
@@ -76,8 +76,8 @@ is (OpenSRF::Utils::JSON->perl2JSONObject(),      undef, "Returns argument unles
 is (OpenSRF::Utils::JSON->perl2JSONObject(3),     3,     "Returns argument unless it's a ref");
 is (OpenSRF::Utils::JSON->perl2JSONObject('foo'), 'foo', "Returns argument unless it's a ref");
 
-is (ref OpenSRF::Utils::JSON->true, 'JSON::XS::Boolean');
-is (OpenSRF::Utils::JSON->perl2JSONObject(OpenSRF::Utils::JSON->true), '1', "Returns argument if it's a JSON::XS::Boolean");
+ok (ref OpenSRF::Utils::JSON->true eq 'JSON::XS::Boolean' || eval{Types::Serialiser::is_bool(ref OpenSRF::Utils::JSON->true)});
+is (OpenSRF::Utils::JSON->perl2JSONObject(OpenSRF::Utils::JSON->true), '1', "Returns argument if it's a JSON::XS::Boolean or Types::Serialiser->is_bool");
 
 my $hashref = { foo => 'bar' };
 is (UNIVERSAL::isa($hashref,'HASH'), 1);