Thrift converting tools: json2thrift, thrift2json
Project description
thrift_converter
A python implemented tool for converting between json and thrift.
Install
pip install thrift-converter
Why build this tool:
- Class in thrift gen-py use
thrift_spec()
to deserialize, but theEnum
object is simply treated asI32
, while in json, it's stored as string of name. set
is not json serializable in standard json, we can treat{'a', 'b', ..}
as{None: ['a', 'b', ...]}
for distinction.
How to:
- Parsing thrift ast by ptsd
- Deserializing thrift object by walking through ast recursively.
Todo:
- learn
ply
from ptsd
Others
- introspection-of-thrift-enums-in-python discussed end with waiting for thrift upgrade(support for python enums)
- thrift_json_decoder in twitter.common can't handle with Enum
- thrift_json_convertor based on thriftpy too heavy
Usage
example: demo
import json
from thrift_converter import JsonThriftConverter, ThriftJsonConverter
thrift_file = 'demo.thrift'
object_name = 'Group'
json2thrift = JsonThriftConverter(thrift_file).convert
thrift2json = ThriftJsonConverter(thrift_file).convert
obj = gen_fake_obj()
# Group(id=-1, worker_map={999: Worker(id=999, tasks=[Task(id=727, status=0, action=888, valid=True, msgs={'msg'})])})
js = thrift2json(obj, object_name)
js_str = json.dumps(js)
# {"id": -1, "worker_map": {"999": {"id": 999, "tasks": [{"id": 727, "status": 0, "action": "UP", "valid": true, "msgs": {"null": ["msg"]}}]}}}
obj = json2thrift(json.loads(js_str), object_name)
# Group(id=-1, worker_map={999: Worker(id=999, tasks=[Task(id=727, status=0, action=888, valid=True, msgs={'msg'})])})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
thrift_converter-0.0.1.tar.gz
(3.4 kB
view hashes)
Built Distribution
Close
Hashes for thrift_converter-0.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c846d16cb4d29cf46020a8719d62346dd40ad9525e6c70b2027dd84c6f07046a |
|
MD5 | 50790323b063f5a8e3ed240c63af4521 |
|
BLAKE2b-256 | 2399395eddb0e8c57dd778b924325f11e193b4f6bd5f0cf953593474f36c0b25 |