Пример #1
0
def test_json_roundtrip(context, path_or_fileobj, encoding):
    context = Context(context.objects, context.properties, context.bools)
    assert 'lattice' not in context.__dict__

    is_fileobj = hasattr(path_or_fileobj, 'seek')
    kwargs = {'encoding': encoding} if encoding is not None else {}

    context.tojson(path_or_fileobj, ignore_lattice=True, **kwargs)
    if is_fileobj:
        path_or_fileobj.seek(0)
    assert 'lattice' not in context.__dict__

    deserialized = Context.fromjson(path_or_fileobj, **kwargs)
    if is_fileobj:
        path_or_fileobj.seek(0)
    assert 'lattice' not in deserialized.__dict__

    assert deserialized == context

    assert isinstance(context.lattice, Lattice)
    assert 'lattice' in context.__dict__

    context.tojson(path_or_fileobj, ignore_lattice=None, **kwargs)
    if is_fileobj:
        path_or_fileobj.seek(0)

    deserialized = Context.fromjson(path_or_fileobj, **kwargs)
    assert 'lattice' in deserialized.__dict__

    assert deserialized == context
    assert deserialized.lattice._eq(context.lattice)
Пример #2
0
def test_json_roundtrip_nonascii_context(nonascii_context, encoding):
    assert isinstance(nonascii_context.lattice, Lattice)
    assert 'lattice' in nonascii_context.__dict__
    kwargs = {'encoding': encoding} if encoding is not None else {}

    with io.StringIO() as f:
        nonascii_context.tojson(f, **kwargs)
        serialized = f.getvalue()
        f.seek(0)

        deserialized = Context.fromjson(f, **kwargs)

    assert 'lattice' in deserialized.__dict__
    assert deserialized == nonascii_context
    assert deserialized.lattice._eq(nonascii_context.lattice)

    assert '"Agneta F\\u00e4ltskog"' in serialized
    assert '"Bj\\u00f6rn Ulvaeus"' in serialized
    assert '"sch\\u00f6n"' in serialized