summaryrefslogtreecommitdiff
path: root/index.rst
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2018-09-06 11:48:37 -0700
committerBob Ippolito <bob@redivi.com>2018-09-06 14:49:59 -0700
commit26524a0d907dda3fee69055026922876b309f8f3 (patch)
treed721c7bdae843ab4d7782bbf30ccb79f22eb8ab4 /index.rst
parentee25b0c7faeb072e2dd5bc3925bd8dc6fbb04d93 (diff)
downloadsimplejson-26524a0d907dda3fee69055026922876b309f8f3.tar.gz
Prep for v3.16.1 and add JSON lines examplejson-lines-example
Diffstat (limited to 'index.rst')
-rw-r--r--index.rst20
1 files changed, 20 insertions, 0 deletions
diff --git a/index.rst b/index.rst
index 94c792a..85388b4 100644
--- a/index.rst
+++ b/index.rst
@@ -124,6 +124,26 @@ Using :mod:`simplejson.tool` from the shell to validate and pretty-print::
.. highlight:: python
+Parsing multiple documents serialized as JSON lines (newline-delimited JSON)::
+
+ >>> import simplejson as json
+ >>> def loads_lines(docs):
+ ... for doc in docs.splitlines():
+ ... yield json.loads(doc)
+ ...
+ >>> sum(doc["count"] for doc in loads_lines('{"count":1}\n{"count":2}\n{"count":3}\n'))
+ 6
+
+Serializing multiple objects to JSON lines (newline-delimited JSON)::
+
+ >>> import simplejson as json
+ >>> def dumps_lines(objs):
+ ... for obj in objs:
+ ... yield json.dumps(obj, separators=(',',':')) + '\n'
+ ...
+ >>> ''.join(dumps_lines([{'count': 1}, {'count': 2}, {'count': 3}]))
+ '{"count":1}\n{"count":2}\n{"count":3}\n'
+
.. note::
JSON is a subset of `YAML <http://yaml.org/>`_ 1.2. The JSON produced by