如何将节俭对象转换为可读的字符串并将其转换回? [英] How to convert thrift objects to readable string and convert it back?

查看:172
本文介绍了如何将节俭对象转换为可读的字符串并将其转换回?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,我们需要在单元测试中创建一些节俭的对象.我们可以通过使用Java代码手动创建对象来做到这一点,例如:

Sometimes, we need to create some thrift objects in unit tests. We can do it by manually create object using Java code, like:

MyObj myObj = new MyObj();
myObj.setName("???");
myObj.setAge(111);

但这不方便.我正在寻找一种创建具有一些可读文本的对象的方法.

But which is not convenient. I'm looking for a way to create objects with some readable text.

我们可以使用TSimpleJSONProtocol将节俭对象转换为JSON,并获得可读性强的JSON字符串,例如:

We can convert thrift objects to JSON with TSimpleJSONProtocol, and get very readable JSON string, like:

{ "name": "???", "age": 111 }

但是问题是TSimpleJSONProtocol是只写的,节俭无法将其读回以构造MyObj的实例.

But the problem is TSimpleJSONProtocol is write only, thrift can't read it back to construct an instance of MyObj.

尽管有一个TJSONProtocol支持序列化和反序列化,但是生成的JSON不可读,但它使用了非常简化的JSON格式,并且大多数字段名都丢失了.在测试中构造它不方便.

Although there is a TJSONProtocol which supports to serialize and deserialize, but the generated JSON is not readable, it uses a very simplified JSON format and most of the field names are missing. Not convenient to construct it in tests.

有什么方法可以将节俭对象转换为可读的字符串,也可以将其转换回?如果TSimpleJSONProtocol支持转换回去,那正是我要寻找的

Is there any way to convert thrift objects to readable string and also can convert it back? If TSimpleJSONProtocol supports converting back, which is just what I'm looking for

推荐答案

Thrift的主要目标是提供高效的序列化和RPC机制.您想要的是(至少部分地)与此相反的东西.人类可读的数据结构和机器处理效率在很大程度上与目标相抵触,而Thrift则更喜欢后者.

The main goal of Thrift is to provide efficient serialization and RPC mechanisms. What you want is something that is - at least partially - contrary to that. Human-readable data structures and machine processing efficiency are to a good extent conflicting goals, and Thrift favors the latter over the former.

您已经发现了有关TSimpleJsonTJson协议及其优缺点的信息,因此没有太多要添加的内容.剩下唯一要说的是:Thrift的协议/传输堆栈非常简单.

You already found out about the TSimpleJson and TJson protocols and about their pros and cons, so there is not much to add. The only thing that is left to say is this: the protocol/transport stack of Thrift is simple enough.

这种简单性使您可以根据您的特定需求添加另一个协议,而无需太多或过于复杂的工作.一个人甚至可能会在短时间内编写XML协议(如果有人真的想要这样的bloatware).

This simplicity makes it possible to add another protocol based on your specific needs without much or overly complicated work. One could probably even write an XML protocol (if anyone really wants such bloatware) in short time.

唯一的警告,尤其是针对您的具体情况,是 Thrift需要字段ID来反序列化数据的事实.因此,您需要将它们存储在数据中,或者需要一些其他机制能够根据字段和结构名称检索该字段ID.

The only caveat, especially vis-à-vis your specific case, is the fact that Thrift needs the field ID to deserialize the data. So you either need to store them in the data, or you need some other mechanism which is able to retrieve that field ID based on the field and structure names.

这篇关于如何将节俭对象转换为可读的字符串并将其转换回?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆