in reply to Why is Dumper showing decoded JSON booleans like this?

Perl doesn't have a boolean type. In order to be able to do round trips, JSON uses an overloaded object to represent booleans.
  • Comment on Re: Why is Dumper showing decoded JSON booleans like this?

Replies are listed 'Best First'.
Re^2: Why is Dumper showing decoded JSON booleans like this?
by nleaman (Initiate) on Mar 03, 2010 at 19:42 UTC
    Okay. That makes sense. I guess my problem then becomes something different which is when using PHP::Serialization to serialize the decoded json text:
    #!/usr/bin/perl -w use strict; use Data::Dumper; use JSON; use PHP::Serialization qw(serialize unserialize); my $json_text ='{ "boolean": true }'; my $json_text_decoded = decode_json $json_text; serialize($json_text_decoded);

    OUTPUT:

    Not a HASH reference at lib/PHP/Serialization.pm line 454.

    It dies when working on the object data type. Is there a straightforward way to convert the boolean object to a literal string?

      You'll have to traverse the returned data structure, using JSON::is_bool to check for boolean nodes, replacing boolean nodes with the values of your choice.

      If PHP::Serialization supported custom object serializers, you could just write one of those for JSON::PP::Boolean, but it doesn't appear to be the case.

        Thank you for your guidance. This is exactly the help I needed.