Skip to main content

Python support for RFC 7464 JSON text sequences

Project description

jsonseq

RFC 7464 JSON Text Sequences encoding and decoding for Python.

Build Status Coverage Status Documentation Status

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}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

jsonseq-1.0a1.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

jsonseq-1.0a1-py3-none-any.whl (3.8 kB view details)

Uploaded Python 3

File details

Details for the file jsonseq-1.0a1.tar.gz.

File metadata

  • Download URL: jsonseq-1.0a1.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.3

File hashes

Hashes for jsonseq-1.0a1.tar.gz
Algorithm Hash digest
SHA256 08e78b6f05a30b9f3f09622332dbc9831ff68016065324dd97a9ab1ff9a32580
MD5 b1c40edbfdb28112fff73c5dbc1dc071
BLAKE2b-256 6d5a14d9b1a69aed17d1d6d09b9104fb6521aa89b50e389fb83c206d66a31e25

See more details on using hashes here.

File details

Details for the file jsonseq-1.0a1-py3-none-any.whl.

File metadata

  • Download URL: jsonseq-1.0a1-py3-none-any.whl
  • Upload date:
  • Size: 3.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.1 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.3

File hashes

Hashes for jsonseq-1.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 3a612110dc6f8b09bc2920987f40debb26318a30d0e34d354376bbae4ac45bdf
MD5 d2580ff6caef0529cb1f656965c7dec8
BLAKE2b-256 39d071e02687d2738d31a36b58e9752fc27ee95b0a68fd30c633e4f8b5f3205f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page