Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How do I tell if my code is using JSON::PP or JSON::XS?

by Cody Fendant (Hermit)
on Nov 16, 2020 at 02:41 UTC ( [id://11123677]=perlquestion: print w/replies, xml ) Need Help??

Cody Fendant has asked for the wisdom of the Perl Monks concerning the following question:

Things are running slowly, and one reason might be that the code is using JSON::PP and not JSON::XS?

When I do Data::Dumper on some data, I’m seeing ‘JSON::PP::Boolean’ for the true/false values. Is that confirmation that the code uses PP or might that be a red herring?

There is a dir for x86-linux-thread-multi/auto/JSON/XS with XS.so inside it. Does that tell us anything definitive?

I thought that ‘use JSON’ would detect the availability of the XS module and use it if possible, is that correct?

TIA

Replies are listed 'Best First'.
Re: How do I tell if my code is using JSON::PP or JSON::XS?
by GrandFather (Saint) on Nov 16, 2020 at 03:35 UTC

    You can:

    my $json = JSON->new; print "Using JSON::XS\n" if $json->is_xs();

    See the JSON choosing backend documentation and (further down in the documentation) the additional methods section for is_xx and related methods.

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
      Thanks! I really should RTFM shouldn’t I?

        Indeed. The thought had crossed my mind :-D.

        Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond
Re: How do I tell if my code is using JSON::PP or JSON::XS?
by choroba (Cardinal) on Nov 16, 2020 at 08:56 UTC
    You can try comparing some of the operations that give different results in different modules. I showed some of them in my Glasgow talk in 2018.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: How do I tell if my code is using JSON::PP or JSON::XS?
by haukex (Archbishop) on Nov 16, 2020 at 16:10 UTC
    When I do Data::Dumper on some data, I’m seeing ‘JSON::PP::Boolean’ for the true/false values. Is that confirmation that the code uses PP or might that be a red herring?

    It's a red herring: JSON, JSON::XS, Cpanel::JSON::XS, JSON::PP, and Mojo::JSON (and possibly more that I've missed) all use JSON::PP::Boolean to represent their boolean types.

Re: How do I tell if my code is using JSON::PP or JSON::XS?
by 1nickt (Canon) on Nov 16, 2020 at 13:57 UTC

    Hi,

    I thought that ‘use JSON’ would detect the availability of the XS module and use it if possible, is that correct?

    Yep.

    You can always find out what modules you've loaded by examining %INC:

    $ perl -MJSON -E 'say for keys %INC' Exporter/Heavy.pm overload.pm Exporter.pm JSON.pm warnings.pm Types/Serialiser.pm overloading.pm Carp.pm warnings/register.pm strict.pm common/sense.pm XSLoader.pm JSON/XS.pm feature.pm attributes.pm

    Hope this helps!


    The way forward always starts with a minimal test.
Re: How do I tell if my code is using JSON::PP or JSON::XS?
by perlfan (Vicar) on Nov 19, 2020 at 05:47 UTC
    > I thought that ‘use JSON’ would detect the availability of the XS module and use it if possible, is that correct?

    I believe you're thinking of JSON::MaybeXS.

    This module first checks to see if either Cpanel::JSON::XS or JSON::XS (at at least version 3.0) is already loaded, in which case it uses that module. Otherwise it tries to load Cpanel::JSON::XS, then JSON::XS, then JSON::PP in order, and either uses the first module it finds or throws an error.
    

    Also please post some representational code, if you're hitting a bottleneck it could be a million different things.

      Did you look at the JSON docs?

      "This module is a thin wrapper for JSON::XS-compatible modules with a few additional features. All the backend modules convert a Perl data structure to a JSON text and vice versa. This module uses JSON::XS by default, and when JSON::XS is not available, falls back on JSON::PP, which is in the Perl core since 5.14. If JSON::PP is not available either, this module then falls back on JSON::backportPP (which is actually JSON::PP in a different .pm file) bundled in the same distribution as this module."

        I was replying to OP, I thought that ‘use JSON’ would detect the availability of the XS module and use it if possible, is that correct? I am really confused about your objection to my answer. I even qualified my answer with "perhaps". Maybe you can clarify exactly what your issue with my answer is because I have no idea what the point of your reply was.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11123677]
Approved by GrandFather
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-19 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found