Skip to main content

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:

  1. Class in thrift gen-py use thrift_spec() to deserialize, but the Enum object is simply treated as I32, while in json, it's stored as string of name.
  2. set is not json serializable in standard json, we can treat {'a', 'b', ..} as {None: ['a', 'b', ...]} for distinction.

How to:

  1. Parsing thrift ast by ptsd
  2. Deserializing thrift object by walking through ast recursively.

Todo:

  • [ ] learn ply from ptsd

Others

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for thrift-converter, version 0.0.1
Filename, size File type Python version Upload date Hashes
Filename, size thrift_converter-0.0.1-py3-none-any.whl (5.6 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size thrift_converter-0.0.1.tar.gz (3.4 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page