diff --git a/docs/index.md b/docs/index.md index aad5bbda..5e678dba 100644 --- a/docs/index.md +++ b/docs/index.md @@ -46,7 +46,7 @@ - [CSVSplitterTask] - [InputCsvReaderTask](reference/tasks/input_csv_reader_task.md) - File/JsonStream - - [JsonStreamReaderTask] + - [JsonStreamReaderTask](reference/tasks/json_stream_reader_task.md) - [JsonStreamWriterTask](reference/tasks/json_stream_writer_task.md) - File/XML - [XmlReaderTask](reference/tasks/xml_reader_task.md) diff --git a/docs/reference/tasks/json_stream_reader_task.md b/docs/reference/tasks/json_stream_reader_task.md new file mode 100644 index 00000000..e48da640 --- /dev/null +++ b/docs/reference/tasks/json_stream_reader_task.md @@ -0,0 +1,43 @@ +JsonStreamReaderTask +============= + +Reads a json file and iterate on each line, returning decoded content as array. Skips empty lines. + +Task reference +-------------- + +* **Service**: `CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamReaderTask` +* **Iterable task** + +Accepted inputs +--------------- + +`string`: Path of the file to read from (absolute) + +Possible outputs +---------------- + +`array`: foreach line, it will return content as array. +Underlying method are [SplFileObject::fgets](https://www.php.net/manual/fr/splfileobject.fgets.php) and [json_decode](https://www.php.net/manual/en/function.json-decode.php). + +Options +------- + +none + +Example +------- + +```yaml +# Task configuration level +entry: + service: '@CleverAge\ProcessBundle\Task\ConstantIterableOutputTask' + outputs: read + options: + output: + file_path: '%kernel.project_dir%/var/data/json_stream_reader.json' +read: + service: '@CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamReaderTask' +``` + + diff --git a/src/Filesystem/JsonStreamFile.php b/src/Filesystem/JsonStreamFile.php index cd4359e0..6d4873b3 100644 --- a/src/Filesystem/JsonStreamFile.php +++ b/src/Filesystem/JsonStreamFile.php @@ -28,8 +28,8 @@ public function __construct(string $filename, string $mode = 'rb') { $this->file = new \SplFileObject($filename, $mode); - // Useful to skip empty trailing lines - $this->file->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); + // Useful to skip empty trailing lines (doesn't work well on PHP 8, see readLine() code) + $this->file->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY); } /** @@ -72,6 +72,10 @@ public function readLine(?int $length = null): ?array } $rawLine = $this->file->fgets(); + // Fix issue on PHP 8 with empty line at the end, even if SKIP_EMPTY is set + if ('' === $rawLine) { + return null; + } ++$this->lineNumber; return json_decode($rawLine, true, 512, \JSON_THROW_ON_ERROR);