Skip to main content

Encoder and decoder for JSON text sequences

Project description

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.

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

simplejsonseq-0.2.1.tar.gz (12.7 kB view details)

Uploaded Source

Built Distributions

simplejsonseq-0.2.1-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

simplejsonseq-0.2.1-py2-none-any.whl (11.0 kB view details)

Uploaded Python 2

File details

Details for the file simplejsonseq-0.2.1.tar.gz.

File metadata

  • Download URL: simplejsonseq-0.2.1.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for simplejsonseq-0.2.1.tar.gz
Algorithm Hash digest
SHA256 fbb9da52ec2f6841c4716fd38789dccc91429df8c7ca29952403749d102b5a52
MD5 0746f70046ee362e9f5905c2289e7924
BLAKE2b-256 96c9ef86d520b182db2241674cd4dad738697681567168b35e9ffd44200a2d28

See more details on using hashes here.

File details

Details for the file simplejsonseq-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: simplejsonseq-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for simplejsonseq-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dc0879d7a7ebdcc30561484e3960877c5845e85567fe08929bfa2bd7fbdd9df2
MD5 234714c373bd24f14e7ae12b1d870d4d
BLAKE2b-256 7014c34ca006b2ed8a4693e79ca6888b3bd24272067d59ee8e366115cbd66fcc

See more details on using hashes here.

File details

Details for the file simplejsonseq-0.2.1-py2-none-any.whl.

File metadata

  • Download URL: simplejsonseq-0.2.1-py2-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 2
  • Uploaded using 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

File hashes

Hashes for simplejsonseq-0.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 fb057f1014d74a9f36c400665a06638256f196cf6edbfe05cc3c279f5be72ea3
MD5 fa56ed9bc4d0bc7c8a92e590431bce1d
BLAKE2b-256 65550a7f82043dba49555ad87f6f51af662b7590f2948d3288eed4af9e1da1cb

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