Problem when dealing with empty arrays #2

Closed
opened 2020-08-12 15:04:10 +03:00 by maxmitti · 0 comments
Contributor

I am facing problems when parsing empty arrays.

Here is a reduced example:

$done = false;
$json = new \JSONStream(function() use(&$done) {
    if($done) {
    return '';
    }
    $done = true;
    return '{"foo":[]}';
});

$json->enterObject();

$json->readValue($k);
print $k;

$json->enterArray();

print $json->readValue($v) ? $v : 'false';

$json->exitArray();

print $json->readValue($v) ? $v : 'false';
$json->exitObject();

I would expect this code to print foo false false and not throw any exception.

Actually, the last readValue throws an exception instead of returning false.
If instead omitting the last readValue exitObject throws an exception.

After further testing, it also seems to omit the last array value, e.g. with the JSON string ´{"foo":[123, 456]}´ it reads 123 successfully, but returns false on the second readValue already.

Edit: It actually stores 456 in $v, but also returns false. I am not sure if that is intended or not.

Apart from that your parser works great and is nice to use :)

I am facing problems when parsing empty arrays. Here is a reduced example: ``` $done = false; $json = new \JSONStream(function() use(&$done) { if($done) { return ''; } $done = true; return '{"foo":[]}'; }); $json->enterObject(); $json->readValue($k); print $k; $json->enterArray(); print $json->readValue($v) ? $v : 'false'; $json->exitArray(); print $json->readValue($v) ? $v : 'false'; $json->exitObject(); ``` I would expect this code to print foo false false and not throw any exception. Actually, the last readValue throws an exception instead of returning false. If instead omitting the last readValue exitObject throws an exception. After further testing, it also seems to omit the last array value, e.g. with the JSON string ´{"foo":[123, 456]}´ it reads 123 successfully, but returns false on the second readValue already. **Edit**: It actually stores 456 in $v, but also returns false. I am not sure if that is intended or not. Apart from that your parser works great and is nice to use :)
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: vitalif/fast-json-stream#2
No description provided.