javajsonthriftthrift-protocol

How to convert thrift objects to readable string and convert it back?


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.

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

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

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

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.

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


Solution

  • 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.

    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.

    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.

    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.