Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: 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 07:40 UTC ( #11140092=note: print w/replies, xml ) Need Help??


in reply to Re: 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?

Thanks, that looks like what I should be doing all right.

But doing this:

use JSON; my $j = JSON->new(); $j->boolean_values( 1, 0 );

Gets me Can't locate object method "boolean_values" via package "JSON"

What am I doing wrong?

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

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

    grepping CPAN it seems the method ships with JSON::PP and you can read about it

    # Note that JSON version 2.0 and above will automatically use # JSON::XS or JSON::PP, so you should be able to just: use JSON;

    This probably means you have also JSON::XS installed and it is loaded first

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Both JSON::PP and JSON::XS support boolean_values, and the error message said the method wasn't found in JSON, not JSON::XS.

        Well my question is about JSON, but if the answer is to use one of the other modules specifically then I guess that works.

        I would still say the documentation could be improved though.

Re^3: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?
by bliako (Monsignor) on Jan 03, 2022 at 08:18 UTC

    works fine for me, JSON v4.03, JSON::PP v4.06

Re^3: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?
by ikegami (Patriarch) on Jan 03, 2022 at 14:23 UTC

    Are you using version 4+?

      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.)

        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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11140092]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-06-26 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My most frequent journeys are powered by:









    Results (85 votes). Check out past polls.

    Notices?