Archive for March, 2010
It appears that there are more than one way to skin a cat and sometimes it tough to decide on how to skin a cat. With serialization there are three common ways to serialize an object each has there advantages and disadvantages.
XML serialization allows you to exchange data between disjoint systems e.g. C# and PHP. The downside of XML serialization is that it is very verbose and out of the box C# does not support serialization of a dicationary.
Example of XML Serialization
using System.Xml.Serialization; StreamWriter _XMLOut = new StreamWriter(_filepath); XmlSerializer _ProjectSerializer = new XmlSerializer(typeof(MyObject)); _ProjectSerializer.Serialize(_XMLOut, _MyObject); _XMLOut.Close();
Example of XML Deserialization
using System.Xml.Serialization; StreamReader _XMLIn = new StreamReader(_filepath); XmlSerializer _ProjectSerializer = new XmlSerializer(typeof(MyObject)); _MyObject = (MyObject)_ProjectSerializer.Deserialize(_XMLIn); _XMLOut.Close();
Binary serialization is less verbose and does support serialization of dicationaries however you can not exchange binary serialized messages between disjoint systems.
Example of Binary Serialization
using System.Runtime.Serialization.Formatters.Binary; FileStream _stream = new FileStream(_filepath, FileMode.CreateNew); BinaryFormatter _Formatter = new BinaryFormatter(); _Formatter.Serialize(_stream, _MyObject); stream.Close();
Example of Binary Deserialization
using System.Runtime.Serialization.Formatters.Binary; FileStream _stream = new FileStream(_filepath, FileMode.Open); BinaryFormatter _Formatter = new BinaryFormatter(); _MyObject = (MyObject)objFormatter.Deserialize(_stream);
JSon serialization is less verbose allows for seriaization of dictionaries and can be used to exchange messages between disjoint systems the downfall of JSon is that the serialized information contains no type definitions.
Example of JSON Serialization
using System.Runtime.Serialization.Json; DataContractJsonSerializer _Serializer = new DataContractJsonSerializer(_MyObject.GetType()); MemoryStream _DataMemoryStream = new MemoryStream(); _Serializer.WriteObject(_DataMemoryStream , _MyObject);
Example of JSON Deserialization
using System.Runtime.Serialization.Json; MemoryStream _MemoryStream = new MemoryStream(_DataStream); DataContractJsonSerializer _Deserializer = new DataContractJsonSerializer(_MyObject.GetType()); return (MyObject)_Deserializer.ReadObject(_MemoryStream);
One major negative aspect JSON serialization is that you need the same version of the object to serialize and deserialize it. The deserializer will fail if the newer version of the object as an additional property.
Back to serialization again this is a beast that just wont die every time I think I know everything about serialization that there is to know about I get whacked over the head with it. One of the big issues with xml serialization is that it will not serialize a dictionary if you try you get an error like this
Cannot serialize member SSD.SourceControl.Modules.Files.Monitors.Business.MonitorSettings.Accesses of type System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=18.104.22.168, Culture=neutral, PublicKeyToken=b77a5c561934e089]], because it implements IDictionary.
You have four ways to deal with this
- Do not use a dictionary
- Override the dictionary and implement IXmlSerializable (http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx)
- Use binary serialization
- Use JSON Serialization
Personally my preference is to either user binary serialization or JSON serialization if that is an option.