jsonparse: Add max_depth, max_size and offset options

This completes all the functionality that I wanted from the JSON parser.
This commit is contained in:
Yorhel 2025-02-01 11:01:43 +01:00
parent abfbba3c10
commit 13eaeb1d4a
3 changed files with 105 additions and 9 deletions

View file

@ -50,6 +50,9 @@ my @error = (
'{,}',
'{"":1,"":2}',
'{"ë":1,"ë":1}',
'[] x',
'{}x',
);
for my $s (@error) {
ok !eval { json_parse($s); 1 };
@ -177,6 +180,10 @@ for (2000..2100, 4000..4200, 8100..8200, 12200..12300, 16300..16400) {
is json_parse("\"$s\""), $s
}
ok !eval { json_parse '[[[[]]]]', max_depth => 4; 1 };
ok !eval { json_parse '{"":{"":{"":{"":1}}}}', max_depth => 4; 1 };
# 500 depth
{
$v = json_parse('['x500 . ']'x500);
@ -191,4 +198,39 @@ for (2000..2100, 4000..4200, 8100..8200, 12200..12300, 16300..16400) {
is $i, 500;
}
# offset / max_size
{
my $off = 0;
my $str = '0123-5.3e1"x"[]{}truefalse 1 '; # cursed
is json_parse($str, offset => \$off), 0;
is $off, 1;
is json_parse($str, offset => \$off, max_size => 4), 123;
is $off, 4;
is json_parse($str, offset => \$off), -53;
is $off, 10;
is json_parse($str, offset => \$off), 'x';
is $off, 13;
is ref json_parse($str, offset => \$off), 'ARRAY';
is $off, 15;
is ref json_parse($str, offset => \$off), 'HASH';
is $off, 17;
ok json_parse($str, offset => \$off);
is $off, 21;
ok !json_parse($str, offset => \$off);
is $off, 27;
is json_parse($str, offset => \$off), 1;
ok !defined $off;
ok !eval { json_parse $str, offset => \$off; 1 };
$off = 100;
ok !eval { json_parse $str, offset => \$off; 1 };
$off = 17;
ok !eval { json_parse $str, offset => \$off, max_size => 3; 1 };
is json_parse('"string"', max_size => 8), 'string';
ok !eval { json_parse '"string"', max_size => 7 };
}
done_testing;