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


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)

Uploaded Source

Built Distribution

thrift_converter-0.0.1-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page