About Thrift: If you are not familiar with RPC or Thrift, you might want to have a look at https://thrift.apache.org/.
So, one problem I have heard is, “Hey, so we are publishing our Thrift APIs to third-party vendors, and we need it to be easy to use.” Now, you either:
- Expose the Thrift endpoints and coach your API consumers on Thrift and help them debug any issues.
- Maintain a library that is a wrapper around the different Thrift services for each language or platform you want to support.
- Create a way to manually convert between human-readable JSON to Thrift and vice versa, and maintain it for every single struct, service, and function.
If these options sound unappealing to you, you’re right—they can be a lot of work. But is there another way?
I will be using the following simple Thrift file in my article below:
See this code sample on GitHubNow, Thrift does support a JSONProtocol which transmits data as JSON between the client and server (this is what we use for our web front end). However, one look at the data being sent will tell you it’s not going to be possible to expose this to third parties:
See this code sample on GitHubAnd this is for a very simple call: login(email, password).
The Thrift JSON has a whole bunch of jargon it uses to parse out the exact call, like the types of each argument/field, the length of arrays/sets, the key number of each field in a struct, etc.
So we came up with a way to convert human-readable JSON to Thrift without:
- Adding a whole bunch of jargon.
- Doing any maintenance. Any changes to the Thrift definitions should automatically be usable.
An example of the same call using the new way:
See this code sample on GitHubMuch more readable!
The format is simple. The method key’s values should be the exact name of the method in the Thrift definition. The arguments object is the list of arguments. The keys inside the arguments object are again the same as the name of the function parameters in the Thrift definition.
Here is a sample response:
See this code sample on GitHubAn example of an API-defined Exception which was thrown:
See this code sample on GitHubNote, it can easily handle Structs (and structs embedded inside structs) too. Above, currentUser is a struct. In JSON it is simply a JSON object whose keys are the same as the field names inside the Thrift Struct definition.
Also, above err (the exception) is also a Struct since in Thrift exceptions are simply a special kind of struct.
Furthermore, note that the success object itself is a struct. It is an auto-generated struct whose name is usually <MethodName>_result (in this case login_result). You can see it if you ever peek into the generated code for any other language. It has two fields: result for a successful call and err for any API-defined error that was thrown.
Similarly, the arguments object is also an auto-generated Struct, usually called <MethodName>_args (in this case login_args). Its fields are simply the function arguments. Thus, this format follows very closely to how actual Thrift works.
There is a JSON generator for Thrift, which generates all the metadata of the:
- Services: name, list of functions
- Functions: name, argument list
- Structs: name, fields along with their names and types
Below is an example of the generated metadata for the Thrift file above:
See this code sample on GitHubI used this metadata inside a new Protocol. A Protocol is the way Thrift marshals and un-marshals data. The problem is that Protocols in Thrift are strongly typed. It needs to be able to pass up the exact type of field (or the element type of the array or the key and value type of the map) and other details like the size of the map or array. That’s why the Thrift JSONProtocol has so much jargon inside it. It carries exactly this information.
This new Protocol, the HumanReadableJsonProtocol, uses the JSON that the Client sends up along with the metadata from the Thrift JSON generator to figure out exactly what function was called, what arguments were sent, and what their exact types are.
For example, in the above call it will first look up the metadata for the login method. Then for each key inside the arguments object in the client JSON it will find the associated key in the arguments array in the metadata. Depending on the type, it will parse it out (if it is a struct, list, or map, it will keep recursing until it reaches a base type—int, string, boolean, double, etc.). For example, in this case it will look for an object whose “name” value is “email” in the metadata, see it has a type of String, and then verify that the value of “email” in the arguments object in the client JSON is also a String. Similarly for “password.”
This new HumanReadableJsonProtocol is still strongly typed. If you send up a key whose value doesn’t map correctly to the type defined in the metadata, it will throw an error.
Here is an error thrown in such a case, where I tried to pass a number instead of a string to the password field:
See this code sample on GitHubNote that this JSON has an exception key; the above ones had a result key followed by either success or error. This is similar to how Thrift treats the different response types. You either get back a Result or an Exception. Result means that the call succeeded and no error other than API-defined errors (the ones in the method signature) were thrown. Thus, Result has two fields: one for success, which has the successful result of the call, and one for error, which holds the API-defined error that was thrown. Exception holds any other error (including Thrift errors like corrupted data, unable to parse data, wrong types, etc.).
Since it is just a protocol (a way to marshal/un-marshal data), the Thrift-generated code using it will automatically take care of details like required or optional fields.
So, for example, if the request has a content-type of application/json, all you need to do is use the HumanReadableJsonProtocol to parse it; otherwise use whatever standard protocol you use. You will also need to pass in the directory where the generated JSON metadata lives.
Finally, it automatically uses any new changes to the Thrift definitions (as long as you also generate the JSON using the JSON generator).
Here is the source code to the project: HumanReadableJsonProtocol.
HumanReadableJsonProtocol Snippet in Ruby
Here is a snippet of the code that does the parsing in Ruby:
See this code sample on GitHubThanks to Adam Neary.










