Wednesday, December 7, 2011

Apache Thrift: Converting from simple JSON

Since I haven't seen this code posted anywhere on the internet, I figured this would help a few people out:


#include <jsoncpp/json/json.h>
#include <transport/TBufferTransports.h>
#include <protocol/TJSONProtocol.h>


template<class T>
void DeserializeThriftObjFromJson(std::string jsonStr, T &obj) {

  boost::shared_ptr<apache::thrift::transport::TMemoryBuffer> membuffer(new apache::thrift::transport::TMemoryBuffer(sizeof(T)));
  membuffer->write(((const uint8_t*)jsonStr.c_str()), jsonStr.length());

  apache::thrift::protocol::TJSONProtocol proto(membuffer);

  obj.read(&proto);

}

template<class T>
void SerializeThriftObjToJson(T &obj, std::string &jsonStr) {

  boost::shared_ptr<apache::thrift::transport::TMemoryBuffer> membuffer(new apache::thrift::transport::TMemoryBuffer(sizeof(T)));
  apache::thrift::protocol::TJSONProtocol proto(membuffer);
  obj.write(&proto);

  jsonStr = membuffer->getBufferAsString();
}


template<class T>
static void SerializeThriftVectorToJson(std::vector<T> &thriftVec, std::string &jsonStr) {
  // Since the vector object doesn't support ->write / ->read, we have to
  // put the data in some other serialized format.  JSON is the easiest.
  Json::Value jsonVec;
  for (size_t i = 0; i < thriftVec.size(); i++) {
   std::string thriftJson;
   SerializeThriftObjToJson< T >(thriftVec[i], thriftJson);
   jsonVec[i] = thriftJson;
  }

  jsonStr = jsonVec.write();
}

template<class T>
static void DeserializeThriftVectorFromJson(std::string &jsonStr, std::vector< T > &thriftVec) {

  Json::Value json = Json::Parse(jsonStr);
  Json::Value::UInt size = json.size();

  for (Json::Value::UInt i = 0; i < size; i++) {
    std::string thriftJson = json[i].asString();
    T e;
    DeserializeThriftObjFromJson< T >(thriftJson, e);
    thriftVec.push_back(e);
  }

}

1 comment:

prannoy said...

Can you help me serialize and deserialize to and from json to thrift object