pizheng/protobuf-net

Boolean with default true doesn't get correctly serialized/deserialized.

Closed this issue · 4 comments

It may be by design but it's not very clear.

Following test fails:

        [ProtoContract]
        public class Data
        {
            [ProtoMember(1)]
            public bool afield = true;
        }

        [Test]
        public void Test()
        {
            Data c = new Data { afield = false };
            using (MemoryStream ms = new MemoryStream())
            {
                Serializer.Serialize(ms, c);
                ms.Seek(0, SeekOrigin.Begin);
                Data cc = Serializer.Deserialize<Data>(ms);
                Assert.That(cc.afield, Is.False);
            }
        }

whilst this will pass:

        [ProtoContract]
        public class Data
        {
            [ProtoMember(1, IsRequired = true)]
            public bool afield = true;
        }

Is it intended to skip writing to context if Optional and the Value is default? 
(line> if (IsOptional && value == DefaultValue) return 0;)

Original issue reported on code.google.com by stuss...@gmail.com on 17 Nov 2010 at 5:41

This is in v1.

Original comment by stuss...@gmail.com on 17 Nov 2010 at 5:41

That can also be addressed by marking `[DefaultValue(true)]`. There are some 
assumptions it makes bout the defaults which *with hindsight* I regret and wish 
to offer as an option in v2.

Original comment by marc.gravell on 17 Nov 2010 at 6:52

Nice one, thanks, will make use of the DefaultValue attrib instead. Didn't like 
making it IsRequired in the first place :)

Original comment by stuss...@gmail.com on 17 Nov 2010 at 7:28

Original comment by marc.gravell on 2 Feb 2011 at 8:15

  • Changed state: Verified