XML deserialization of derived classes - c#

How can I create deserialization method that can take an object of class or any of derived classes?
public class Config
{
public string appname;
}
public class CustomConfig1 : Config
{
public string CustomConfig1Param1;
public string CustomConfig1Param2;
}
public class CustomConfig2 : Config
{
public string CustomConfig2Param1;
public string CustomConfig2Param2;
}
I want to get something like serialization method that defines type of input object:
public string serialize(object obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
StringWriter serialized = new StringWriter();
serializer.Serialize(serialized, obj);
return serialized.ToString();
}
But when I read an XML from DB I can't define the type of object, so I can't pass it to XmlSerializer. It may be the Config object or any of derived classes
Please help. How can I define the type of input object?

[XmlInclude(typeof(CustomConfig1))]
[XmlInclude(typeof(CustomConfig2))]
public class Config
{
public string appname;
}
Then just serialize/deserialize specifying typeof(Config); the library will give you back an instance of the appropriate type based on the data.
Edit: full example, including the preference to not hard-code the sub-types:
using System;
using System.IO;
using System.Xml.Serialization;
public class Config
{
public string appname;
}
public class CustomConfig1 : Config
{
public string CustomConfig1Param1;
public string CustomConfig1Param2;
}
public class CustomConfig2 : Config
{
public string CustomConfig2Param1;
public string CustomConfig2Param2;
}
static class Program
{
static void Main()
{
var original = new CustomConfig1
{
appname = "foo",
CustomConfig1Param1 = "x",
CustomConfig1Param2 = "y"
};
var xml = Serialize(original);
var clone = DeserializeConfig(xml);
Console.WriteLine(clone.appname);
var typed = (CustomConfig1)clone;
Console.WriteLine(typed.CustomConfig1Param1);
Console.WriteLine(typed.CustomConfig1Param2);
}
public static string Serialize(Config obj)
{
using (var serialized = new StringWriter())
{
GetConfigSerializer().Serialize(serialized, obj);
return serialized.ToString();
}
}
public static Config DeserializeConfig(string xml)
{
using(var reader = new StringReader(xml))
{
return (Config)GetConfigSerializer().Deserialize(reader);
}
}
static Type[] GetKnownTypes()
{
// TODO: resolve types properly
return new[] { typeof(CustomConfig1), typeof(CustomConfig2) };
}
private static XmlSerializer configSerializer;
public static XmlSerializer GetConfigSerializer()
{
return configSerializer ?? (configSerializer =
new XmlSerializer(typeof(Config), GetKnownTypes()));
}
}

Related

How would you check a specific field of xml in c#? [duplicate]

How can I create deserialization method that can take an object of class or any of derived classes?
public class Config
{
public string appname;
}
public class CustomConfig1 : Config
{
public string CustomConfig1Param1;
public string CustomConfig1Param2;
}
public class CustomConfig2 : Config
{
public string CustomConfig2Param1;
public string CustomConfig2Param2;
}
I want to get something like serialization method that defines type of input object:
public string serialize(object obj)
{
XmlSerializer serializer = new XmlSerializer(obj.GetType());
StringWriter serialized = new StringWriter();
serializer.Serialize(serialized, obj);
return serialized.ToString();
}
But when I read an XML from DB I can't define the type of object, so I can't pass it to XmlSerializer. It may be the Config object or any of derived classes
Please help. How can I define the type of input object?
[XmlInclude(typeof(CustomConfig1))]
[XmlInclude(typeof(CustomConfig2))]
public class Config
{
public string appname;
}
Then just serialize/deserialize specifying typeof(Config); the library will give you back an instance of the appropriate type based on the data.
Edit: full example, including the preference to not hard-code the sub-types:
using System;
using System.IO;
using System.Xml.Serialization;
public class Config
{
public string appname;
}
public class CustomConfig1 : Config
{
public string CustomConfig1Param1;
public string CustomConfig1Param2;
}
public class CustomConfig2 : Config
{
public string CustomConfig2Param1;
public string CustomConfig2Param2;
}
static class Program
{
static void Main()
{
var original = new CustomConfig1
{
appname = "foo",
CustomConfig1Param1 = "x",
CustomConfig1Param2 = "y"
};
var xml = Serialize(original);
var clone = DeserializeConfig(xml);
Console.WriteLine(clone.appname);
var typed = (CustomConfig1)clone;
Console.WriteLine(typed.CustomConfig1Param1);
Console.WriteLine(typed.CustomConfig1Param2);
}
public static string Serialize(Config obj)
{
using (var serialized = new StringWriter())
{
GetConfigSerializer().Serialize(serialized, obj);
return serialized.ToString();
}
}
public static Config DeserializeConfig(string xml)
{
using(var reader = new StringReader(xml))
{
return (Config)GetConfigSerializer().Deserialize(reader);
}
}
static Type[] GetKnownTypes()
{
// TODO: resolve types properly
return new[] { typeof(CustomConfig1), typeof(CustomConfig2) };
}
private static XmlSerializer configSerializer;
public static XmlSerializer GetConfigSerializer()
{
return configSerializer ?? (configSerializer =
new XmlSerializer(typeof(Config), GetKnownTypes()));
}
}

Ignore null parameters when serializing an object using FullSerializer in Unity

I have an object I would like to serialize into json in Unity to send to a service via REST call. In .NET I know you can easily ignore null properties.
[JsonProperty("some_model", NullValueHandling = NullValueHandling.Ignore)]
public class SomeModel
{
....
}
Is this possible using FullSerializer in Unity?
Currently I have
fsData data = null;
fsResult r = sm_Serializer.TrySerialize(objectToSerialize, out data);
string sendjson = data.ToString();
Is there a similar attribute I can add to the DataModel using FullSerializer?
[fsObject(ignoreNullProperties)]
public class SomeModel
{
....
}
Looks like one answer is custom converters.
private static fsSerializer sm_Serializer = new fsSerializer();
[fsObject(Converter = typeof(CustomConverter))]
public class SomeClass
{
string MyProp { get; set; }
}
public class CustomConverter : fsConverter
{
private static fsSerializer sm_Serializer = new fsSerializer();
public override bool CanProcess(Type type)
{
return type == typeof(SomeClass);
}
public override fsResult TryDeserialize(fsData data, ref object instance, Type storageType)
{
throw new NotImplementedException();
}
public override fsResult TrySerialize(object instance, out fsData serialized, Type storageType)
{
SomeClass someClass = (SomeClass)instance;
serialized = null;
Dictionary<string, fsData> serialization = new Dictionary<string, fsData>();
fsData tempData = null;
if (someClass.MyProp != null)
{
sm_Serializer.TrySerialize(someClass.MyProp, out tempData);
serialization.Add("myProp", tempData);
}
serialized = new fsData(serialization);
return fsResult.Success;
}
}
This works but any other suggestions are greatly appreciated!

Xml output class to generic class

I have a class I found on another post that I'm trying to modify.
using System;
using System.IO;
namespace Misc
{
internal class ConfigManager
{
private string _sConfigFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}.xml", AppDomain.CurrentDomain.FriendlyName));
private Config m_oConfig = new Config();
public Config MyConfig
{
get { return m_oConfig; }
set { m_oConfig = value; }
}
// Load configuration file
public void LoadConfig()
{
if (System.IO.File.Exists(_sConfigFileName))
{
System.IO.StreamReader srReader = System.IO.File.OpenText(_sConfigFileName);
Type tType = m_oConfig.GetType();
System.Xml.Serialization.XmlSerializer xsSerializer = new System.Xml.Serialization.XmlSerializer(tType);
object oData = xsSerializer.Deserialize(srReader);
m_oConfig = (Config)oData;
srReader.Close();
}
}
// Save configuration file
public void SaveConfig()
{
System.IO.StreamWriter swWriter = System.IO.File.CreateText(_sConfigFileName);
Type tType = m_oConfig.GetType();
if (tType.IsSerializable)
{
System.Xml.Serialization.XmlSerializer xsSerializer = new System.Xml.Serialization.XmlSerializer(tType);
xsSerializer.Serialize(swWriter, m_oConfig);
swWriter.Close();
}
}
}
}
I'd like to pass in an object of type X and have it save. On that same premise, I'd like to pass in a type and have it pass back the object of type X. Right now, it is hard coded to use Config. So, if there is a way to pass in the class object (?) then I'd like it to save it as that object and/or return it of that object.
Is that possible? If so, how would I go about doing this?
Use generic:
internal class ConfigManager<T>
{
private string _fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, string.Format("{0}.xml", AppDomain.CurrentDomain.FriendlyName));
private T _config;
private XmlSerializer serializer = new XmlSerializer(typeof(T));
public T MyConfig
{
get { return _config; }
set { _config = value; }
}
public void LoadConfig()
{
if (File.Exists(_fileName))
{
using (var reader = File.OpenText(_fileName))
{
_config = (T)serializer.Deserialize(reader);
}
}
}
public void SaveConfig()
{
using (var writer = File.CreateText(_fileName))
{
serializer.Serialize(writer, _config);
}
}
}
Usage:
var man = new ConfigManager<Foo>();
Basically, we'd need to make it work with Generics. So, we start by giving it a type variable, and replace every use of the class Config with T:
internal class Manager<T>
{
private T m_oObj; // etc
Next, I'll just do one method; you can do the rest. (and I'm removed the explicit namespaces, cuz they're ugly)
using System.IO;
using System.Xml.Serialization;
public void LoadConfig<T>()
{
if (File.Exists(_sConfigFileName))
{
var srReader = File.OpenText(_sConfigFileName);
var xsSerializer = new XmlSerializer(typeof(T));
var oData = xsSerializer.Deserialize(srReader);
m_oObj = (T)oData;
srReader.Close();
}
}

Deserialize JSON into Derived classes

{
"Class1": {
"Class2": [
{"Name": "DerivedV1"},
{"Name": "DerivedV2"},
{"Name": "DerivedV3"}
]
}
}
JsonConvert.DeserializeObject<Class1>(jsonString, settings);
public class Class1
{
public List<BaseClass> DerivedClasses { get; set; }
}
public abstract BaseClass
{
public string Name { get; set; }
public abstract bool DoSomething;
}
public class DerivedV1 : BaseClass
{
public override bool DoSomething()
{
// Logic here, different for each derived class.
}
}
When trying to deserialize Class1, I can't figure out how to create the list of derived classed from name. I can't declare something like List BaseClass where BaseClass is abstract and I'm not sure how to use reflection during deserialization within Class2 to determine the derived class from the "name" value. I also looked into ICloneable but didn't get anywhere with it in this context.
Edit:
Here is what I ended up creating and calling from get and set
public static List<T> DeserializeJObjectsToObjects<T>(IEnumerable<JObject> jObjects, string typeKey, string nameSpaceOfClass)
{
Assembly assembly = Assembly.GetExecutingAssembly();
List<T> convert = new List<T>();
foreach (var jObject in jObjects)
{
JToken typeName;
jObject.TryGetValue(typeKey, out typeName);
string fullNameSpace = string.Format(namespaceFormat, nameSpaceOfClass, typeName);
Type t = Type.GetType(string.Format(fullNameSpace));
convert.Add((T) Activator.CreateInstance(t));
}
return convert;
}
public static List<JObject> SerializeObjectsToJObjects<T>(IEnumerable<T> variableObjects )
{
List<JObject> convert = new List<JObject>();
foreach (T variableObject in variableObjects)
{
var jsonString = JsonConvert.SerializeObject(variableObject);
convert.Add(JObject.Parse(jsonString));
}
return convert;
}
First, some notes - I don't use JSonConvert, but there are articles that show you how to do this with. See Json.net serialize/deserialize derived types?, for example. However, you didn't include a json.net tag, so I'm assuming this should hopefully help, or at least point you to the right place.
I used the built in .Net JavaScriptSerializer. You may need to adjust this to work with your input. I created my input based on your code, and my json looks nothing like yours. So, you may still have some work left.
I was able to get it working with a SimpleTypeResolver. Code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Class1 oClass = new Class1();
DerivedV1 v1 = new DerivedV1();
v1.Name = "DerivedV1";
oClass.DerivedClasses.Add(v1);
DerivedV2 v2 = new DerivedV2();
v2.Name = "DerivedV2";
oClass.DerivedClasses.Add(v2);
DerivedV3 v3 = new DerivedV3();
v3.Name = "DerivedV3";
oClass.DerivedClasses.Add(v3);
JavaScriptSerializer ser = new JavaScriptSerializer(new SimpleTypeResolver());
string sSer = ser.Serialize(oClass);
var test =ser.Deserialize(sSer,typeof(Class1));
foreach (var tst in ((Class1)test).DerivedClasses)
{
Console.WriteLine(tst.Name + Environment.NewLine);
Console.WriteLine(tst.GetType().ToString() + Environment.NewLine);
}
}
public class Class1
{
public List<BaseClass> DerivedClasses { get; set; }
public Class1()
{
DerivedClasses = new List<BaseClass>();
}
}
public abstract class BaseClass
{
public string Name { get; set; }
private bool _dosom;
public abstract bool DoSomething();
public BaseClass(){}
}
public class DerivedV1 : BaseClass
{
public override bool DoSomething()
{
return true;
// Logic here, different for each derived class.
}
}
public class DerivedV2 : BaseClass
{
public override bool DoSomething()
{
return false;
// Logic here, different for each derived class.
}
}
public class DerivedV3 : BaseClass
{
public override bool DoSomething()
{
return true;
// Logic here, different for each derived class.
}
}
}
}
my output json (with the SimpleTypeResolver):
{"__type":"WindowsFormsApplication6.Form1+Class1, WindowsFormsApplication6, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","DerivedClasses":[{"__type":"WindowsFormsApplication6.Form1+DerivedV1, WindowsFormsApplication6, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"DerivedV1"},{"__type":"WindowsFormsApplication6.Form1+DerivedV2, WindowsFormsApplication6, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"DerivedV2"},{"__type":"WindowsFormsApplication6.Form1+DerivedV3, WindowsFormsApplication6, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","Name":"DerivedV3"}]}
and without the type resolver, for comparison (causes errors):
{"DerivedClasses":[{"Name":"DerivedV1"},{"Name":"DerivedV2"},{"Name":"DerivedV3"}]}
And once I deserialize (in the for loop, Console.WriteLine)...
DerivedV1
WindowsFormsApplication6.Form1+DerivedV1
DerivedV2
WindowsFormsApplication6.Form1+DerivedV2
DerivedV3
WindowsFormsApplication6.Form1+DerivedV3
Using this as my solution.
public static List<T> DeserializeJObjectsToObjects<T>(IEnumerable<JObject> jObjects, string typeKey, string nameSpaceOfClass)
{
Assembly assembly = Assembly.GetExecutingAssembly();
List<T> convert = new List<T>();
foreach (var jObject in jObjects)
{
JToken typeName;
jObject.TryGetValue(typeKey, out typeName);
string fullNameSpace = string.Format(nameSpaceFormat, nameSpaceOfClass, typeName);
Type t = Type.GetType(string.Format(fullNameSpace));
convert.Add((T) Activator.CreateInstance(t));
}
return convert;
}
public static List<JObject> SerializeObjectsToJObjects<T>(IEnumerable<T> variableObjects )
{
List<JObject> convert = new List<JObject>();
foreach (T variableObject in variableObjects)
{
var jsonString = JsonConvert.SerializeObject(variableObject);
convert.Add(JObject.Parse(jsonString));
}
return convert;
}

I can't serialize a list of objects in C# with XmlSerializer

I have a list of many test implements the interface IDoTest, that I want to store in a file. I also want to read from this file.
It seemed natural to simple use the XmlSerializer to store the objects in my List of IDoTest. But when I do this I get a vague I am sorry I cant do that error in the neighborhood of System.Xml.Serialization.TypeDesc.CheckSupported()
Can the XmlSerializer only do trivial jobs? Or am I missing something? They are talking about custom serialization at MSDN.
Here is my simplified code example.
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
public interface IDoTest
{
void DoTest();
void Setup();
}
internal class TestDBConnection : IDoTest
{
public string DBName;
public void DoTest()
{
Console.WriteLine("DoHardComplicated Test");
}
public void Setup()
{
Console.WriteLine("SetUpDBTest");
}
}
internal class PingTest : IDoTest
{
public string ServerName;
public void DoTest()
{
Console.WriteLine("MaybeDoAPing");
}
public void Setup()
{
Console.WriteLine("SetupAPingTest");
}
}
internal class Program
{
private static void Main(string[] args)
{
TestDBConnection Do1 = new TestDBConnection { DBName = "SQLDB" };
PingTest Do2 = new PingTest { ServerName = "AccTestServ_5" };
List<IDoTest> allTest = new List<IDoTest> { Do1, (Do2) };
// Now I want to serialize my list.
// Its here where I get the error at allTest
XmlSerializer x = new XmlSerializer(allTest.GetType());
StreamWriter writer = new StreamWriter("mySerializedTestSuite.xml");
x.Serialize(writer, allTest);
}
}
}
XmlSerializer cannot serialize an interface, and by extension, it cannot serialize a List<> of some interface. It can only serialize concrete object types.
The assumption is that you will probably want to deserialize the objects at some point, and if it only output information pertaining to that interface, it can't guarantee that all the necessary data is present to reconstruct the original objects.
This post shows a potential workaround, if you are able to use an abstract base class and explicitly provide every possible type of object that may appear in the list.
I followed the link that StriplingWarrior gave and found this excellent answer. https://stackoverflow.com/a/15089253/648076 from webturner
I changed his implementation and made a class class ListOfToDo that implemented both List and IXmlSerializable. That worked!
Here is my changed code.
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
public interface IDoTest
{
void DoTest();
void Setup();
}
public class TestDBConnection : IDoTest
{
public string DBName;
public void DoTest()
{
Console.WriteLine("DoHardComplicated Test");
}
public void Setup()
{
Console.WriteLine("SetUpDBTest");
}
}
public class PingTest : IDoTest
{
public string ServerName;
public void DoTest()
{
Console.WriteLine("MaybeDoAPing");
}
public void Setup()
{
Console.WriteLine("SetupAPingTest");
}
}
public class ListOfToDo : List<IDoTest>, **IXmlSerializable**
{
#region IXmlSerializable
public XmlSchema GetSchema(){ return null; }
public void ReadXml(XmlReader reader)
{
reader.ReadStartElement("ListOfToDo");
while (reader.IsStartElement("IDoTest"))
{
Type type = Type.GetType(reader.GetAttribute("AssemblyQualifiedName"));
XmlSerializer serial = new XmlSerializer(type);
reader.ReadStartElement("IDoTest");
this.Add((IDoTest)serial.Deserialize(reader));
reader.ReadEndElement(); //IDoTest
}
reader.ReadEndElement(); //IDoTest
}
public void WriteXml(XmlWriter writer)
{
foreach (IDoTest test in this)
{
writer.WriteStartElement("IDoTest");
writer.WriteAttributeString("AssemblyQualifiedName", test.GetType().AssemblyQualifiedName);
XmlSerializer xmlSerializer = new XmlSerializer(test.GetType());
xmlSerializer.Serialize(writer, test);
writer.WriteEndElement();
}
}
#endregion
}
internal class Program
{
private static void Main(string[] args)
{
TestDBConnection Do1 = new TestDBConnection { DBName = "SQLDB" };
PingTest Do2 = new PingTest { ServerName = "AccTestServ_5" };
ListOfToDo allTest = new ListOfToDo { Do1, (Do2) };
// Now I want to serialize my list.
// Its here where I get the error at allTest
XmlSerializer x = new XmlSerializer(allTest.GetType());
StreamWriter writer = new StreamWriter("mySerializedTestSuite.xml");
x.Serialize(writer, allTest);
writer.Flush();
writer.Close();
//Read it aka deserialize
{
var xmlSerializer = new XmlSerializer(typeof(ListOfToDo));
var xmlReader = XmlReader.Create(new StreamReader("mySerializedTestSuite.xml"));
ListOfToDo readWhatToTest = (ListOfToDo)xmlSerializer.Deserialize(xmlReader);
xmlReader.Close();
}
}
}
}
Output will then be:
<?xml version="1.0" encoding="utf-8"?>
<ListOfToDo>
<IDoTest AssemblyQualifiedName="ConsoleApplication1.TestDBConnection, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<TestDBConnection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<DBName>SQLDB</DBName>
</TestDBConnection>
</IDoTest>
<IDoTest AssemblyQualifiedName="ConsoleApplication1.PingTest, ConsoleApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<PingTest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ServerName>AccTestServ_5</ServerName>
</PingTest>
</IDoTest>
</ListOfToDo>
Not sure if this might be the cause of your issue but on these two examples they do use typeof(T) instead of T.GetType()
http://msdn.microsoft.com/en-us/library/71s92ee1.aspx
I can't serialize a list of objects in C# with XmlSerializer

Categories