i’m not sure this problem whether is the bug or it’s just this,before i can’t noticed this.
i create a Document class and declare the protobuf-net restrict.
[ProtoContract]
public class Document
{
[ProtoMember(1)]
private Dictionary<string,string> _items;
[ProtoMember(2)]
public int DocNumber
{
get;
set;
}
public Document()
{
this.DocNumber = -1;
this._items = new Dictionary<string,string>();
}
public byte[] Serialize()
{
byte[] bytes = null;
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, this);
bytes = ms.ToArray();
ms.Close();
}
return bytes;
}
public static Document Deserialize(byte[] bytes)
{
Document obj = null;
using (var ms = new MemoryStream(bytes))
{
obj = Serializer.Deserialize<Document>(ms);
ms.Close();
}
return obj;
}
}
in the test code:
var doc = new Document();
doc.DocNumber = 0;
var bytes = doc.Serialize();
var new_doc = Document.Deserialize(bytes);
Console.WriteLine(new_doc.DocNumber + " vs " + doc.DocNumber);
the output message is : -1 vs 0.i can’t believe this result(the correct result is 0 vs 0),so i change the doc.DocNumber = 0 to doc.DocNumber = 1,
the output is correct: 1 vs 1.
this problem means i can’t assign the zero to the DocNumber property,in the constructure method of the Document i must be declare the DocNumber property is -1.
someone can help me? this problem is my cause or the protobuf-net cause? thanks.
By default. It assumes zero values as defaults. I regret this design choice, so it can be disabled in v2, but is preserved for compatibility. You can, in both v1 and v2, also simply tell it that -1 is the default: