Skip to content

Commit c673ace

Browse files
#169 Fix JsonStreamFile empty line at the end issue, even if SKIP_EMPTY is set. Add JsonStreamReaderTask doc.
1 parent b2fc5c1 commit c673ace

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

docs/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
- [CSVSplitterTask]
4747
- [InputCsvReaderTask](reference/tasks/input_csv_reader_task.md)
4848
- File/JsonStream
49-
- [JsonStreamReaderTask]
49+
- [JsonStreamReaderTask](reference/tasks/json_stream_reader_task.md)
5050
- [JsonStreamWriterTask](reference/tasks/json_stream_writer_task.md)
5151
- File/XML
5252
- [XmlReaderTask](reference/tasks/xml_reader_task.md)
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
JsonStreamReaderTask
2+
=============
3+
4+
Reads a json file and iterate on each line, returning decoded content as array. Skips empty lines.
5+
6+
Task reference
7+
--------------
8+
9+
* **Service**: `CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamReaderTask`
10+
* **Iterable task**
11+
12+
Accepted inputs
13+
---------------
14+
15+
`string`: Path of the file to read from (absolute)
16+
17+
Possible outputs
18+
----------------
19+
20+
`array`: foreach line, it will return content as array.
21+
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).
22+
23+
Options
24+
-------
25+
26+
none
27+
28+
Example
29+
-------
30+
31+
```yaml
32+
# Task configuration level
33+
entry:
34+
service: '@CleverAge\ProcessBundle\Task\ConstantIterableOutputTask'
35+
outputs: read
36+
options:
37+
output:
38+
file_path: '%kernel.project_dir%/var/data/json_stream_reader.json'
39+
read:
40+
service: '@CleverAge\ProcessBundle\Task\File\JsonStream\JsonStreamReaderTask'
41+
```
42+
43+

src/Filesystem/JsonStreamFile.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public function __construct(string $filename, string $mode = 'rb')
2828
{
2929
$this->file = new \SplFileObject($filename, $mode);
3030

31-
// Useful to skip empty trailing lines
32-
$this->file->setFlags(\SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY);
31+
// Useful to skip empty trailing lines (doesn't work well on PHP 8, see readLine() code)
32+
$this->file->setFlags(\SplFileObject::DROP_NEW_LINE | \SplFileObject::READ_AHEAD | \SplFileObject::SKIP_EMPTY );
3333
}
3434

3535
/**
@@ -72,6 +72,10 @@ public function readLine(?int $length = null): ?array
7272
}
7373

7474
$rawLine = $this->file->fgets();
75+
// Fix issue on PHP 8 with empty line at the end, even if SKIP_EMPTY is set
76+
if ('' === $rawLine) {
77+
return null;
78+
}
7579
++$this->lineNumber;
7680

7781
return json_decode($rawLine, true, 512, \JSON_THROW_ON_ERROR);

0 commit comments

Comments
 (0)