C++. ParseFromString doesn't fill the object with data

564 views
Skip to first unread message

Boris Pitel

unread,
May 29, 2019, 10:32:23 AM5/29/19
to Protocol Buffers
Hello team,
I am new to C++ protobuf. library.
I experience following difficulty.
I have an pObj, i serialize it using SerializeToString, then as a test i try to deserialize the string using ParseFromString. I expect that after deserialization the object will contain the same data as original pObj. It doesn't happen. The new object contains nulls. The ParseFromString return true. Here is the pseudo-code:

string body;
int n1 = pObj->_objectSetType.ByteSizeLong();
result = pObj->_objectSetType.SerializeToString(&body);
PriceTicObjectSet* pnewObj = new PriceTicObjectSet;
result = pnewObj->_objectSetType.ParseFromString(body);

string dump1 = toString(pObj);
printf("Dump1:\n%s\n", dump1.c_str());
string dump2 = toString(pnewObj);
printf("Dump2:\n%s\n", dump2.c_str());


Again, I expect that after deserialization objects pObj and pnewObj contain same data. 
This issue puzzles me and it is really important to solve it.

Please can somebody help me - probably I am doing something wrong.

Thank you 

Boris Pitel

Adam Cozzette

unread,
May 29, 2019, 10:40:02 AM5/29/19
to Boris Pitel, Protocol Buffers
What does your toString() function do?

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/dfae067f-71ec-4550-b18e-aa8aabab0af2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Boris Pitel

unread,
May 29, 2019, 11:04:49 AM5/29/19
to Protocol Buffers
toString is just a function which dumps the object ( PriceTicObjectSet ) - PriceTicObjectSet is just a very thin wrapper around protobuf class. I use toString to see the content of the message.

-Boris


On Wednesday, May 29, 2019 at 10:40:02 AM UTC-4, Adam Cozzette wrote:
What does your toString() function do?

On Wed, May 29, 2019 at 7:32 AM Boris Pitel <boris...@gmail.com> wrote:
Hello team,
I am new to C++ protobuf. library.
I experience following difficulty.
I have an pObj, i serialize it using SerializeToString, then as a test i try to deserialize the string using ParseFromString. I expect that after deserialization the object will contain the same data as original pObj. It doesn't happen. The new object contains nulls. The ParseFromString return true. Here is the pseudo-code:

string body;
int n1 = pObj->_objectSetType.ByteSizeLong();
result = pObj->_objectSetType.SerializeToString(&body);
PriceTicObjectSet* pnewObj = new PriceTicObjectSet;
result = pnewObj->_objectSetType.ParseFromString(body);

string dump1 = toString(pObj);
printf("Dump1:\n%s\n", dump1.c_str());
string dump2 = toString(pnewObj);
printf("Dump2:\n%s\n", dump2.c_str());


Again, I expect that after deserialization objects pObj and pnewObj contain same data. 
This issue puzzles me and it is really important to solve it.

Please can somebody help me - probably I am doing something wrong.

Thank you 

Boris Pitel

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prot...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/dfae067f-71ec-4550-b18e-aa8aabab0af2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

On Wednesday, May 29, 2019 at 10:40:02 AM UTC-4, Adam Cozzette wrote:
What does your toString() function do?

On Wed, May 29, 2019 at 7:32 AM Boris Pitel <boris...@gmail.com> wrote:
Hello team,
I am new to C++ protobuf. library.
I experience following difficulty.
I have an pObj, i serialize it using SerializeToString, then as a test i try to deserialize the string using ParseFromString. I expect that after deserialization the object will contain the same data as original pObj. It doesn't happen. The new object contains nulls. The ParseFromString return true. Here is the pseudo-code:

string body;
int n1 = pObj->_objectSetType.ByteSizeLong();
result = pObj->_objectSetType.SerializeToString(&body);
PriceTicObjectSet* pnewObj = new PriceTicObjectSet;
result = pnewObj->_objectSetType.ParseFromString(body);

string dump1 = toString(pObj);
printf("Dump1:\n%s\n", dump1.c_str());
string dump2 = toString(pnewObj);
printf("Dump2:\n%s\n", dump2.c_str());


Again, I expect that after deserialization objects pObj and pnewObj contain same data. 
This issue puzzles me and it is really important to solve it.

Please can somebody help me - probably I am doing something wrong.

Thank you 

Boris Pitel

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prot...@googlegroups.com.

Adam Cozzette

unread,
May 29, 2019, 12:15:02 PM5/29/19
to Boris Pitel, Protocol Buffers
Could you provide some more details on what that function does and what its outputting? Without that it is hard to know what the problem is.

To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.

Boris Pitel

unread,
May 29, 2019, 12:48:41 PM5/29/19
to Protocol Buffers
Ok, let me explain. I have a class let's call it PriceTicObjectSet. This class is a wrapper around protobuf class. This PriceTicObjectSet has member variables which are directly point to protobuf class variables - just setters and getters. Function toString is for convenience only, all it does it is dumps the content of  the instance of PriceTicObjectSet. Its implementation is like :
.....
if ( name_internal != NULL ) toReturn += ("name=" + name_internal->value() );
....

in other words toString() is just not essential here. We could just use debugger and see that after ParseFromString the object doesn't change at all.

Sorry for not being clear.

Thanks,

-Boris

Adam Cozzette

unread,
May 29, 2019, 12:57:49 PM5/29/19
to Boris Pitel, Protocol Buffers
So after parsing, the message is just empty? The first thing I would do is double-check that SerializeToString() and ParseFromString() are returning true and also verify that the length of the serialized string is reasonable.

To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.

Boris Pitel

unread,
May 29, 2019, 1:01:11 PM5/29/19
to Protocol Buffers
Thank you, Adam!
Yes, both returns values are true, and size is very reasonable - 89. 
Any other ideas? :(
-Boris

Adam Cozzette

unread,
May 29, 2019, 1:12:35 PM5/29/19
to Boris Pitel, Protocol Buffers
What's the value of pnewObj->_objectSetType.ByteSizeLong() after the parse?

To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.

Boris Pitel

unread,
May 29, 2019, 1:37:25 PM5/29/19
to Protocol Buffers
89. The same as original object.
It is strange. Isn't it?

Adam Cozzette

unread,
May 29, 2019, 1:41:48 PM5/29/19
to Boris Pitel, Protocol Buffers
Hmm, that makes me think perhaps everything is working then. What is different about the messages in a way that you didn't expect?

To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.

Boris Pitel

unread,
May 29, 2019, 2:06:09 PM5/29/19
to Protocol Buffers
Adam, the new deserealized object doesn't contain ANY data which was in original object instance.

To put it simple 
1) we have obj
2) we make a string obj.SerializetoString ( & body)
3) We parse the string into new object : newobj.ParseFromString ( body). 
4) I expect obj == newobj. But it isn't so. newobj is not equal to obj ( in terms of content - field value)

Thanks,
Boris

Adam Cozzette

unread,
May 29, 2019, 2:13:40 PM5/29/19
to Boris Pitel, Protocol Buffers
Can you give an example of a specific field that has different values between the old and new messages? I suspect the problem might be that you're expecting them to be equal on a byte-for-byte comparison of their in-memory structure, and that will usually not be the case because the messages will contain things like pointers to submessages.

To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.

Boris Pitel

unread,
May 29, 2019, 2:24:58 PM5/29/19
to Protocol Buffers
example :
I initialize my fields to certain values like this:
pObj->PriceTicMIObject_public->set_ContentDelayed("True");
pObj->PriceTicMIObject_public->set_KeyExchangeSource("558");
pObj->PriceTicMIObject_public->set_TradingSymbol("BAC");
pObj->PriceTicMIObject_public->set_CUSIP("VaskaKushaetISpit");

after serialization and deserialization newObj has all this fields equal to NULL.

Thanks,

Boris

Adam Cozzette

unread,
May 29, 2019, 2:29:25 PM5/29/19
to Boris Pitel, Protocol Buffers
But in your previous example code the original protobuf message was pObj->_objectSetType so I don't see how that could affect pObj->PriceTicMIObject_public, which seems to be a separate class member. Is pObj->PriceTicMIObject_public also a protobuf message?

To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

To post to this group, send email to prot...@googlegroups.com.
Visit this group at https://groups.google.com/group/protobuf.

Boris Pitel

unread,
May 29, 2019, 3:02:15 PM5/29/19
to Protocol Buffers

PriceTicMIObject_public coinatins a pointer to protobuf. But may be in long run you are correct. I have to double check if I "restore" pointers correctly.

Thank you Adam very much. I will be back as soon as possible. I could send you my h file, but I am not sure you would enjoy looking at it.

-Boris
Reply all
Reply to author
Forward
0 new messages