英文:
How to deserialize repeated message of one of message in C++ protobuf
问题
我有一个protobuf消息定义如下:
message TestWordOne {
uint32 word = 1;
}
message TestWordTwo {
repeated TestWordOne words = 1;
}
message TestMessage {
oneof payload {
TestWordTwo test_data = 1;
<some more stuff here>
}
}
现在,我可以按以下方式进行序列化:
TestMessage sentMessage;
TestMessage recvMessage;
TestWordTwo* data_test(new TestWordTwo);
for (int i=0; i<5; i++)
{
TestWordOne* oneWord = data_test->add_words();
oneWord->set_word(i);
}
然后使用outputstream进行序列化:
sentMessage.SerializeToOstream(&outputStream)
然而,当我执行以下操作:
recvMessage.ParseFromIstream(&outputStream)
我不知道如何逐个获取recVMessage中的单词。有什么建议吗?
我尝试使用Descriptor和Reflection,但所有可用的示例都没有说明如何在我的情况下继续。
英文:
I have a protobuf such as:
message TestWordOne {
uint32 word = 1;
}
message TestWordTwo {
repeated TestWordOne words = 1;
}
message TestMessage {
oneof payload {
TestWordTwo test_data = 1;
<some more stuff here>
}
}
Now, I can serialize in such a way:
TestMessage sentMessage;
TestMessage recvMessage;
TestWordTwo* data_test(new TestWordTwo);
for (int i=0; i<5; i++)
{
TestWordOne* oneWord = data_test->add_words();
oneWord->set_word(i);
}
Then serialize it with outputstream
sentMessage.SerializeToOstream(&outputStream)
However, I when I
recvMessage.ParseFromIstream(&outputStream)
I have no idea on how to get words from recVMessage, one by one. Any tips?
I tried using Descriptor and Reflection, but all available examples do not specify on how to proceed in my case.
答案1
得分: 1
在*.pb.h文件中很容易找到:
for (int i = 0; i < 5; i++) {
const TestWordOne& oneWord = data_test->words(i);
TestWordOne* oneWordMutable = data_test->mutable_words(i);
}
英文:
It's easy to find in *.pb.h:
for (int i=0; i<5; i++) {
const TestWordOne& oneWord = data_test->words(i);
TestWordOne* oneWordMutable = data_test->mutable_words(i);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论