# jsonseq
[RFC 7464 JSON Text Sequences](https://tools.ietf.org/html/rfc7464) encoding and decoding for Python.
[![Build
Status](https://travis-ci.com/sgillies/jsonseq.svg?branch=master)](https://travis-ci.com/sgillies/jsonseq)
[![Coverage
Status](https://coveralls.io/repos/github/sgillies/jsonseq/badge.svg?branch=master)](https://coveralls.io/github/sgillies/jsonseq?branch=master)
[![Documentation Status](https://readthedocs.org/projects/jsonseq/badge/?version=latest)](https://jsonseq.readthedocs.io/en/latest/?badge=latest)
## 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`).
```python
>>> 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.
```python
>>> 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.
```python
>>> 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.
```python
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.
```python
>>> 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.
```python
>>> 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}
````
Raw data
{
"_id": null,
"home_page": "https://github.com/sgillies/jsonseq",
"name": "jsonseq",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "json rfc7464",
"author": "Sean Gillies",
"author_email": "sean.gillies@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/63/70/faca1f522bc03f92ac75da1eb29fe045bf89246a8e8ed04ccbd563540520/jsonseq-1.0.0.tar.gz",
"platform": "",
"description": "# jsonseq\n\n[RFC 7464 JSON Text Sequences](https://tools.ietf.org/html/rfc7464) encoding and decoding for Python.\n\n[![Build\nStatus](https://travis-ci.com/sgillies/jsonseq.svg?branch=master)](https://travis-ci.com/sgillies/jsonseq)\n[![Coverage\nStatus](https://coveralls.io/repos/github/sgillies/jsonseq/badge.svg?branch=master)](https://coveralls.io/github/sgillies/jsonseq?branch=master)\n[![Documentation Status](https://readthedocs.org/projects/jsonseq/badge/?version=latest)](https://jsonseq.readthedocs.io/en/latest/?badge=latest)\n\n## Usage\n\nThe `jsonseq.encode.JSONSeqEncoder` class takes streams of JSON-serializable\nPython objects and yields for each object its JSON representation sandwiched\nbetween an optional ASCII record separator (RS, `\\x1e`) and a newline (`\\n`).\n\n```python\n>>> from jsonseq.encode import JSONSeqEncoder\n>>> for chunk in JSONSeqEncoder().encode(({\"a\": i, \"b\": i} for i in range(3))):\n... print(repr(chunk))\n...\n'{\"a\": 0, \"b\": 0}\\n'\n'{\"a\": 1, \"b\": 1}\\n'\n'{\"a\": 2, \"b\": 2}\\n'\n```\n\nThe RS allows pretty-printed JSON to be streamed out in sequences that can be\ndecoded again.\n\n```python\n>>> for chunk in JSONSeqEncoder(with_rs=True, indent=2).encode(({\"a\": i, \"b\": i} for i in range(3))):\n... print(repr(chunk))\n...\n'\\x1e{\\n \"a\": 0,\\n \"b\": 0\\n}\\n'\n'\\x1e{\\n \"a\": 1,\\n \"b\": 1\\n}\\n'\n'\\x1e{\\n \"a\": 2,\\n \"b\": 2\\n}\\n'\n```\n\nYou can also get small chunks of the JSON sequences as they are encoded with\nthe `iterencode()` method.\n\n```python\n>>> for chunk in JSONSeqEncoder(with_rs=True).iterencode(({\"a\": i} for i in range(3))):\n... print(repr(chunk))\n...\n'\\x1e'\n'{'\n'\"a\"'\n': '\n'0'\n'}'\n'\\n'\n'\\x1e'\n'{'\n'\"a\"'\n': '\n'1'\n'}'\n'\\n'\n'\\x1e'\n'{'\n'\"a\"'\n': '\n'2'\n'}'\n'\\n'\n```\n\nYou can use either `encode()` or `iterencode()` to copy JSON text sequences to a file.\n\n```python\nwith open(\"/tmp/example.jsons\", \"w\") as f:\n for chunk in JSONSeqEncoder(with_rs=True, indent=2).iterencode(({\"a\": i, \"b\": i} for i in range(3))):\n f.write(chunk)\n```\n\nThere is no need to add a newline when calling the file's `write()` method.\nJSONSeqEncoder ensures that it's already there where it needs to be.\n\nThe `jsonseq.decode.JSONSeqDecoder` class takes streams of JSON texts\nsandwiched between the optional ASCII record separator (RS, `\\x1e`) and\na newline (`\\n`) and yields decoded Python objects.\n\n```python\n>>> stream = ['\\x1e', '{', '\"a\"', ': ', '0', '}', '\\n', '\\x1e', '{', '\"a\"', ': ', '1', '}', '\\n', '\\x1e', '{', '\"a\"', ': ', '2', '}', '\\n']\n>>> for obj in JSONSeqDecoder().decode(stream):\n... print(repr(obj))\n...\n{'a': 0}\n{'a': 1}\n{'a': 2}\n```\n\nObjects can be read from a file in the same way.\n\n```python\n>>> with open(\"/tmp/example.jsons\") as f:\n... for obj in JSONSeqDecoder().decode(f):\n... print(repr(obj))\n...\n{'a': 0, 'b': 0}\n{'a': 1, 'b': 1}\n{'a': 2, 'b': 2}\n````\n\n\n",
"bugtrack_url": null,
"license": "",
"summary": "Python support for RFC 7464 JSON text sequences",
"version": "1.0.0",
"project_urls": {
"Homepage": "https://github.com/sgillies/jsonseq"
},
"split_keywords": [
"json",
"rfc7464"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "04f5367876253306f752190203917a51670682780179665b38fc713629e0be71",
"md5": "fe0d4b73ba04ada7f53a079d0caa46a0",
"sha256": "d4add916420fc02796a503e59ce4d8008152830fd1625cc70692b1f980a32231"
},
"downloads": -1,
"filename": "jsonseq-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "fe0d4b73ba04ada7f53a079d0caa46a0",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4567,
"upload_time": "2019-07-31T19:47:33",
"upload_time_iso_8601": "2019-07-31T19:47:33.486387Z",
"url": "https://files.pythonhosted.org/packages/04/f5/367876253306f752190203917a51670682780179665b38fc713629e0be71/jsonseq-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "6370faca1f522bc03f92ac75da1eb29fe045bf89246a8e8ed04ccbd563540520",
"md5": "8ecd561fa318378f3df49f7c0497799f",
"sha256": "238f51aa741132d2a41d1fb89e58eb8d43c6da9d34845c9499dd882a4cd0253a"
},
"downloads": -1,
"filename": "jsonseq-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "8ecd561fa318378f3df49f7c0497799f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 3341,
"upload_time": "2019-07-31T19:47:34",
"upload_time_iso_8601": "2019-07-31T19:47:34.624549Z",
"url": "https://files.pythonhosted.org/packages/63/70/faca1f522bc03f92ac75da1eb29fe045bf89246a8e8ed04ccbd563540520/jsonseq-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2019-07-31 19:47:34",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "sgillies",
"github_project": "jsonseq",
"travis_ci": true,
"coveralls": false,
"github_actions": false,
"lcname": "jsonseq"
}