问题
It looks like there is an encoding problem for 0 as Int64. Other values as Int64 ok.
[ProtoMember(3)] private readonly Int64 _intValue
is deserialized as Int64.MinValue
Any idea?
I confirm the bug. This class will not serialize properly if _val == 0
[ProtoContract]
class VerySimple
{
[ProtoMember(1)]
private readonly Int64 _val = Int64.MinValue;
public VerySimple(long val)
{
_val = val;
}
public long Val
{
get { return _val; }
}
public VerySimple()
{
}
}
this test fails
[Test]
public void TestProtobufEncodingSimple()
{
//OK
{
MemoryStream stream = new MemoryStream();
Serializer.Serialize(stream, new VerySimple(1));
stream.Seek(0, SeekOrigin.Begin);
VerySimple reloaded = Serializer.Deserialize<VerySimple>(stream);
Assert.AreEqual(reloaded.Val, 1L);
}
//KO
{
MemoryStream stream = new MemoryStream();
Serializer.Serialize(stream, new VerySimple(0));
stream.Seek(0, SeekOrigin.Begin);
VerySimple reloaded = Serializer.Deserialize<VerySimple>(stream);
Assert.AreEqual(reloaded.Val, 0L);
}
}
回答1:
sorry for the delay - I had a few days off-line ;-p
The protocol buffers specification has an implicit default of zero on most types. To ensure compliant handling, it respects this default unless you tell it otherwise. I will try to make this clearer in the documentation.
There are several solutions:
- Add a
[DefaultValue(int.MinValue)]
attribute to the field to set an explicit default - Add
IsRequired = true
to the[ProtoMember]
attribute - If you use
Nullable<int>
, I suspect it will treat zero as explicit - In the case of properties, it respects the
ShouldSerialize*
pattern, so if theVal
property (with a setter) had the[ProtoMember]
, it would look forbool ShouldSerializeVal()
来源:https://stackoverflow.com/questions/1379332/protobuf-net-not-serializing-zero