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
|
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
| [reply] [Watch: Dir/Any] [d/l] |
|
Thanks! I really should RTFM shouldn’t I?
| [reply] [Watch: Dir/Any] |
|
| [reply] [Watch: Dir/Any] |
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]
| [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] |
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.
| [reply] [Watch: Dir/Any] [d/l] [select] |
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
|
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. | [reply] [Watch: Dir/Any] |
|
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."
| [reply] [Watch: Dir/Any] |
|
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.
| [reply] [Watch: Dir/Any] |
|
|
|