def test_string_with_utf8_bom(): # see #18958 bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8') with pytest.raises(ValueError, match="Expected object or value") as e: sd_ujson.loads(bom_json) with pytest.raises(ValueError, match="Expected object or value") as e: sd_ujson.load(StringIO(bom_json)) # make sure that the BOM is not detected in the middle of a string bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8')) assert sd_ujson.loads(bom_in_str) == '\ufeff' assert sd_ujson.json.load(StringIO(bom_in_str)) == '\ufeff'
def write_then_read(obj): with TemporaryDirectory() as tmpdir: tmpfile = pathlib.Path(tmpdir) / "output.json" with open(tmpfile, "w") as fp: sd_ujson.dump(obj, fp) with open(tmpfile, "r") as fp: return sd_ujson.load(fp)
def test_load_file_like_object(): class FileLike: def read(self): try: self.end except AttributeError: self.end = True return "[1,2,3,4]" f = FileLike() assert [1, 2, 3, 4] == sd_ujson.load(f)
def test_load_file_args_error(): with pytest.raises(TypeError): sd_ujson.load("[]")
def test_load_file(): f = six.StringIO("[1,2,3,4]") assert [1, 2, 3, 4] == sd_ujson.load(f)