user1188611
user1188611

Reputation: 955

json text or perl structure exceeds maximum nesting level (max_depth set too low?)

I am getting the following error from JSON's encode_json:

json text or perl structure exceeds maximum nesting level (max_depth set too low?)

The code is in question is

my $jsonString = encode_json($dataXML);

$dataXML was produced by XML::Simple's XMLin. Any pointers about how to remove this error?

Upvotes: 1

Views: 1461

Answers (1)

ikegami
ikegami

Reputation: 386216

You get that error from JSON::PP when the structure has 512 levels of nesting. It's probably meant to catch unserializable reference loops (my $data = { }; $data->{foo} = $data;) and to prevent malicious attempts to use up all your memory.

If those aren't your problems, if the issue is simply that you need to support ginormous structures, you can increase the threshold using ->max_depth. Keep mind that

encode_json($data)

is short for

my $json = JSON->new->utf8;
$json->encode($data)

so you can use

my $json = JSON->new->utf8->max_depth(...);
$json->encode($data)

Alternatively, JSON::XS might not have that check. If it doesn't, simply installing JSON::XS will avoid the error. That's on top of speeding up your encoding and decoding.

Upvotes: 2

Related Questions