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 details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

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

Uploaded Python 3

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

Hashes for thrift_converter-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7beb4a680cfa1544a5c904a0bbfd353700d35317bef72b2f8a186992f20a07ad
MD5 f04dbe6e2722ad33b6f6db6d6f323e68
BLAKE2b-256 5f221709755faccf79ca22643927471c67b63e68a836d28eeaed169c83b9b56b

See more details on using hashes here.

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

Hashes for thrift_converter-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c846d16cb4d29cf46020a8719d62346dd40ad9525e6c70b2027dd84c6f07046a
MD5 50790323b063f5a8e3ed240c63af4521
BLAKE2b-256 2399395eddb0e8c57dd778b924325f11e193b4f6bd5f0cf953593474f36c0b25

See more details on using hashes here.

Supported by

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