json-lines
json-lines copied to clipboard
Problem with InvalidJson thrown in Generator
Hi Raphael,
I'm parsing JSON Lines files from an external source, and recently one of those files contained a line of invalid JSON.
This library then throws an InvalidJson exception
https://github.com/raphaelstolt/json-lines/blob/e6c7c1e48fd8e2630b6ab1c7507cff5e0cdca499/src/JsonLines.php#L46
, but because I'm iterating over the file using delineEachLineFromFile I cannot catch the exception without exiting the loop/closing the Generator. Or did I miss anything?
My code looks like this:
foreach ((new JsonLines())->delineEachLineFromFile($file) as $line) {
yield $this->doSomething(json_decode($line, true, 512, JSON_THROW_ON_ERROR));
}
Do you have any ideas on how to catch the exception and continue with the next line?
I was thinking about yielding the exception; then I could add an instanceof InvalidJson or something similar and just ignore the line. What do you think?
public function delineEachLineFromFile($jsonLinesFile)
{
$jsonLines = [];
foreach ($this->getFileLines($jsonLinesFile) as $line) {
try {
$this->guardedJsonLine($line);
} catch (InvalidJson $exception) {
yield $exception;
continue;
}
yield trim($line);
}
}
// and then
foreach ((new JsonLines())->delineEachLineFromFile($this->dir->path($file)) as $line) {
try {
yield $this->doSomething(json_decode($line, true, 512, JSON_THROW_ON_ERROR));
} catch (InvalidJson $e) {
//continue
}
}
But this would break BC I guess …