ujson_dump_iobase.py 664 B

1234567891011121314151617181920212223242526272829303132
  1. # test ujson.dump in combination with uio.IOBase
  2. try:
  3. import uio as io
  4. import ujson as json
  5. except ImportError:
  6. try:
  7. import io, json
  8. except ImportError:
  9. print('SKIP')
  10. raise SystemExit
  11. if not hasattr(io, 'IOBase'):
  12. print('SKIP')
  13. raise SystemExit
  14. # a user stream that only has the write method
  15. class S(io.IOBase):
  16. def __init__(self):
  17. self.buf = ''
  18. def write(self, buf):
  19. if type(buf) == bytearray:
  20. # uPy passes a bytearray, CPython passes a str
  21. buf = str(buf, 'ascii')
  22. self.buf += buf
  23. # dump to the user stream
  24. s = S()
  25. json.dump([123, {}], s)
  26. print(s.buf)