Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

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 ( [id://11140117]=note: print w/replies, xml ) Need Help??


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

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.

  • Comment on Re^4: 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^5: How do I make the JSON module convert true/false to 1/0 instead of a blessed Boolean object?
by pryrt (Abbot) on Jan 03, 2022 at 22:54 UTC
    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.

        JSON.pm isn't a parser. It's just a front-end ...

        Sorry, you're right, I hadn't remembered that JSON.pm just calls one of the others as a back end.

        but the parent post was clear that JSON.pm was the module being used directly.

        The original post was clear that JSON.pm was the module originally being used directly. However, in the message I replied to, Cody Fendant said, "Upgrading JSON::PP and specifically using that has fixed it.", where I interpreted° "specially using that" as meaning "calling use JSON::PP instead of use JSON". And as you stated, and as my code shows, you do not have to specifically use JSON::PP in order to get it to work correctly, so one of my points -- that you can get it to work while calling JSON.pm directly -- is still valid.

        edit: °... Though I see, after reading your other posts, that another possible reading is that they were manually selecting that JSON.pm use the JSON::PP backend. So maybe we've just been talking past each other, with different base assumptions on what certain phrasings implied. I agree with everything you said, but wanted to leave this as a clarification of what I said earlier.

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

    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://11140117]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-26 04:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found