in reply to Re^9: JSON::XS and blessings
in thread JSON::XS and blessings

Running on both virtual server apache servers,

my $json = '{"a":true}'; $d = decode_json($json); printf "Dumper: %s<br>\n",Dumper($d);

prints the same: Dumper: $VAR1 = { 'a' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ) };

Yet encode_json($d) fails on one and works correctly on the other.

Replies are listed 'Best First'.
Re^11: JSON::XS and blessings (incompat)
by tye (Sage) on Oct 16, 2015 at 15:01 UTC

    In checking what I thought were reasonable assumptions, I found that JSON::PP's handling of booleans and JSON::XS's handling of booleans are simply incompatible.

    use JSON::XS(); use JSON::PP(); JSON::XS->new()->encode( JSON::PP->new()->decode('[true]') ); __END__ encountered object '1', but neither allow_blessed nor convert_blessed +settings are enabled

    Note that even enabling convert_blessed() won't help as JSON::PP generates booleans that don't even have a TO_JSON() method. [And enabling allow_blessed() would just turn true/false into null.]

    Both modules could use some work on dealing with booleans.

    - tye        

      Interesting!

      As for me, I'd be happy to have a boolAsInt method, such that

      JSON->new()->boolAsInt(1)->decode('[true]')

      would return [1].