Serialization

Started by G_G, November 22, 2009, 05:06:39 pm

Previous topic - Next topic

G_G

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?

fugibo

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.

G_G

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.

Blizzard

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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.