python利用thriftpy2将json、struct、obj互相转换

github thriftpy2

import thriftpy2.protocol.json as proto

def test_struct_to_json():
    obj = TItem(id=13, phones=["5234", "12346456"])
    json = proto.struct_to_json(obj)
    # 返回dict类型,str(json)转字符串
    assert {"id": 13, "phones": ["5234", "12346456"]} == json

def test_struct_to_obj():
    json = {"id": 13, "phones": ["5234", "12346456"]}
    obj = TItem()

    obj = proto.struct_to_obj(json, obj)

    assert obj.id == 13 and obj.phones == ["5234", "12346456"]
posted @ 2021-12-07 19:42  致林  阅读(643)  评论(0编辑  收藏  举报