in reply to Re^2: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?
in thread How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?

Are you using version 4+?

  • Comment on Re^3: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?

Replies are listed 'Best First'.
Re^4: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?
by Cody Fendant (Hermit) on Jan 03, 2022 at 22:27 UTC

    I was using the very latest JSON, but not the very latest JSON::PP, so that's a factor. Upgrading JSON::PP and specifically using that has fixed it.

      It doesn't require JSON::PP or JSON::XS : it works perfectly well with JSON newer than v4.0:

      #!perl -l use 5.012; # strict, // use warnings; use JSON 4.0; use Data::Dumper; select STDERR; print $JSON::VERSION; my $json_text = q({"flag":true}); my $json = JSON->new; my $json_perl = $json->decode($json_text); print "Before boolean_values = ", Dumper($json_perl); $json->boolean_values(0,1); my $json_perl_after = $json->decode($json_text); print "After boolean_values = ", Dumper($json_perl_after); __END__ 4.02 Before boolean_values = $VAR1 = { 'flag' => bless( do{\(my $o = 1)}, 'JSON::PP::Boolean' ) }; After boolean_values = $VAR1 = { 'flag' => 1 };

      Unlike what you said in Re^5: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?, I believe the documentation is quite explicit about version requirements for that method: the text "boolean_values (since version 4.0)" is in bold header text in the POD, which I interpret as claiming that the method has only been supported since version 4.0. The only clarification in that section of the docs that I would like: not using brackets for "optional" arguments in POD, especially in code blocks, because brackets have very well-defined meaning in Perl, and boolean_values(0,1) works whereas boolean_values([0,1]) as their example implies does not.

      (Note also that your example code in Re^2: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object? swapped the order of the arguments, so a false would decode as 1 and a true as 0, which would cause you some difficulty at some point.)

        JSON.pm isn't a parser. It's just a front-end for JSON::PP or JSON::XS. So one most definitely needs JSON::PP or JSON::XS.

        Maybe you meant one doesn't need to use JSON::PP directly, but the parent post was clear that JSON.pm was the module being used directly.

      Since you're installing modules anyway, why not install JSON::XS. Doing so will make JSON.pm a "million" times faster.

      Caveat: It doesn't support threads.

      Note: I don't use JSON.pm because it defaults to the slow JSON::PP. I used to use JSON::XS directly.

      Note: I don't use JSON::XS because of the author's attitude and his unwillingness to support threads and who knows what else. I use Cpanel::JSON::XS directly.

      JSON.pm supports Cpanel::JSON::XS, but requires the use of an env var. That said, it doesn't support boolean_values.