/ .net

Immutable serializer

As often as possible, I create immutable classes. Immutable means once an instance is created, all its member variables are read only. I will not go into the many reasons why to do this (start here and here). I just like to say it should be applied to any struct and very often to data entity classes, i.e. objects that you often would like to serialize. So more than once, I've found myself banging my head against the wall, because the XmlSerializer in the .NET Framework does not allow serialization nor deserialization of immutable objects: A parameterless constructor and getter/setter for each property is required. This means you either have to make your data entities mutable (very bad design) or you can use the Soap serializer that can take any [Serializable] object (very bad XML).

To save myself from any more headache, I've created two simple classes to perform serialization/deserialization of shallow immutable types. The deserializer is made as an extension to the one in the Framework, whereas the serializer is completely custom.

Here is a C# example of it in action:

XmlTextWriter wr = new XmlTextWriter(@"c:\test.xml", Encoding.UTF8);  
 ImmutableSerializer.Serialize(wr, deserializedObject);  

 public class MyClassDeserializer : ImmutableDeserializer  
 { }  

 XmlSerializer ser = new XmlSerializer(typeof(MyClassDeserializer));  
 MyClass deserializedObject = (MyClassDeserializer)  
       new XmlTextReader(@"c:\test.xml"),  

Start doing better design: [Download] the classes.