You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@thrift.apache.org by "Chan, Jack" <ja...@flukenetworks.com> on 2013/10/30 02:23:00 UTC

serializing binary data in C++

Hi,

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(binaryProtcol);
    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.

Am I understanding everything correctly (probably not cuz it's not working)? What's the correct way to serialize an object? Pleeeeeaase edify me. Here is the stackoverflow link: http://stackoverflow.com/questions/19672155/apache-thrift-serialization-in-c

Thanks,
Jack


Re: serializing binary data in C++

Posted by Randy Abernethy <ra...@gmail.com>.
Hello Jack,

The get() method is a member of boost (and C++11) shared_ptr, the proto
looks something like this:

element_type * get
<http://www.boost.org/doc/libs/1_53_0/libs/smart_ptr/shared_ptr.htm#get>()
const; // never throws


Shared pointers imply shared ownership of the target object, making
implicit conversion to uncontrolled raw pointers something you want to
do explicitly.


Best,

Randy




On Wed, Oct 30, 2013 at 10:05 AM, Chan, Jack <ja...@flukenetworks.com>wrote:

> Hi Randy,
>
> Thanks for your help!
>
> 1. Yeah, I did mean obj.write(binaryProtocol); to be
> obj.write(protocolOut). I was trying different stuff, which resulted in a
> copy/paste error.
>
> 2. The protocolOut.get() did the trick. There's a thing I don't
> understand. I was looking at the source files before, particularly at
> TBinaryProtocol.h and TProtocol.h and did not see where the .get() method
> was defined. Where is the definition located?
>
> -Jack
>
> -----Original Message-----
> From: Randy Abernethy [mailto:randy.abernethy@gmail.com]
> Sent: Tuesday, October 29, 2013 7:34 PM
> To: user@thrift.apache.org
> Subject: Re: serializing binary data in C++
>
> Hello Jack,
>
> These two lines of code are the issue:
>
>     shared_ptr<TBinaryProtocol> protocolOut(new
> TBinaryProtocol(transportOut));
>     obj.write(binaryProtcol);
>
>
> 1. I take it that the obj.write(binaryProtcol); is meant to be: obj.write(
> protocolOut);
>
> 2. The problem with the write is that the Thrift generated write function
> wants a TProtocol * If you change your code to the following I think you
> will have success:
>
>     obj.write(protocolOut.get());
>
> Best,
> Randy
>
>
> On Tue, Oct 29, 2013 at 6:23 PM, Chan, Jack <jack.chan@flukenetworks.com
> >wrote:
>
> > Hi,
> >
> > 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(binaryProtcol);
> >     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::pr
> > otocol::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.
> >
> > Am I understanding everything correctly (probably not cuz it's not
> > working)? What's the correct way to serialize an object? Pleeeeeaase
> > edify me. Here is the stackoverflow link:
> > http://stackoverflow.com/questions/19672155/apache-thrift-serializatio
> > n-in-c
> >
> > Thanks,
> > Jack
> >
> >
>
>

RE: serializing binary data in C++

Posted by "Chan, Jack" <ja...@flukenetworks.com>.
Hi Randy,

Thanks for your help!

1. Yeah, I did mean obj.write(binaryProtocol); to be obj.write(protocolOut). I was trying different stuff, which resulted in a copy/paste error.

2. The protocolOut.get() did the trick. There's a thing I don't understand. I was looking at the source files before, particularly at TBinaryProtocol.h and TProtocol.h and did not see where the .get() method was defined. Where is the definition located?

-Jack

-----Original Message-----
From: Randy Abernethy [mailto:randy.abernethy@gmail.com] 
Sent: Tuesday, October 29, 2013 7:34 PM
To: user@thrift.apache.org
Subject: Re: serializing binary data in C++

Hello Jack,

These two lines of code are the issue:

    shared_ptr<TBinaryProtocol> protocolOut(new TBinaryProtocol(transportOut));
    obj.write(binaryProtcol);


1. I take it that the obj.write(binaryProtcol); is meant to be: obj.write( protocolOut);

2. The problem with the write is that the Thrift generated write function wants a TProtocol * If you change your code to the following I think you will have success:

    obj.write(protocolOut.get());

Best,
Randy


On Tue, Oct 29, 2013 at 6:23 PM, Chan, Jack <ja...@flukenetworks.com>wrote:

> Hi,
>
> 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(binaryProtcol);
>     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::pr
> otocol::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.
>
> Am I understanding everything correctly (probably not cuz it's not 
> working)? What's the correct way to serialize an object? Pleeeeeaase 
> edify me. Here is the stackoverflow link:
> http://stackoverflow.com/questions/19672155/apache-thrift-serializatio
> n-in-c
>
> Thanks,
> Jack
>
>


Re: serializing binary data in C++

Posted by Randy Abernethy <ra...@gmail.com>.
Hello Jack,

These two lines of code are the issue:

    shared_ptr<TBinaryProtocol> protocolOut(new
TBinaryProtocol(transportOut));
    obj.write(binaryProtcol);


1. I take it that the obj.write(binaryProtcol); is meant to be: obj.write(
protocolOut);

2. The problem with the write is that the Thrift generated write function
wants a TProtocol *
If you change your code to the following I think you will have success:

    obj.write(protocolOut.get());

Best,
Randy


On Tue, Oct 29, 2013 at 6:23 PM, Chan, Jack <ja...@flukenetworks.com>wrote:

> Hi,
>
> 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(binaryProtcol);
>     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.
>
> Am I understanding everything correctly (probably not cuz it's not
> working)? What's the correct way to serialize an object? Pleeeeeaase edify
> me. Here is the stackoverflow link:
> http://stackoverflow.com/questions/19672155/apache-thrift-serialization-in-c
>
> Thanks,
> Jack
>
>