6

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.

Davita
  • 8,928
  • 14
  • 67
  • 119
  • Have you tried using [`pickle`](https://docs.python.org/2/library/pickle.html#subclassing-unpicklers)? – BeetDemGuise Jun 16 '14 at 19:56
  • 1
    What have you tried? Are you actually using `protoobuf` on the server side? Did you try the [tutorial](https://developers.google.com/protocol-buffers/docs/pythontutorial)? – Lukas Graf Jun 16 '14 at 20:01
  • 1
    Yes I'm using protobuf. I can deserialize a single object without problem, but I have not idea how to do it with lists. That's why I couldn't try anything because I don't know. @DarinDouglass I'm new to python, but I guess pickle won't talk with .net. That's why I'm using protobuf – Davita Jun 16 '14 at 20:27
  • 2
    P.S. Yes I've seen the official tutorial, but it doesn't mention deserializing a list – Davita Jun 16 '14 at 20:28

2 Answers2

17

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()

Davita
  • 8,928
  • 14
  • 67
  • 119
2

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

AGo
  • 284
  • 2
  • 6
  • I'm using protobuf-net. I'm not aware how it serializes the data and/or where it puts the size of each message :( – Davita Jun 17 '14 at 09:13