Reputation: 5301
I have a bunch of different DTO classes. They are being serialized into an XML string at one point and shot over to client-side of the web app. Now when the client shoots back an XML string, I need to deserialize it back to an instance of the DTO class that it represents. The problem is that I want to make it generic and possibly a function which takes in an xml string and spits out an object of a type. Something like a long these lines:
public sometype? Deserialize (string xml)
{
//some code here
return objectFromXml;
}
EDIT: Horrible example! I just contradicted myself!
I cannot do the following:
Person person = Deserialize(personXmlStringFromClient);
because I don't know that personXmlStringFromClient is a representation of Person DTO object instance.
I don't know what serialized object is given to me and that seems to be my problem here. I've been reading about reflection and other techniques which involve sticking the type into the xml so that deserializer knows what to do with it. I can't seem to pull it all together into one working piece. Also, in pretty much most examples, the author knows what type there will be after deserialization. Any suggestion is welcome! If I need to do something special with the serialization process, please share that too.
Upvotes: 17
Views: 35283
Reputation: 12902
Forget generics. What if you don't know the return type? If you're using Visual C# 2010 or later, this is the beauty of the new dynamic
keyword. Below is an example serializer class that I wrote, and sample usage that takes only the XML string and tries to parse it for a general System
type, returning back an output value on success. You can probably extend it for other, more custom types you have, but they probably need to have a default constructor and you would probably need to do some more parsing in order to get the type name and then get the path to it in your assembly. Its a bit tricky but once you get the hang of how the code below works, it starts to open up a bunch of possibilities. Isn't this exactly what you're looking for? Your question asks how to get an object of said type back without knowing that type (note however, that you still have to have a definition of that type in your code in order to deserialize it). Let me explain. If you look at how assemblyFormatter
was used in the code below, you'll see that its trickier for a type you define yourself, like a struct
or enum
for example, because for these types you'd have to pass assemblyFormatter
in as myObject.GetType().FullName
. Thats the string that the activator uses to call the default constructor of your type to create it in order to be able to create a serializer off of it; it basically comes down to the complexity of having to know in your assembly that type definition in order to be able to deserialize it.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
namespace DynamicSerializer
{
class Program
{
static void Main(string[] args)
{
bool myObject = true;
// There are a bunch of other examples you can try out:
// string myObject = "Hello, world.";
// long myObject = 1000;
// int myObject = 100;
string mySerializedObject;
if (Serializer.TrySerialize(myObject, out mySerializedObject))
{
Console.WriteLine("Serialized {0} as {1}.", myObject, mySerializedObject);
dynamic myDeserializedObject;
if (Serializer.TryDeserialize(mySerializedObject, out myDeserializedObject))
{
Console.WriteLine("Deserialized {0} as {1}.", mySerializedObject, myDeserializedObject);
}
}
Console.ReadLine();
}
class Serializer
{
public static bool TrySerialize(dynamic unserializedObject, out string serializedObject)
{
try
{
StringWriter writer = new StringWriter();
XmlSerializer serializer = new XmlSerializer(unserializedObject.GetType());
serializer.Serialize(writer, unserializedObject);
serializedObject = writer.ToString();
return true;
}
catch
{
serializedObject = null;
return false;
}
}
// The assemblyFormatter parameter is normally not passed in. However, it may be passed in for cases where the type is a special case (such as for enumerables or structs) that needs to be passed into the serializer. If this is the case, this value should be passed in as yourObject.GetType().FullName.
public static bool TryDeserialize(string serializedObject, out dynamic deserializedObjectOut, string assemblyFormatter = "System.{0}")
{
try
{
StringReader reader = new StringReader(serializedObject);
XDocument document = XDocument.Load(reader);
string typeString = null;
// Map the object type to the System's default value types.
switch (document.Root.Name.LocalName)
{
case "string":
typeString = "String";
break;
case "dateTime":
typeString = "DateTime";
break;
case "int":
typeString = "Int32";
break;
case "unsignedInt":
typeString = "UInt32";
break;
case "long":
typeString = "Int64";
break;
case "unsignedLong":
typeString = "UInt64";
break;
case "boolean":
typeString = "Boolean";
break;
case "double":
typeString = "Double";
break;
case "float":
typeString = "Single";
break;
case "decimal":
typeString = "Decimal";
break;
case "char":
typeString = "Char";
break;
case "short":
typeString = "Int16";
break;
case "unsignedShort":
typeString = "UInt16";
break;
case "byte":
typeString = "SByte";
break;
case "unsignedByte":
typeString = "Byte";
break;
}
if (assemblyFormatter != "System.{0}")
{
typeString = document.Root.Name.LocalName;
}
if (typeString == null)
{
// The dynamic object's type is not supported.
deserializedObjectOut = null;
return false;
}
if (typeString == "String")
{
// System.String does not specify a default constructor.
XmlSerializer serializer = new XmlSerializer(typeof(String));
reader = new StringReader(serializedObject);
deserializedObjectOut = serializer.Deserialize(reader);
}
else
{
object typeReference;
if (assemblyFormatter != "System.{0}")
{
typeReference = Activator.CreateInstance(Type.GetType(assemblyFormatter));
}
else
{
typeReference = Activator.CreateInstance(Type.GetType(String.Format(assemblyFormatter, typeString)));
}
XmlSerializer serializer = new XmlSerializer(typeReference.GetType());
reader = new StringReader(serializedObject);
deserializedObjectOut = serializer.Deserialize(reader);
}
return true;
}
catch
{
deserializedObjectOut = null;
return false;
}
}
}
}
}
Upvotes: 1
Reputation: 13947
You can use a generic:
public T Deserialize<T>(string input)
where T : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
return (T)ser.Deserialize(sr);
}
If you don't know which type it will be, I assume you have a fixed number of possible types, and you could try deserializing to each one until you don't encounter an exception. Not great, but it would work.
Or, you could inspect the start of the xml for the outer object name and hopefully be able to determine the type from there. This would vary depending on what the xml looks like.
Edit: Per your edit, if the caller knows the type that they are passing, could they supply the fully qualified typename as a string as an additional parameter to the service?
If so, you could do this:
Type t = Type.GetType(typeName);
and change the Deserialize method to be like this:
public object Deserialize(string input, Type toType)
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(toType);
using (StringReader sr = new StringReader(input))
return ser.Deserialize(sr);
}
However, this only gets you an object
... If all of the types in question implement a common interface, you could deserialize as above but change the return type to the interface (and cast to it in the return statement)
Upvotes: 27
Reputation: 184622
If you don't mind generics:
public static T DeserializeFromString<T>(string value)
{
T outObject;
XmlSerializer deserializer = new XmlSerializer(typeof(T));
StringReader stringReader = new StringReader(value);
outObject = (T)deserializer.Deserialize(stringReader);
stringReader.Close();
return outObject;
}
Edit: If you don't know what type of object the XML will translate to you cannot return anything other than an object. You could of course test afterwards what kind of object you just got. One way to do this would probably be to pass all the types of objects that may be deserialized the XmlSerializer
.
public static object DeserializeFromString(string value, Type[] types)
{
XmlSerializer deserializer = new XmlSerializer(typeof(object), types);
StringReader stringReader = new StringReader(value);
object outObject = deserializer.Deserialize(stringReader);
stringReader.Close();
return outObject;
}
Using this method will assume that your object got boxed (you should serialize the same way), which gives you XML like this:
<object xsi:type="Person">
...
</object>
(If you have many types to pass you can use reflection to get them, e.g. using something like Assembly.GetExecutingAssembly().GetTypes()
)
Upvotes: 2
Reputation: 108957
if you have custom serialization/deserialization routine for each type, you could use something like this
public T Deserialize <T>(string xml)
{
if(typeof(T) == typeof(Person))
{
// deserialize and return Person instance
}
else if(typeof(T) == typeof(Address)
{
// deserialize and return Address instance
}
...
...
...
}
And you can call
Person p = Deserialize<Person>(personXmlStringFromClient);
Upvotes: 0
Reputation: 71573
How about making a non-generic "front door" function whose purpose is to figure this out? Most XML schemas use the object name, or a reasonable facsimile, as the outermost tag for the object.
Upvotes: 0