Skip to content
Merged
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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ with httpx.Client() as client, client.stream('GET', 'http://example.com/data.jso

### <a id="iterators"></a> Stream an iterable

`json-stream`'s parsing functions can take any iterable object that produces encoded JSON as
`byte` objects.
`json-stream`'s parsing functions can take any iterable that produces encoded JSON chunks. The chunks can be `byte`s or `str`s.

```python
import json_stream
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "json-stream"
version = "2.4.0"
version = "2.4.1"
authors = [
{name = "Jamie Cockburn", email="jamie_cockburn@hotmail.co.uk"},
]
Expand Down
16 changes: 12 additions & 4 deletions src/json_stream/iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@
class IterableStream(io.RawIOBase):
def __init__(self, iterable):
self.iterator = iter(iterable)
self.remainder = None
self.remainder = None # type: bytes | None

def _normalize_chunk(self, chunk):
# Ensure chunk is bytes for writing into a binary buffer
if isinstance(chunk, str):
return chunk.encode()
return chunk

def readinto(self, buffer):
try:
chunk = self.remainder or next(self.iterator)
length = min(len(buffer), len(chunk))
buffer[:length], self.remainder = chunk[:length], chunk[length:]
# Wrap `buffer: WriteableBuffer` in memoryview to ensure len() and slicing
mv = memoryview(buffer)
chunk = self.remainder or self._normalize_chunk(next(self.iterator))
length = min(len(mv), len(chunk))
mv[:length], self.remainder = chunk[:length], chunk[length:]
return length
except StopIteration:
return 0 # indicate EOF
Expand Down
13 changes: 13 additions & 0 deletions src/json_stream/tests/test_iterators.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,16 @@ def test_read(self):
# stream it and check the result
stream = IterableStream(data)
self.assertEqual(stream.read(), b"".join(data))

def test_read_str_chunks(self):
# create some chunks of text data
data_str = (
"a" * io.DEFAULT_BUFFER_SIZE,
"b" * (io.DEFAULT_BUFFER_SIZE + 1),
"c" * (io.DEFAULT_BUFFER_SIZE - 1),
)
expected = ("".join(data_str)).encode()

# stream it and check the result
stream = IterableStream(data_str)
self.assertEqual(stream.read(), expected)