Sign in to follow this  
nixon

Serialize/Deserialize in .NET

Recommended Posts

nixon    122
Hello, It's my understanding that most commonly methods to serialize objects is done by using: - Binary Formatter - Soap Formatter However, every example that I've seen on the web shows you how to serialize/deserialize one type of class. What if there are more? F.ex. if a client serializes a Car object and sends it to the server. Fine. What happens if the client wants to serialize a Motorcycle class? How does the server "know" which class he's deserializing?

Share this post


Link to post
Share on other sites
coderx75    435
I'm somewhat new to .NET so my knowledge of this isn't very broad. However, I have managed to serialize and deserialize objects using System.Runtime.Serialization and binary formatter. The app (or apps) that are doing the conversions must use the correct references and the serializer pretty much knows what to do from there. Now, if you're deserializing a vehicle object and want to cast it as a motorcycle object, I see know reason why you couldn't explicitly cast it as such.

Share this post


Link to post
Share on other sites
dalep    331
If you use the default serialization services (like a BinaryFormatter) it will include all the meta data it needs to reconstruct objects. So you can deserialize to an object and then use GetType or something like that to find out exactly what you just deserialized.

As a result, this sort of serialization stream tends to be very bulky. The ones binary formatter makes can have well over 50% of their bytes be meta data, particularly if the stream is a container class full of objects.

Share this post


Link to post
Share on other sites
Sirisian    2263
http://cpp.sourceforge.net/?show=31820

I created this for a small MMO engine I made.

So if your in OOP you just make serialize functions and such in the game objects.

GameObject.serializePosition(Buffer);

Kind of like that. Or loop through all the game objects and call that function.

Here's a tutorial I wrote a while ago, should help you understand it.
http://gpwiki.org/index.php/Binary_Packet

Share this post


Link to post
Share on other sites
I strongly recommend not using the built in .NET serialization... it has way too much overhead for a game.

For example, I sent over the word 'Hi' and a 4 byte integer using the built in serialization stuff (BinarySerializer/Serializable/ISerialable), and it blew this packet out into over 300 bytes IIRC!! Even turning off every option that it had, it was still huge.

Write your own serialization. .NET has lots of metadata available to you, and you can use this to your advantage. Check out this thread to see my super slick serialization / messaging system which uses my own custom serialization stuff. The beauty of this technique is that you only have to write the initial reflection stuff and never have to write code to serialize any of your packets again. You simply inherit from the base 'GameMessage' class, and it will automatically be serializable via the ToByte() function and constructor.

[Edited by - graveyard filla on March 1, 2007 4:44:02 PM]

Share this post


Link to post
Share on other sites
Bob Janova    769
That's a pretty awesome system you have there. I second the recommendation not to use .Net serialisation, to save bandwidth. You have only major space saving advantage: you know the metadata (because you'll define the same classes on both sides), so you don't need to send it.

I do something similar, though less hi-tech: anything that needs sending typically has a constructor that takes a ByteBuilder (from my sockets library, it lets you grab segments of a byte array basically) and a ToBytes() method or a WriteTo(ByteBuilder) method. You send a code that specifies which sort of message you're sending, which defines all the classes you'll be using and so on in just one number (2 or 4 bytes typically).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this