summaryrefslogtreecommitdiff
path: root/tests/extmod/ujson_load_readinto.py
blob: 001aa4fb2c72fd3a13512f60893a9fb377b020ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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}')))