makamaka / JSON

perl implementation of JSON encoder/decoder
42 stars 40 forks source link

How to handle old JSON::XS version #48

Open rovo89 opened 3 years ago

rovo89 commented 3 years ago

I have a scenario with hundreds of servers with different Perl versions and operating systems. On these servers, I need to run a script which decodes a JSON file. I can push additional libraries to the servers via a shared directory, but due to the different Perl versions, they can't contain XS code.

I'd like to leverage a pre-installed JSON::XS wherever possible and fall back to JSON::PP otherwise. Caveat: I use boolean_values(), which is only available from version 4.0. Various servers have only JSON::XS version 3.0.1, so this call fails.

Is there any way to use JSON::XS only if it has a certain minimum version / supports a certain method and use PP otherwise? I also tried -support_by_pp, but it didn't help here.

charsbar commented 3 years ago

I hope this works for you.

BEGIN {
    if ( !eval { require JSON::XS; JSON::XS->VERSION(4.00) } ) {
        $ENV{PERL_JSON_BACKEND} = 'JSON::backportPP'; 
    }
}
use JSON;