jsonseq
RFC 7464 JSON Text Sequences encoding and decoding for Python.
Usage
The jsonseq.encode.JSONSeqEncoder class takes streams of JSON-serializable
Python objects and yields for each object its JSON representation sandwiched
between an optional ASCII record separator (RS, \x1e) and a newline (\n).
>>> from jsonseq.encode import JSONSeqEncoder
>>> for chunk in JSONSeqEncoder().encode(({"a": i, "b": i} for i in range(3))):
... print(repr(chunk))
...
'{"a": 0, "b": 0}\n'
'{"a": 1, "b": 1}\n'
'{"a": 2, "b": 2}\n'
The RS allows pretty-printed JSON to be streamed out in sequences that can be decoded again.
>>> for chunk in JSONSeqEncoder(with_rs=True, indent=2).encode(({"a": i, "b": i} for i in range(3))):
... print(repr(chunk))
...
'\x1e{\n "a": 0,\n "b": 0\n}\n'
'\x1e{\n "a": 1,\n "b": 1\n}\n'
'\x1e{\n "a": 2,\n "b": 2\n}\n'
You can also get small chunks of the JSON sequences as they are encoded with
the iterencode() method.
>>> for chunk in JSONSeqEncoder(with_rs=True).iterencode(({"a": i} for i in range(3))):
... print(repr(chunk))
...
'\x1e'
'{'
'"a"'
': '
'0'
'}'
'\n'
'\x1e'
'{'
'"a"'
': '
'1'
'}'
'\n'
'\x1e'
'{'
'"a"'
': '
'2'
'}'
'\n'
You can use either encode() or iterencode() to copy JSON text sequences to a file.
with open("/tmp/example.jsons", "w") as f:
for chunk in JSONSeqEncoder(with_rs=True, indent=2).iterencode(({"a": i, "b": i} for i in range(3))):
f.write(chunk)
There is no need to add a newline when calling the file's write() method.
JSONSeqEncoder ensures that it's already there where it needs to be.
The jsonseq.decode.JSONSeqDecoder class takes streams of JSON texts
sandwiched between the optional ASCII record separator (RS, \x1e) and
a newline (\n) and yields decoded Python objects.
>>> stream = ['\x1e', '{', '"a"', ': ', '0', '}', '\n', '\x1e', '{', '"a"', ': ', '1', '}', '\n', '\x1e', '{', '"a"', ': ', '2', '}', '\n']
>>> for obj in JSONSeqDecoder().decode(stream):
... print(repr(obj))
...
{'a': 0}
{'a': 1}
{'a': 2}
Objects can be read from a file in the same way.
>>> with open("/tmp/example.jsons") as f:
... for obj in JSONSeqDecoder().decode(f):
... print(repr(obj))
...
{'a': 0, 'b': 0}
{'a': 1, 'b': 1}
{'a': 2, 'b': 2}
Metadata
Release files for jsonseq 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| jsonseq-1.0.0.tar.gz | 3.3 kB | Without provenance | Details for jsonseq-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| jsonseq-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for jsonseq-1.0.0-py3-none-any.whl |
Total release size: 7.9 kB
Release files / jsonseq-1.0.0.tar.gz
| Download URL | jsonseq-1.0.0.tar.gz |
|---|---|
| Size | 3.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
238f51aa741132d2a41d1fb89e58eb8d43c6da9d34845c9499dd882a4cd0253a
|
|
BLAKE2b-256 checksum How to use checksums |
6370faca1f522bc03f92ac75da1eb29fe045bf89246a8e8ed04ccbd563540520
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.3
|
Release files / jsonseq-1.0.0-py3-none-any.whl
| Download URL | jsonseq-1.0.0-py3-none-any.whl |
|---|---|
| Size | 4.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d4add916420fc02796a503e59ce4d8008152830fd1625cc70692b1f980a32231
|
|
BLAKE2b-256 checksum How to use checksums |
04f5367876253306f752190203917a51670682780179665b38fc713629e0be71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.3
|
Security for jsonseq 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.