예제 #1
0
def write_then_read(obj, tmpdir: PathPlus):
    tmpfile = tmpdir / "output.json"

    with open(tmpfile, 'w', encoding="UTF-8") as fp:
        sdjson.dump(obj, fp)

    with open(tmpfile, encoding="UTF-8") as fp:
        return sdjson.load(fp)
예제 #2
0
def write_then_read(obj):
    with TemporaryDirectory() as tmpdir:
        tmpfile = pathlib.Path(tmpdir) / "output.json"

        with open(tmpfile, "w") as fp:
            sdjson.dump(obj, fp)

        with open(tmpfile, "r") as fp:
            return sdjson.load(fp)
예제 #3
0
def test_string_with_utf8_bom():
    import sys

    # see #18958
    bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8')
    with pytest.raises(sdjson.JSONDecodeError) as e:
        sdjson.loads(bom_json)
    # TODO:
    if sys.version_info.major >= 3 and sys.version_info.minor == 7:
        assert 'BOM' in str(e)

    with pytest.raises(sdjson.JSONDecodeError) as e:
        sdjson.load(StringIO(bom_json))
    # TODO:
    if sys.version_info.major >= 3 and sys.version_info.minor == 7:
        assert 'BOM' in str(e)

    # 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 sdjson.loads(bom_in_str) == '\ufeff'
    assert sdjson.load(StringIO(bom_in_str)) == '\ufeff'
예제 #4
0
def test_object_pairs_hook() -> None:
    s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
    p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), ("qrt", 5),
         ("pad", 6), ("hoy", 7)]
    assert sdjson.loads(s, object_pairs_hook=lambda x: x) == p
    assert sdjson.load(StringIO(s), object_pairs_hook=lambda x: x) == p
    od = sdjson.loads(s, object_pairs_hook=OrderedDict)
    assert od == OrderedDict(p)
    assert type(od) == OrderedDict
    # the object_pairs_hook takes priority over the object_hook
    assert sdjson.loads(s,
                        object_pairs_hook=OrderedDict,
                        object_hook=lambda x: None) == OrderedDict(p)
    # check that empty object literals work (see #17368)
    assert sdjson.loads("{}", object_pairs_hook=OrderedDict) == OrderedDict()
    assert sdjson.loads('{"empty": {}}',
                        object_pairs_hook=OrderedDict) == OrderedDict([
                            ("empty", OrderedDict())
                        ])