Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize a class that contains objects of other classes (recursive serializing?)

How can I do this? Or will the serializer automatically go with recursion, and serialize all those child objects into XML?

Give me an example how would you serialize classes that contain other classes' objects in themselves! That was the core of this question!

I've tried this, and it didn't output anything (except the XML header) to the targeted XML file.

My problem is that I need to serialize a simple class, that just holds a List object. But, those Entities also hod List objects. (Another plus would be if I could avoid the serialization of some components, because some are derived and have dictionaries in them).

public void SaveCurrent(string MapFileName)
{
    string MapPath = world_.game_.Content.RootDirectory + "/Maps/" + MapFileName + ".xml";
    StreamWriter MapWriter = new StreamWriter(MapPath);

    Map SavedMap = new Map();
    SavedMap.Entities = world_.Entities;
    XmlSerializer xSerializer = new XmlSerializer(SavedMap.GetType());

    xSerializer.Serialize(MapWriter, SavedMap);
    MapWriter.Close();
}

That's the piece of code that does the serialization.

public class Map
{
    internal string MapName;
    internal string MapDescription;
    internal string MapAuthor;
    public List<Entity> Entities = new List<Entity>();
}

And this is the class that's serialized. Could internals be counted as publics, if the serialization is called from the same assembly? The code throws exception at the SavedMap.GetType() function, and I've tried typeof(Map) too, but without success. I guess it's because I need some other way to handle each new class (deep serialization) how do I do that?

Also, I've found on some examples, that there are no interface inheritance or attributes, therefore I didn't add those either, but I'm planning to use IXmlSerializable, though I don't know how to call another serialization inside WriteXML implementation.

like image 910
Johnny Avatar asked Jan 30 '10 23:01

Johnny


People also ask

Which of this class contains the method for serializing an object?

The ObjectOutputStream class contains writeObject() method for serializing an Object.

What does serializing an object do?

To serialize an object means to convert its state to a byte stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements either the java. io. Serializable interface or its subinterface, java.

What is serializing code?

Serialization is the process of converting a data object—a combination of code and data represented within a region of data storage—into a series of bytes that saves the state of the object in an easily transmittable form.


1 Answers

Add Serializable and XmlInclude your class:

[System.Serializable]
[System.Xml.Serialization.XmlInclude(typeof(Entity))]
public class Map
{
    internal string MapName;
    internal string MapDescription;
    internal string MapAuthor;
    public List<Entity> Entities = new List<Entity>();
}

Usage:

System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(Map));
serializer.Serialize(mapWriter, savedMap);
like image 191
pr0gg3r Avatar answered Sep 28 '22 11:09

pr0gg3r