Skip to content
Merged

169 #171

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
43 changes: 43 additions & 0 deletions docs/reference/tasks/json_stream_reader_task.md
Original file line number Diff line number Diff line change
@@ -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'
```


8 changes: 6 additions & 2 deletions src/Filesystem/JsonStreamFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down