simplejsonseq is a simple encoder and decoder for RFC 7464 JSON text sequences with an API mirroring that of json and simplejson. A JSON text sequence is a collection of JSON items in a text file, with each item preceded by an ASCII record separator (^^, U+1E) and usually followed by a line feed (^J, U+0A).
To convert a file to an iterable or to write out an iterable into a file, use the dump() and load() functions, similar to the json module:
>>> import io, simplejsonseq # In Python < 3, use cStringIO not io
>>> s = io.StringIO()
>>> simplejsonseq.dump(['hello', 'world'], s, buffered=False)
>>> s.getvalue()
'\x1e"hello"\n\x1e"world"\n'
>>> for e in simplejsonseq.load(io.StringIO('\x1e"hello"\n\x1e"world"\n')):
... print(e)
...
hello
world
The buffered=False setting flushes every item to storage as soon as it is written instead of doing it only once at the end of dump().
By default, the reader accepts unparseable items and wraps them into InvalidJSON objects, but the writer refuses to write these. Use the strict argument of either load and dump or JSONSeqReader and JSONSeqWriter to tune this behaviour, but bear in mind that the RFC recommends recovering from invalid input items:
>>> import sys; sys.stderr = sys.stdout # placate doctest
>>> import io, simplejsonseq # In Python < 3, use cStringIO not io
>>> value = '\x1e{"parrot":\n'
>>> items = list(simplejsonseq.load(value)) # doctest: +ELLIPSIS
/...: InvalidJSONWarning: Read invalid JSON: '{"parrot":\n'
...
>>> items
[InvalidJSON('{"parrot":\n', JSONDecodeError('Expecting value: line 2 column 1 (char 11)'))]
>>> list(simplejsonseq.load(value, strict=True))
Traceback (most recent call last):
...
json.decoder.JSONDecodeError: Expecting value: line 2 column 1 (char 11)
>>> s = io.StringIO()
>>> simplejsonseq.dump(items, s)
Traceback (most recent call last):
...
TypeError: Object of type InvalidJSON is not JSON serializable
>>> s = io.StringIO()
>>> simplejsonseq.dump(items, s, strict=False) # doctest:+ELLIPSIS
/...: InvalidJSONWarning: Wrote invalid JSON: '{"parrot":\n'
...
>>> s.getvalue() == value
True
For more sophisticated processing, use JSONSeqReader and JSONSeqWriter. These can also function as context managers to close the underlying file, if necessary:
>>> from simplejsonseq import JSONSeqReader, JSONSeqWriter
>>> test = ['hello', 'world']
>>> with JSONSeqWriter(open("/tmp/test.jsonseq", 'w'),
... buffered=False) as w:
... w.write(test[0])
... w.write(test[1])
...
>>> with JSONSeqReader(open("/tmp/test.jsonseq", 'r')) as r:
... print(list(r) == test)
...
True
In addition to passing buffered=False to the constructor of JSONSeqWriter, it is also possible to flush unwritten data explicitly using the flush() method, or to pass flush=True to an individual write() call to flush all data at the end of the call. It’s pointless to do either with a writer constructed with buffered=False, of course, as it flushes after every entry anyway.
Both the functions and the class constructors pass all extra keyword arguments to the underlying JSONDecoder or JSONEncoder. This can be used, for example, to dump the entries in a more readable (but still valid!) format:
>>> import io, simplejsonseq # In Python < 3, use cStringIO not io
>>> value = [True, {'holy': 'grenade', 'killer': 'bunny'}]
>>> s = io.StringIO()
>>> simplejsonseq.dump(value, s, indent=2)
>>> print(s.getvalue().replace('\x1e', '!'), end='')
!true
!{
"holy": "grenade",
"killer": "bunny"
}
>>> list(simplejsonseq.load(s.getvalue())) == value
True
Detailed documentation is available in the docstrings.
Release files for simplejsonseq 0.2.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| simplejsonseq-0.2.1.tar.gz | 12.7 kB | Details |
Built distributions (wheels)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| simplejsonseq-0.2.1-py3-none-any.whl | Python 3 | none | any | Details |
| simplejsonseq-0.2.1-py2-none-any.whl | Python 2 | none | any | Details |
Total release size: 34.7 kB
Release files / simplejsonseq-0.2.1.tar.gz
| Download URL | simplejsonseq-0.2.1.tar.gz |
|---|---|
| Size | 12.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fbb9da52ec2f6841c4716fd38789dccc91429df8c7ca29952403749d102b5a52
|
|
BLAKE2b-256 checksum How to use checksums |
96c9ef86d520b182db2241674cd4dad738697681567168b35e9ffd44200a2d28
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2
|
Release files / simplejsonseq-0.2.1-py3-none-any.whl
| Download URL | simplejsonseq-0.2.1-py3-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
dc0879d7a7ebdcc30561484e3960877c5845e85567fe08929bfa2bd7fbdd9df2
|
|
BLAKE2b-256 checksum How to use checksums |
7014c34ca006b2ed8a4693e79ca6888b3bd24272067d59ee8e366115cbd66fcc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2
|
Release files / simplejsonseq-0.2.1-py2-none-any.whl
| Download URL | simplejsonseq-0.2.1-py2-none-any.whl |
|---|---|
| Size | 11.0 kB |
| Tags | Python 2 |
|
SHA-256 checksum How to use checksums |
fb057f1014d74a9f36c400665a06638256f196cf6edbfe05cc3c279f5be72ea3
|
|
BLAKE2b-256 checksum How to use checksums |
65550a7f82043dba49555ad87f6f51af662b7590f2948d3288eed4af9e1da1cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.12.1 pkginfo/1.5.0.1 requests/2.18.4 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.2
|