views:

164

answers:

1

Hello

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);
        }
    }
A: 

Hi; 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()
Marc Gravell