Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataContractSerializer serializing List<T> getting error

I am currently trying to serialize a List, it serializes (I think fine), but when it deserialize,

Sorry for the amount of code, but I am really stuck and have no idea why this is happening, i also tried to changed the struct into a class and no help.

THANKS.

i get the following error UPDATED

    There was an error deserializing the object of type There was an error deserializing the object of type 
`System.Collections.Generic.List`1[[A.B.C.DataValues, A.V, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]. Unexpected end of file. Following elements are not closed: Time, DataValues, ArrayOfDataValues.`

I am serializing like this UPDATED

     public void SerializeDataValue(List<DataValues> values)
            {
                DataContractSerializer serializer = new DataContractSerializer(typeof(List<DataValues>));

                using (MemoryStream stream = new MemoryStream())
                {
                    using (GZipStream compress = new GZipStream(stream, CompressionMode.Compress))
                    {
                        XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(compress);
                        serializer.WriteObject(w, values);

                    }
                    _serializedData = stream.ToArray();
                }
            }

I am deserializing like this UPDATED

 public List<DataValues> DeserializeDataValue()
{
    if (SerializedData == null || SerializedData.Length == 0)
    {
        return new List<DataValues> ();
    }
    else
    {
        DataContractSerializer serializer = new DataContractSerializer(typeof(List<DataValues>));
        using (MemoryStream stream = new MemoryStream(SerializedData))
        {
            using (GZipStream decompress = new GZipStream(stream, CompressionMode.Decompress))
            {
                XmlDictionaryReader r = XmlDictionaryReader.CreateBinaryReader(decompress, XmlDictionaryReaderQuotas.Max);
                return serializer.ReadObject(r, true) as List<DataValues>;
            }
        }
    }
}

Properties

private byte[] _serializedData;

[DataMember]
[Browsable(false)]
public byte[] SerializedData
{
    get { return _serializedData; }
    set { _serializedData = value; }
}

helper Methods

public static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16 * 1024];
    input.Position = 0;
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

Struct

[DataContract(Name = "DataValues", Namespace = "A.B.C")]
public struct DataValues
{
    [DataMember]
    public DateTime Time { get; set; }
    [DataMember]
    public Single Value { get; set; }

    public DataValues(DateTime dateTime, Single value)
    {
        Time = dateTime;
        Value = value;
   }
} 
like image 591
Kev84 Avatar asked Feb 09 '11 22:02

Kev84


2 Answers

It’s because you are not serialising the object(s) completely. You need to close the stream(s) after writing, especially when using gzip. Recommended practice is to use using:

public void SerializeDataValue(List<DataValues> values)
{
    DataContractSerializer serializer = new DataContractSerializer(typeof(List<DataValues>));
    using (MemoryStream stream = new MemoryStream())
    {
        using (GZipStream compress = new GZipStream(stream, CompressionMode.Compress))
        {
            XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(compress);
            serializer.WriteObject(w, values);
        }
        _serializedData = stream.ToArray();
    }
}
like image 194
Timwi Avatar answered Sep 17 '22 21:09

Timwi


Sorry to be late to this question.

The problem with the initial approach was simply that you weren't flushing (read: disposing) the XmlDictionaryWriter.

This should work (note the 2nd using clause):

using (GZipStream compress = new GZipStream(stream, CompressionMode.Compress))
using (XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(compress))
{
    serializer.WriteObject(w, values);
}

Hope this helps someone.

like image 22
Todd Roll Avatar answered Sep 16 '22 21:09

Todd Roll