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

Use boolean_values

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

Replies are listed 'Best First'.
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

    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?

      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.

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

      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.