protobuf.net not serializing zero

自古美人都是妖i 提交于 2019-12-12 21:05:20

问题


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 the Val property (with a setter) had the [ProtoMember], it would look for bool ShouldSerializeVal()


来源:https://stackoverflow.com/questions/1379332/protobuf-net-not-serializing-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!