Davita
Davita

Reputation: 9114

Deserialize list of objects using protobuf

I'm building a C# server and python client app with socket communication. The server sends serialized list of objects to client, but I've got no idea (and couldn't find either) how to deserialize a list in python. Any help would be appreciated.

Upvotes: 7

Views: 12681

Answers (2)

Davita
Davita

Reputation: 9114

Allright, I found solution if anyone is interested. The trick is to create a new message type and add original one as repeated. Here's how

message TransactionPackets {
    repeated TransactionPacket packet = 1;
}

message TransactionPacket {
    required int32 trans_id = 1;
    required string user_id = 2;
    required int64 date = 3;
}

Now I can simply deserialize a list of objects by calling TransactionPackets.ParseFromString()

Upvotes: 19

AGo
AGo

Reputation: 304

Check this:

"The Protocol Buffer wire format is not self-delimiting, so protocol buffer parsers cannot determine where a message ends on their own. The easiest way to solve this problem is to write the size of each message before you write the message itself. When you read the messages back in, you read the size, then read the bytes into a separate buffer, then parse from that buffer."

https://developers.google.com/protocol-buffers/docs/techniques

Upvotes: 2

Related Questions