Skip to content

Commit 5ac6e0b

Browse files
committed
Implement stream-based load()
This is the decoding part of issue python-rapidjson#80: the new function load() accepts a stream instead of a string, and Decoder() may be called with either a string or a stream.
1 parent f471e5b commit 5ac6e0b

7 files changed

Lines changed: 577 additions & 139 deletions

File tree

‎docs/api.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
dumps
1818
loads
19+
load
1920
encoder
2021
decoder
2122
validator

‎docs/decoder.rst‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
.. testsetup::
1515

16+
import io
1617
from rapidjson import (Decoder, Encoder, DM_NONE, DM_ISO8601, DM_UNIX_TIME,
1718
DM_ONLY_SECONDS, DM_IGNORE_TZ, DM_NAIVE_IS_UTC, DM_SHIFT_TO_UTC,
1819
UM_NONE, UM_CANONICAL, UM_HEX, NM_NATIVE, NM_DECIMAL, NM_NAN,
@@ -31,17 +32,20 @@
3132
extensions <loads-parse-mode>`
3233

3334

34-
.. method:: __call__(json)
35+
.. method:: __call__(json, chunk_size=102400)
3536

36-
:param json: either a ``str`` instance or an *UTF-8* ``bytes`` instance, containing
37-
the ``JSON`` to be decoded
37+
:param json: either a ``str`` instance, an *UTF-8* ``bytes`` instance or a
38+
*file-like* stream, containing the ``JSON`` to be decoded
39+
:param int size: in case of a stream, it will be read in chunks of this size
3840
:returns: a Python value
3941

4042
.. doctest::
4143

4244
>>> decoder = Decoder()
4345
>>> decoder('"€ 0.50"')
4446
'€ 0.50'
47+
>>> decoder(io.StringIO('"€ 0.50"'))
48+
'€ 0.50'
4549
>>> decoder(b'"\xe2\x82\xac 0.50"')
4650
'€ 0.50'
4751

‎docs/load.rst‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
.. -*- coding: utf-8 -*-
2+
.. :Project: python-rapidjson -- load function documentation
3+
.. :Author: Lele Gaifax <lele@metapensiero.it>
4+
.. :License: MIT License
5+
.. :Copyright: © 2017 Lele Gaifax
6+
..
7+
8+
=================
9+
load() function
10+
=================
11+
12+
.. module:: rapidjson
13+
14+
.. testsetup::
15+
16+
from io import StringIO
17+
from rapidjson import load
18+
19+
.. function:: load(stream, *, object_hook=None, number_mode=None, datetime_mode=None, \
20+
uuid_mode=None, parse_mode=None, chunk_size=102400, allow_nan=True)
21+
22+
Decode the given Python file-like `stream` containing a ``JSON`` formatted value
23+
into Python object.
24+
25+
:param stream: a file-like object
26+
:param callable object_hook: an optional function that will be called with the result
27+
of any object literal decoded (a :class:`dict`) and should
28+
return the value to use instead of the :class:`dict`
29+
:param int number_mode: enable particular behaviors in handling numbers
30+
:param int datetime_mode: how should :class:`datetime` and :class:`date` instances be
31+
handled
32+
:param int uuid_mode: how should :class:`UUID` instances be handled
33+
:param int parse_mode: whether the parser should allow non-standard JSON extensions
34+
:param int chunk_size: read the stream in chunks of this size at a time
35+
:param bool allow_nan: *compatibility* flag equivalent to ``number_mode=NM_NAN``
36+
:returns: An equivalent Python object.
37+
38+
The function has the same behaviour as :func:`loads()`, except for the kind of the
39+
first argument that is expected to be file-like object instead of a string:
40+
41+
.. doctest::
42+
43+
>>> stream = StringIO('["string", {"kind": "object"}, 3.14159]')
44+
>>> load(stream)
45+
['string', {'kind': 'object'}, 3.14159]
46+
47+
Consult the :func:`loads()` documentation for details on all other arguments.

‎docs/loads.rst‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
UM_NONE, UM_CANONICAL, UM_HEX, NM_NATIVE, NM_DECIMAL, NM_NAN,
1919
PM_NONE, PM_COMMENTS, PM_TRAILING_COMMAS)
2020

21-
.. function:: loads(s, object_hook=None, number_mode=None, datetime_mode=None, \
21+
.. function:: loads(string, *, object_hook=None, number_mode=None, datetime_mode=None, \
2222
uuid_mode=None, parse_mode=None, allow_nan=True)
2323

24-
Decode the given Python string `s` containing a ``JSON`` formatted value
24+
Decode the given Python string `string` containing a ``JSON`` formatted value
2525
into Python object.
2626

27-
:param str s: The JSON string to parse
27+
:param str string: The JSON string to parse
2828
:param callable object_hook: an optional function that will be called with the result
2929
of any object literal decoded (a :class:`dict`) and should
3030
return the value to use instead of the :class:`dict`

0 commit comments

Comments
 (0)