开发者

Why do I get "Usage: JSON::XS::new(klass)" error?

开发者 https://www.devze.com 2023-04-06 07:08 出处:网络
This line my $json = new JSON(autoconv => 0);# <-- line X raises this error Usage: JSON::XS::new(klass) at (...) line X

This line

my $json = new JSON(autoconv => 0); # <-- line X

raises this error

Usage: JSON::XS::new(klass) at (...) line X
开发者_运维技巧

What's wrong?

It's a follow up to my question Is there a way to force quotation of numbers in JSON 1.x Perl module?


The API for the JSON module changed substantially between 1.15 and 2.00. Code written for JSON 1.x won't necessarily work with JSON 2.x. In particular, the 1.x constructor took optional parameters. The 2.x constructor takes no parameters; instead, you use mutator functions after construction.

If you must support both JSON 1.x and 2.x for some reason, you'll need to check if JSON->VERSION < 2 (actually JSON->VERSION < 1.99 if you count the development releases of the 2.x API) and have two versions of your code, one for the 1.x API and one for 2.x.


Try:

my $json = JSON::XS->new;

I don't see any autoconv anywhere in either JSON or JSON::XS, but JSON does say this:

$JSON::AUTOCONVERT

Needless. JSON backend modules have the round-trip integrity.

0

精彩评论

暂无评论...
验证码 换一张
取 消