diff options
| author | Dan Halbert <halbert@adafruit.com> | 2020-07-16 14:33:33 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-16 14:33:33 -0400 |
| commit | 88d89563783771daba2973eb8dc3faa237ba4b62 (patch) | |
| tree | a5403791e9126eb2ec6c654b36f8c4b4ca705085 /tests | |
| parent | 72f6d7ee21bd442b87def2751d9c65dd887fe3ba (diff) | |
| parent | 372bcf8a956c8b20c423333114d7aa061dd54751 (diff) | |
Merge pull request #3143 from tannewt/improve_json
Add support to json.load for any object with readinto
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/extmod/ujson_load_readinto.py | 22 | ||||
| -rw-r--r-- | tests/extmod/ujson_load_readinto.py.exp | 4 |
2 files changed, 26 insertions, 0 deletions
diff --git a/tests/extmod/ujson_load_readinto.py b/tests/extmod/ujson_load_readinto.py new file mode 100644 index 000000000..a277f40ef --- /dev/null +++ b/tests/extmod/ujson_load_readinto.py @@ -0,0 +1,22 @@ +import ujson as json + +# Test that json can load from any object with readinto + +class Buffer: + def __init__(self, data): + self._data = data + self._i = 0 + + def readinto(self, buf): + end = self._i + len(buf) + remaining = len(self._data) - self._i + end = min(end, len(self._data)) + l = min(len(buf), remaining) + buf[:l] = self._data[self._i:end] + self._i += l + return l + +print(json.load(Buffer(b'null'))) +print(json.load(Buffer(b'"abc\\u0064e"'))) +print(json.load(Buffer(b'[false, true, 1, -2]'))) +print(json.load(Buffer(b'{"a":true}'))) diff --git a/tests/extmod/ujson_load_readinto.py.exp b/tests/extmod/ujson_load_readinto.py.exp new file mode 100644 index 000000000..f8c3c693b --- /dev/null +++ b/tests/extmod/ujson_load_readinto.py.exp @@ -0,0 +1,4 @@ +None +abcde +[False, True, 1, -2] +{'a': True} |
