4

I was wondering if it's possible to just use the serialization portions of apache thrift.

I basically have a custom communication protocol already created, and would like to pass a thrift object from my server (PHP) to the client (C#).

My home-grown communication protocol is basically JSON. I wanted to use thrift to construct the object properly on the PHP side, then send it through JSON where my C# app would then re-construct the object using the classes generated by thrift --gen csharp myfile.thrift

Does anyone know if this is possible or where I would get started? Would I overload TProtocol somehow?

BlueSun
  • 3,395
  • 1
  • 16
  • 33
Geesu
  • 5,318
  • 9
  • 38
  • 69

2 Answers2

2

Yes, you can use Thrift just for the object definitions. We do something similar, although instead of using JSON we use a binary format.

You could extend TProtocol, or you could just have your own class interrogate the Thrift object you have created and pull out and encode your fields. Then when you receive the JSON, simply create a new Thrift object from the values you have received.

Depending on how you implement it, and how good the client libraries are for C# and PHP (I haven't used either), you might be able to implement this in a way so you do not have to change your code when you change your schema, although you will still have to generate the Thrift objects and deploy them. Basically you walk through the objects and encode each item in JSON. We did this in Java.

andrewrjones
  • 1,680
  • 19
  • 24
1

For the C# client, you can use Thrift.Protocol.TJSONProtocol to read from your input stream as follows:

TBase object; // replace TBase here with your thrift generated type
TJSONProtocol jsonIn = new TJSONProtocol(new TStreamTransport(inputStream, null));
object.Read(jsonIn);

You should be able to generate the json in a similar way on the PHP side.

bobbymond
  • 81
  • 3