I succeed in serializing instances of the following class but when I try to deserialize right after I get the following error message: " Invalid field in source data: 0".
I have no clue what it refers to because I find below class straight forward. I just updated protobuf-net version to 2.00.614 (runtime version: 2.0.50727).
Any idea whether I am possibly overlooking something trivial?
[ProtoContract]
public class TimeSeriesProperties
{
[ProtoMember(1)]
public string TimeSeriesName { get; private set; }
[ProtoMember(2)]
public string FileName { get; private set; }
[ProtoMember(3)]
public string TemplateName { get; private set; }
[ProtoMember(4)]
public int PacketLength { get; private set; }
[ProtoMember(5)]
public long FileSizeBytes { get; set; }
[ProtoMember(6)]
public long NumberRecords { get; set; }
[ProtoMember(7)]
public DateTime DateTimeStart { get; set; }
[ProtoMember(8)]
public DateTime DateTimeEnd { get; set; }
public TimeSeriesProperties()
{
}
public TimeSeriesProperties(string timeSeriesName, string fileName, string templateName, int PacketLength)
{
this.TimeSeriesName = timeSeriesName;
this.FileName = fileName;
this.TemplateName = templateName;
this.PacketLength = PacketLength;
}
}
public static byte[] Serialize_ProtoBuf<T>(T serializeThis)
{
using (var stream = new MemoryStream())
{
ProtoBuf.Serializer.Serialize<T>(stream, serializeThis);
return stream.GetBuffer();
}
}
public static T Deserialize_ProtoBuf<T>(byte[] byteArray)
{
using (var stream = new MemoryStream(byteArray))
{
return ProtoBuf.Serializer.Deserialize<T>(stream);
}
}
MemoryStream- nothing to do with protobuf-net; please can you show the code that does the serialize/deserialize test? (and ideally, add a @marc comment, so I know to come back and look at it)nbytes, for somen). At the end of each field, it expects either another field-header, or an EOF. 0 is never a valid field-header, so trailing zeros will break the deserializer. Every time.stream.LengthStreams etc), the length is trivially available, and the entire thing is fixed just viaToArray(), or a constrained read. With some implementations (such as aNetworkStreamsending multiple messages),[Serialize|Deserialize]WithLengthPrefixis your friend. I'm not sure why you don't know the length after serializing. Can you expand on that? If this is for a header at the start of a file, the*WithLengthPrefixshould work fine.