LebGeeks

A community for technology geeks in Lebanon.

You are not logged in.

#1 December 6 2011

Kassem
Member

[.NET] Serialization

Given the different options of serialization in .NET, which one would you consider the most efficient? The ones I know of are the following:

- XmlSerializer
- Binary serialization
- DataContractSerializer & NetDataContractSerializer
- Custom serializer

Your thoughts?

Offline

#2 December 7 2011

arithma
Member

Re: [.NET] Serialization

Do you care about performance or size?
DataContract are aimed at interoperability. NetDataContract is aimed at WCF data service serialization with clients that are .NET compatible (AFAIK). The rest is obvious.

Offline

#3 December 7 2011

Kassem
Member

Re: [.NET] Serialization

arithma wrote:

Do you care about performance or size?
DataContract are aimed at interoperability. NetDataContract is aimed at WCF data service serialization with clients that are .NET compatible (AFAIK). The rest is obvious.

I care about performance. Which one performs better than all the others?

Offline

#4 December 7 2011

Joe3dr
Member

Re: [.NET] Serialization

Binary Serialization is the fastest.

Check out http://code.google.com/apis/protocolbuf … rview.html @googlecode

Last edited by Joe3dr (December 7 2011)

Offline

#5 March 17 2016

scorz
Member

Re: [.NET] Serialization

Reviving an old post!!

I am currently working on a personal project.
I've created a WCF service and I decided to use the same return for all the method

The return class is:

 [DataContract]
    [Serializable]
    public class ServiceOutputDC
    {
        /// <summary>
        /// Client should always check for errors first
        /// </summary>

        [DataMember]
        public bool Faultless { get; set; }

        [DataMember]
        public List<DataContract.GlobalDataContract.ErrorDC> Error { get; set; }

        [DataMember]
        public List<DataContract.GlobalDataContract.DataDC> Data { get; set; }

        public ServiceOutputDC()
        {
            Error = new List<ErrorDC>();
            Data = new List<DataDC>();
        }

    }

Where Error is:

 [DataContract]
    [Serializable]
    public class ErrorDC
    {
        [DataMember]
        public string Error { get; set; }

        [DataMember]
        public string ErrorMsg { get; set; }

    }

and data is

 [DataContract]
    [Serializable]
    public class DataDC
    {
        [DataMember]
        public string DataKey { get; set; }

        [DataMember]
        public byte[] Data { get; set; }

    }

I faced a problem while trying to send "object" type. that's why I've used byte[] instead of "object".

The functions used to serialize and deserialize are:

  public byte[] Serialize(object input)
        {
            BinaryFormatter BF = new BinaryFormatter();
            byte[] output;
            using (var MemStream = new MemoryStream())
            {

                BF.Serialize(MemStream, input);
                output = MemStream.ToArray();
            }
            return output;
        }

        public object DeSerializer(byte[] input)
        {
            object output;
            using (var MemStream = new MemoryStream())
            {
                BinaryFormatter BF = new BinaryFormatter();
                MemStream.Write(input, 0, input.Length);
                MemStream.Seek(0, SeekOrigin.Begin);
                output = BF.Deserialize(MemStream);

            }

            return output;

        }

Any thoughts?

Offline

Board footer