Does someone have a Serialize sample for protobuf-net?  I'm using VS
2005/C#.

When I try Encoding.ASCII.GetBytes() and Encoding.ASCII.GetString(), I
get an exception "invalid wire type - 7". If I
Convert.FromBase64String, serialize/deserialize works but the message
is rejected by the server which is running on Linux and is expecting a
valid protobuf as a string.

        private string Serialize(OrderProto proto)
        {
            using (MemoryStream stream = new MemoryStream())
            {
                Serializer.Serialize<OrderProto>(stream, proto);
                byte[] data = new byte[stream.Length];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(data, 0, data.Length);
                return Convert.ToBase64String(data);
            }
        }

        private OrderProto TestDeserialize(string str)
        {
            byte[] data = Convert.FromBase64String(str);
            MemoryStream stream = new MemoryStream(data);
            OrderProto proto = Serializer.Deserialize<OrderProto>
(stream);
            return proto;
        }

OrderProto proto = p.GetOrderProto(order.OrderID);
string protoStr = this.Serialize(proto); // but as Base64, this
protobuf is rejected by the server
OrderProto test = TestDeserialize(protoStr);

Another thing I noticed is that if I re-serialize a protobuf I
received from the server, the resulting byte array is roughly 10 bytes
shorter than when came in. But the deserialized class instance looks
correct.

                    OrderProto order =
Serializer.Deserialize<OrderProto>(new MemoryStream(contentBytes));
                    MemoryStream ms = new MemoryStream();
                    Serializer.Serialize<OrderProto>(ms, order);
                    ms.Seek(0, SeekOrigin.Begin);
                    byte[] buffer = ms.ToArray();
                    OrderProto test =
Serializer.Deserialize<OrderProto>(ms);

Thanks
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to protobuf@googlegroups.com
To unsubscribe from this group, send email to 
protobuf+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to