Serialization of element of unknown type

Jul 10, 2012 at 6:10 AM

I'm wondering how/if it's possible to serialize something of type "object" (eg you have an object with a property of type "object", so it can be anything like a string or an int...).

For ex consider the following code:

string fileName = "myfile.xml";
string content = "this is just a simple test";
object contentO = content;
YAXLib.YAXSerializer ser = new YAXLib.YAXSerializer(typeof(object));
ser.SerializeToFile(contentO, fileName);
contentO = ser.DeserializeFromFile(fileName);
string contentRead = (string)contentO;

The XML doesn't contain the string, it only contains:
<?xml version="1.0" encoding="utf-8"?>
<Object />

I stumbled upon this because I had several "object[]" of the same size, and instead of serializing them straight (using "YAXSerializer(typeof(object[]))"), I was just writing the size of these arrays and then each element one by one.

Note that when I create the YAXSerializer to serialize the object, I could specify the correct type, ie YAXSerializer(typeof(string)), but then when I want to deserialize it, I would need to know that what I wrote was a string, else I would end up with an instance of the object class.

Coordinator
Jul 10, 2012 at 2:13 PM

I suppose if you encapsulate the object or the object array into a class, that should work correctly. 

However I will check and see why it is not working when you pass the object directly.

Jul 11, 2012 at 1:59 AM

Thanks for the reply.

Actually I have my own "fast binary serialization" methods and I wanted to also be able to serialize things in XML for debug purposes (I can easily look at what's serialized and modify it manually if needed). In my library,I have WriteObject/ReadObject methods, which write/read the type of the object being serialized/deserialized. But it doesn't work like this for YAXLib it seems. For now I just changed the way one of my classes was serialized so that it works with YAXLib (ie I say I wan't to serialize an array of objects, instead of a simple object, that way the info on each type is stored in XML).

Appart from that small problem, everything works fine, so thanks for this nice library!

Coordinator
Jul 22, 2012 at 1:03 PM

I've just released YAXLib 2.9. Could you please verify that this problem is fixed in the new version?

Jul 30, 2012 at 2:59 AM
Edited Jul 30, 2012 at 3:01 AM

Looks like it's working well now! Thanks.

Using the same sample code, I now get

<?xml version="1.0" encoding="utf-8"?>
<Object yaxlib:realtype="System.String" xmlns:yaxlib="http://www.sinairv.com/yaxlib/">this is just a simple test</Object>