I need to serialize an object into a form that can be easily transported. Transportation across an object boundary will then occur, where the object must be a string. After transportation across an application boundary I will deserialize the object and then do some work with it.
My object will have a number of simple public properties, and a collection.
I need to work within the boundaries of the .Net framework due to a sandboxed environment, i.e. no use of Json.Net or other third party libraries.
It seems .Net offer a number of options for this type of work, I am trying to understand which I should be using.
Binary Serialization (BinaryFormatter
& SoapFormatter
)
These seems pretty straightforward, all I have to do is mark my classes as [Seralizable]
and use appropriate classes to create the stream. I then need to convert the stream into a string
which would seem straight forward. The SoapFormatter
has the bonus of creating some data which is reasonably human readable.
XML Serialization (XmlSerializer
)
For this I don't need to decorate my classes, however I should consider using Sgen.exe to improve performance (which sounds like a pain). It has the added bonus over Binary Serialization in that the data is human readable.
Data Contract Serializer (DataContractSerializer
& DataContractJsonSerializer
)
Part of WCF, for this I should decorate my classes (but I can get away without it), i.e. [DataContract] and [DataMember]
. The additional decoration is a pain, but I do end up with some human readable data. Also I have the option for Json formatted data, but they are fundamentally the same process.
So in terms of picking between them all:
- The MSDN suggests the default position is
DataContractSerializer
overXmlSerializer
(apart from in some situations). Whilst I have to decorate my classes this avoids the need to use Sgen.exe. - I'm not really sure how to pick between
DataContractSerializer
andBinaryFormatter
. - Performance wise they all seem pretty similar, if slower than other non .Net options.
At the moment I'm steering towards Binary Serialization for simplicity, but is there anything else I should consider?