|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionI needed a simple serialization / deserialization mechanism to save and load state information for objects whose type (the class) is constructed and compiled at runtime (see my article on Declaratively Populating A PropertyGrid). Neither SerializationThe serializer consists of three steps:
InitializationThis is very straight forward. A public void Start()
{
ms=new MemoryStream();
xtw = new XmlTextWriter(ms, Encoding.UTF8);
xtw.Formatting=Formatting.Indented;
xtw.Namespaces=false;
xtw.WriteStartDocument();
xtw.WriteComment("Auto-Serialized");
xtw.WriteStartElement("Objects");
}
The SerializerThe serializer does a few important things and has a few limitations:
// simple property serialization
public void Serialize(object obj)
{
Trace.Assert(xtw != null, "Must call Serializer.Start() first.");
Trace.Assert(obj != null, "Cannot serialize a null object.");
Type t=obj.GetType();
xtw.WriteStartElement(t.Name);
foreach(PropertyInfo pi in t.GetProperties())
{
Type propertyType=pi.PropertyType;
// with enum properties, IsPublic==false, even if marked public!
if ( (propertyType.IsSerializable) && (!propertyType.IsArray) &&
(pi.CanWrite) && ( (propertyType.IsPublic) || (propertyType.IsEnum) ) )
{
object val=pi.GetValue(obj, null);
if (val != null)
{
bool isDefaultValue=false;
// look for a default value attribute.
foreach(object attr in pi.GetCustomAttributes(false))
{
if (attr is DefaultValueAttribute)
{
// it exists--compare current value to default value
DefaultValueAttribute dva=(DefaultValueAttribute)attr;
isDefaultValue=val.Equals(dva.Value);
}
}
// only non-default values or properties without a default value are
// serialized.
if (!isDefaultValue)
{
// do a type conversion to a string, as this yields a
// deserializable value, rather than what ToString returns.
TypeConverter tc=TypeDescriptor.GetConverter(propertyType);
if (tc.CanConvertTo(typeof(string)))
{
val=tc.ConvertTo(val, typeof(string));
xtw.WriteAttributeString(pi.Name, val.ToString());
}
else
{
Trace.WriteLine("Cannot convert "+pi.Name+" to a string value.");
}
}
}
else
{
// null values not supported!
}
}
}
xtw.WriteEndElement();
}
FinisherThe public string Finish()
{
Trace.Assert(xtw != null, "Must call Serializer.Start() first.");
xtw.WriteEndElement();
xtw.Flush();
xtw.Close();
Encoding e8=new UTF8Encoding();
xml=e8.GetString(ms.ToArray(), 1, ms.ToArray().Length-1);
return xml;
}
DeserializationThe deserializer expects that the instance has already been constructed. This is a very helpful shortcut to take, because constructing an object at runtime often requires a fully qualified assembly name, namespace, and other information. Furthermore, the type information for my runtime constructed classes is actually not available--only the instance is. For my particular requirement, this is not an issue. Also, the deserializer will inspect each property for a default value and restore that value to the specified object unless it is being overridden in the XML. Finally, when deserializing multiple objects, you must know the exact sequence that was used to serialize the objects, as an index pointing to the serialized object's element is passed in to the deserializer. Again, for my purposes, this restriction is not an issue. // simple property deserialization
public void Deserialize(object obj, int idx)
{
Trace.Assert(doc != null, "Must call Deserializer.Start() first.");
Trace.Assert(doc.ChildNodes.Count==3, "Incorrect xml format.");
Trace.Assert(idx < doc.ChildNodes[2].ChildNodes.Count,
"No element for the specified index.");
Trace.Assert(obj != null, "Cannot deserialize to a null object");
// skip the encoding and comment, and get the indicated
// child in the Objects tag
XmlNode node=doc.ChildNodes[2].ChildNodes[idx];
Type t=obj.GetType();
Trace.Assert(t.Name==node.Name, "Object name does not match element tag.");
// set all properties that have a default value and not overridden.
foreach(PropertyInfo pi in t.GetProperties())
{
Type propertyType=pi.PropertyType;
// look for a default value attribute.
foreach(object attr in pi.GetCustomAttributes(false))
{
if (attr is DefaultValueAttribute)
{
// it has a default value
DefaultValueAttribute dva=(DefaultValueAttribute)attr;
if (node.Attributes[pi.Name] == null)
{
// assign the default value, as it's not being overridden.
// this reverts the object's property back to the default
pi.SetValue(obj, dva.Value, null);
}
}
}
}
// now parse the xml attributes that are going to change property values
foreach(XmlAttribute attr in node.Attributes)
{
string pname=attr.Name;
string pvalue=attr.Value;
PropertyInfo pi=t.GetProperty(pname);
if (pi != null)
{
TypeConverter tc=TypeDescriptor.GetConverter(pi.PropertyType);
if (tc.CanConvertFrom(typeof(string)))
{
try
{
object val=tc.ConvertFrom(pvalue);
pi.SetValue(obj, val, null);
}
catch(Exception e)
{
Trace.WriteLine("Setting "+pname+" failed:\r\n"+e.Message);
}
}
}
}
}
UsageUsage is very simple. Let's say we want to serialize a simple class (in this example, one that is constructed at compile time): public class TestClass
{
protected string firstName;
protected string lastName;
[DefaultValue("Marc")]
public string FirstName
{
get {return firstName;}
set {firstName=value;}
}
[DefaultValue("Clifton")]
public string LastName
{
get {return lastName;}
set {lastName=value;}
}
public TestClass()
{
firstName="Marc";
lastName="Clifton";
}
}
Serializing an instance of this class would look like this: Serializer s=new Serializer();
s.Start();
TestClass tc=new TestClass();
tc.FirstName="Joe";
tc.LastName="Smith";
s.Serialize(tc);
string text=s.Finish();
Resulting in XML that looks like this: <?xml version="1.0" encoding="utf-8"?>
<!--Auto-Serialized-->
<Objects>
<TestClass FirstName="Joe" LastName="Smith" />
</Objects>
Deserialization of this object is done as: Deserializer d=new Deserializer();
d.Start(text);
TestClass tc=new TestClass();
d.Deserialize(tc, 0);
After which, the properties of the class are set to "Joe" and "Smith". Revisions11/30/04 - Added support for property types that implement IList. ConclusionThe code presented above meets a very specific requirement that I have. Even if you don't have this requirement, hopefully you'll gain something from the techniques demonstrated, especially the use of the type converter to convert to and from a string. This is an important "trick" to ensure that the serialized string is in a format that the deserializer can handle and avoids writing special case code for
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||