Reputation: 5434
I’m having a lot of trouble serializing data. What am I doing wrong?
std::string serialize(ContactsList& obj, std::string filename) {
shared_ptr<TMemoryBuffer> transportOut(new TMemoryBuffer());
shared_ptr<TBinaryProtocol> protocolOut(new TBinaryProtocol(transportOut));
obj.write(protocolOut);
std::string serialized_string = transportOut->getBufferAsString();
return serialized_string;
}
This is the method I call from another method. I expect to get back a serialized binary string which I can write out to disk. Inside this serialize method, I create a TMemory buffer, then I wrap it in a TBinaryProtocol, and then the object’s write method, which will write itself into the memory buffer. Then, I get that buffer back as a string. I’d then write out the serialized string to disk.
I get this error:
error: no matching function for call to ‘addressbook::ContactsList::write(boost::shared_ptr<apache::thrift::protocol::TBinaryProtocolT<apache::thrift::transport::TTransport> >&)
As well as this note:
note: no known conversion for argument 1 from ‘boost::shared_ptr<apache::thrift::protocol::TBinaryProtocolT<apache::thrift::transport::TTransport> >’ to ‘apache::thrift::protocol::TProtocol*
I'm using Apache Thrift 1.0-dev, C++ 98 if these things make a difference.
Upvotes: 3
Views: 5043
Reputation: 584
In c++ you can use the TFileTransport and a Thrift protocol of your choice, i.e. TBinaryProtocol:
shared_ptr<TFileTransport> transport(new TFileTransport(filename));
shared_ptr<TBinaryProtocol> protocol(new TBinaryProtocol(transport));
yourObj.write(protocol.get()); // to write
or
yourObj.read(protocol.get()); // to read
to make sure the file exists you can use a open before:
open(filename.c_str(), O_CREAT|O_TRUNC|O_WRONLY, 0666); // create file
ps.: It's actually very similar in all other target languages.
Upvotes: 3
Reputation: 176
The ThriftObject.write() function expects an argument of type
apache::thrift::protocol::TProtocol*
i.e. a 'Raw' pointer of type TProtocol. The protocolOut object used here is of shared pointer of the type.
shared_ptr<TBinaryProtocol> protocolOut
The 'get()' method of shared_ptr gives the raw pointer being managed by the shared_ptr object. Hence using
obj.write(protocolOut.get());
should resolve the problem
Upvotes: 2