Chaos Project

Game Development => Sea of Code => Topic started by: G_G on November 22, 2009, 05:06:39 pm

Title: Serialization
Post by: G_G on November 22, 2009, 05:06:39 pm
I googled serialization, tried to figure out what it does and how it works and how you use it. So maybe someone here can help me understand it a bit more. I found several examples but I still don't understand it. I even looked at Blizz's Babs Config app to see, but I can't understand how it works or how to use it.
Any help?
Title: Re: Serialization
Post by: fugibo on November 22, 2009, 05:08:29 pm
Quote from: game_guy on November 22, 2009, 05:06:39 pm
I googled serialization, tried to figure out what it does and how it works and how you use it. So maybe someone here can help me understand it a bit more. I found several examples but I still don't understand it. I even looked at Blizz's Babs Config app to see, but I can't understand how it works or how to use it.
Any help?


Serialization is just a technique that turns an object into a platform-independent string, which can then be used to store the object and load it later. Ruby's Marshall module is an example.
Title: Re: Serialization
Post by: G_G on November 22, 2009, 05:10:19 pm
oh so it pretty much is just dumping data into a file like Marshal.dump? That makes more sense but I'm still curious to how to use it.
Title: Re: Serialization
Post by: Blizzard on November 23, 2009, 04:12:48 am
Add the [Serializable] attribute to a class (add before "class XYZ") and use a variant of this piece of code to serialize the class:

            FileStream stream = new FileStream();
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, your_instance);


Obviously the FileStream instance should be an actual file stream. I use the serializer in Blizz-ABS config to create deep-copy of instances as well. You need following namespaces:

using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


Make sure all classes contained in that class are also [Serializable]. In fact I suggest that you make a container class specifically designed just to contain all other classes that are being serialized.