{"action":"created","issue":{"url":"https://api.github.com/repos/eleme/thriftpy/issues/180","repository_url":"https://api.github.com/repos/eleme/thriftpy","labels_url":"https://api.github.com/repos/eleme/thriftpy/issues/180/labels{/name}","comments_url":"https://api.github.com/repos/eleme/thriftpy/issues/180/comments","events_url":"https://api.github.com/repos/eleme/thriftpy/issues/180/events","html_url":"https://github.com/eleme/thriftpy/issues/180","id":130374813,"number":180,"title":"Converter Between Json and Thrift Struct","user":{"login":"xuanyuanking","id":4833765,"avatar_url":"https://avatars.githubusercontent.com/u/4833765?v=3","gravatar_id":"","url":"https://api.github.com/users/xuanyuanking","html_url":"https://github.com/xuanyuanking","followers_url":"https://api.github.com/users/xuanyuanking/followers","following_url":"https://api.github.com/users/xuanyuanking/following{/other_user}","gists_url":"https://api.github.com/users/xuanyuanking/gists{/gist_id}","starred_url":"https://api.github.com/users/xuanyuanking/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xuanyuanking/subscriptions","organizations_url":"https://api.github.com/users/xuanyuanking/orgs","repos_url":"https://api.github.com/users/xuanyuanking/repos","events_url":"https://api.github.com/users/xuanyuanking/events{/privacy}","received_events_url":"https://api.github.com/users/xuanyuanking/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2016-02-01T14:25:05Z","updated_at":"2016-02-16T07:16:57Z","closed_at":"2016-02-16T07:16:57Z","body":"In my project I need a converter between standard json and thrift struct. So I implemented a generic one based on thriftpy. Like convert the json below:\r\n``` python\r\n{\r\n    \"test_simple_map\": {\r\n        \"a\": 1,\r\n        \"b\": 2\r\n    },\r\n    \"name\": \"abc\",\r\n    \"add_more\": {\r\n        \"more\": \"more\"\r\n    },\r\n    \"error_type\": \"SUCCESS\",\r\n    \"in_list\": [\r\n        {\r\n            \"l_name\": \"l_name1\"\r\n        },\r\n        {\r\n            \"l_name\": \"l_name2\"\r\n        },\r\n        {\r\n            \"l_name\": \"l_name3\"\r\n        }\r\n    ],\r\n    \"test_struct_map\": {\r\n        \"a\": {\r\n            \"value\": \"abc\"\r\n        },\r\n        \"b\": {\r\n            \"value\": \"abc1\"\r\n        }\r\n    },\r\n    \"num\": 128,\r\n    \"simple_list\": [\r\n        1,\r\n        2,\r\n        3,\r\n        4\r\n    ],\r\n    \"desc\": \"for_lyj_test\"\r\n}\r\n```\r\nTo the thrift struct:\r\n``` python\r\nmeta(test_simple_map={u'a': 1, u'b': 2}, name=u'abc', add_more=in_meta(in_list=None, more=u'more'), error_type=1, in_list=[list_test(l_name=u'l_name1'), list_test(l_name=u'l_name2'), list_test(l_name=u'l_name3')], test_struct_map={u'a': test_map_value(value=u'abc'), u'b': test_map_value(value=u'abc1')}, num=128, simple_list=[1, 2, 3, 4], desc=u'for_lyj_test')\r\n```\r\nAnd also convert back to json. details: https://github.com/xuanyuanking/thrift_json_convertor\r\n\r\nIf you think it's necessary, I'm glad to give a PR about this.\r\n\r\nThanks!"},"comment":{"url":"https://api.github.com/repos/eleme/thriftpy/issues/comments/184556161","html_url":"https://github.com/eleme/thriftpy/issues/180#issuecomment-184556161","issue_url":"https://api.github.com/repos/eleme/thriftpy/issues/180","id":184556161,"user":{"login":"lxyu","id":678685,"avatar_url":"https://avatars.githubusercontent.com/u/678685?v=3","gravatar_id":"","url":"https://api.github.com/users/lxyu","html_url":"https://github.com/lxyu","followers_url":"https://api.github.com/users/lxyu/followers","following_url":"https://api.github.com/users/lxyu/following{/other_user}","gists_url":"https://api.github.com/users/lxyu/gists{/gist_id}","starred_url":"https://api.github.com/users/lxyu/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lxyu/subscriptions","organizations_url":"https://api.github.com/users/lxyu/orgs","repos_url":"https://api.github.com/users/lxyu/repos","events_url":"https://api.github.com/users/lxyu/events{/privacy}","received_events_url":"https://api.github.com/users/lxyu/received_events","type":"User","site_admin":false},"created_at":"2016-02-16T07:16:57Z","updated_at":"2016-02-16T07:16:57Z","body":"@xuanyuanking Thanks for you time and contribution, however I also think it's not very suitable for merge due to these two reason:\r\n\r\n1. Thrift struct is more flexible than json, for example json dict key type is limit to string. So there do not exist a simple mapping between json and thrift without introducing `protocol`.\r\n2. The converter itself can be more generic, you can implement a converter between all protocols (build in or customized protocols) with thrift struct.\r\n\r\nThere're 2 helper function in the `thriftpy.utils` that may help in implementing the converter. I used your thrift file to make the following example:\r\n\r\n```python\r\ntest_thrift = thriftpy.load(\"test.thrift\", module_name=\"test_thrift\")\r\ntest_thrift_obj = meta(add_more=in_meta(in_list=None, more='more'), error_type=1, test_struct_map={'a': test_map_value(value='abc'), 'b': test_map_value(value='abc1')}, simple_list=[1, 2, 3, 4], test_simple_map={'a': 1, 'b': 2}, num=128, in_list=[list_test(l_name='l_name1'), list_test(l_name='l_name2'), list_test(l_name='l_name3'), list_test(l_name='l_name4')], name='abc', desc='for_lyj_test')\r\n\r\nserialize(test_thrift_obj, proto_factory=TJSONProtocolFactory())\r\n# b'\\x00\\x00\\x01\\xc2{\"metadata\": {\"version\": 1}, \"payload\": {\"add_more\": {\"more\": \"more\"}, \"test_struct_map\": [{\"value\": {\"value\": \"abc\"}, \"key\": \"a\"}, {\"value\": {\"value\": \"abc1\"}, \"key\": \"b\"}], \"in_list\": [{\"l_name\": \"l_name1\"}, {\"l_name\": \"l_name2\"}, {\"l_name\": \"l_name3\"}, {\"l_name\": \"l_name4\"}], \"name\": \"abc\", \"desc\": \"for_lyj_test\", \"error_type\": 1, \"simple_list\": [1, 2, 3, 4], \"test_simple_map\": [{\"value\": 1, \"key\": \"a\"}, {\"value\": 2, \"key\": \"b\"}], \"num\": 128}}'\r\n```\r\n\r\nIt produce a similar json string that can be `deserialize` back to thrift struct. If you want to make it more cleaner to be readable in json lib, it's easy to customize a new json protocol to fit the needs."}}