in reply to Issues using boolean_values in JSON v4.02

Update: As ikegami++ points out, the below won't work: as mentioned in the documentation, the different backends have incompatible object formats, so you can't mix-and-match. Without updating any modules, then, the only way to get at this functionality is to use JSON::PP as the backend (eg by setting the PERL_JSON_BACKEND environment variable), which will be at the cost of the speed benefit of the XS backend.

It should be possible to create a shim module that checks which of the available backends has a boolean_values function, and sets the backend accordingly. But if the code is deployed only in a single place that probably has little value.

I would expect the approach in my original answer below to work for classes that have a single master object and delegate to your choice of backends for the functionality - this is how I wrongly thought the JSON class worked.

Looking at the code for JSON-4.02, there is no mention of boolean_values, so I suspect you will need to call the JSON::PP function explicitly. This should work:

JSON::PP::boolean_values($json, 0, 1);

Alternatively, in your code you could inject the desired pass-through function into the JSON package:

{ package JSON { sub boolean_values { JSON::PP::boolean_values(@_); } }; }

.. or create your own application-specific JSON wrapper:

package Our::JSON; use base qw{ JSON }; sub boolean_values { JSON::PP::boolean_values(@_); } 1;

For extra points, inject the function in a BEGIN block only if !JSON->can('boolean_values'), so it automatically turns itself off when a JSON release catches up - that doesn't seem to have happened yet as of v4.10.

I'd recommend reporting this as an issue against the JSON package, it's possible I'm missing some good reason why this isn't already implemented - I suspect it involves no more than adding it to the @PublicMethods list, with corresponding docs and tests.

Replies are listed 'Best First'.
Re^2: Issues using boolean_values in JSON v4.02
by ikegami (Patriarch) on Jun 15, 2023 at 04:20 UTC

    Your fix can't possibly work.

    JSON inherits from either JSON::PP, JSON::XS or Cpanel::JSON::XS

    If it's inheriting from JSON::PP, then your fix does nothing at all. That doesn't help.

    If it's inheriting from JSON::XS, your fix would cause a JSON::XS object to be passed to JSON::PP. That won't work.

    If it's inheriting from Cpanel::JSON::XS, your fix would cause a Cpanel::JSON::XS object to be passed to JSON::PP. That won't work.

      Ah, I think you're right; I guess I didn't read enough of the code - I see now that packages such as JSON::Backend::PP fiddle with @JSON::ISA (which seems like exactly the sort of action-at-a-distance we usually recommend against).