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 theEnumobject is simply treated asI32, while in json, it's stored as string of name. setis 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
plyfrom 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file thrift_converter-0.0.1.tar.gz.
File metadata
- Download URL: thrift_converter-0.0.1.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7beb4a680cfa1544a5c904a0bbfd353700d35317bef72b2f8a186992f20a07ad
|
|
| MD5 |
f04dbe6e2722ad33b6f6db6d6f323e68
|
|
| BLAKE2b-256 |
5f221709755faccf79ca22643927471c67b63e68a836d28eeaed169c83b9b56b
|
File details
Details for the file thrift_converter-0.0.1-py3-none-any.whl.
File metadata
- Download URL: thrift_converter-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.30.0 CPython/3.7.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c846d16cb4d29cf46020a8719d62346dd40ad9525e6c70b2027dd84c6f07046a
|
|
| MD5 |
50790323b063f5a8e3ed240c63af4521
|
|
| BLAKE2b-256 |
2399395eddb0e8c57dd778b924325f11e193b4f6bd5f0cf953593474f36c0b25
|