2020-07-10 20:33:17 -04:00
|
|
|
import ujson as json
|
|
|
|
|
|
|
|
# Test that json can load from any object with readinto
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
2020-07-10 20:33:17 -04:00
|
|
|
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)
|
2021-03-15 09:57:36 -04:00
|
|
|
buf[:l] = self._data[self._i : end]
|
2020-07-10 20:33:17 -04:00
|
|
|
self._i += l
|
|
|
|
return l
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
|
|
|
|
print(json.load(Buffer(b"null")))
|
2020-07-10 20:33:17 -04:00
|
|
|
print(json.load(Buffer(b'"abc\\u0064e"')))
|
2021-03-15 09:57:36 -04:00
|
|
|
print(json.load(Buffer(b"[false, true, 1, -2]")))
|
2020-07-10 20:33:17 -04:00
|
|
|
print(json.load(Buffer(b'{"a":true}')))
|