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