GitXplorerGitXplorer
s

jsonseq

public
9 stars
2 forks
2 issues

Commits

List of commits on branch master.
Verified
d9a9c86c6c05abca1ed2bdbff248eb3cd9f214fd

Update version for 1.0.0 (#6)

ssgillies committed 5 years ago
Unverified
4e1231a8e61dd1dc0d01aca79abe8123850234c3

Bump numpydoc from 0.8.0 to 0.9.1 (#5)

ddependabot-preview[bot] committed 5 years ago
Verified
c36c535ab8c632a6121a5a76fb7e0ff897fe3da5

Upgrade pytest

ssgillies committed 6 years ago
Unverified
55297465b1390de58a4981e5dac2ee5cd951ec6d

Pin numpydoc to latest version 0.8.0 (#3)

ppyup-bot committed 6 years ago
Unverified
15980d02af9d556faeba4fa310e0d5db12d26c4a

Add missing requirements file

ssgillies committed 6 years ago
Unverified
a5d6afa6546f0c67c70d5e763dfaa5701ca72c4d

Add requirements file and doc badge

ssgillies committed 6 years ago

README

The README file for this repository.

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}