Fix stream version and add basic readinto test
This commit is contained in:
parent
734661e79c
commit
372bcf8a95
|
@ -127,6 +127,8 @@ STATIC mp_obj_t _mod_ujson_load(mp_obj_t stream_obj, bool return_first_json) {
|
|||
stream_p = mp_get_stream_raise(stream_obj, MP_STREAM_OP_READ);
|
||||
s.stream_obj = stream_obj;
|
||||
s.read = stream_p->read;
|
||||
s.errcode = 0;
|
||||
s.cur = 0;
|
||||
}
|
||||
|
||||
JSON_DEBUG("got JSON stream\n");
|
||||
|
|
|
@ -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}')))
|
|
@ -0,0 +1,4 @@
|
|||
None
|
||||
abcde
|
||||
[False, True, 1, -2]
|
||||
{'a': True}
|
Loading…
Reference in New Issue