DerrickBrayanClayton / protobuf-net

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

CollectionDataContract / collection handling #25

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
f you tag a collection class simply as DataContract it doesn't throw an 
exception, however, serializing an instance of the class and then 
deserializing it loses the Items.

A test code for the classes below:

ContainerClass cc = new ContainerClass()
                        {
                            ContainerID = 9,
                            Description = "foocontainer",
                            Items = new MyCollection()
                                        {
                                            {
                                                3, new MyItem()
                                                       {
                                                           ItemID = 3,
                                                           Description = 
"fooitem"
                                                       }
                                                }
                                        }
                        };

string b64 = null;
using (MemoryStream ms = new MemoryStream())
{
    ProtoBuf.Serializer.Serialize(ms, cc);
    long length = ms.Length;
    ms.Position = 0;
    b64 = Convert.ToBase64String(ms.ToArray());
}

ContainerClass cont = null;
using(MemoryStream ms = new MemoryStream())
{
    byte[] data = Convert.FromBase64String(b64);
    ms.Write(data, 0, data.Length);
    ms.Position = 0;
    cont = ProtoBuf.Serializer.Deserialize<ContainerClass>(ms);
}

Console.WriteLine("Items after deserialization: " + cont.Items.Count);

// ============================================

Also, in Serializer.IsEntityType() method you should check for 
CollectionDataContract tag if anyone else uses collection classes.

Original issue reported on code.google.com by marc.gravell on 7 Oct 2008 at 12:15

GoogleCodeExporter commented 8 years ago
Not an issue; the [DataContract] aspect is identical to DataContractSerializer 
(i.e. 
functions correctly).

Original comment by marc.gravell on 24 Oct 2008 at 11:12

GoogleCodeExporter commented 8 years ago
also - [CollectionDataContract] is not needed; does not offer anything that is 
not 
already supported (as far as I can tell...)

Original comment by marc.gravell on 24 Oct 2008 at 11:13

GoogleCodeExporter commented 8 years ago

Original comment by marc.gravell on 8 Nov 2008 at 9:27