AugustoAngeletti / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

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

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
This is in v1.

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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago
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

GoogleCodeExporter commented 9 years ago

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