Everybody wants their game to be optimized as much as possible. While developing a game I’ve faced with an issue when I had to synchronize something very large (let’s assume a List of Entity classes which have their own parameters inside, such as Age, BirthDate, Name, etc.); or I had to write save and load system. Of course you can say, why do I need to serialize that, if I just can pass the raw data of Entity. Well, theoretically you can, but what if Entity class has 10+ parameters and it is not just a single Entity but a bunch of them, would you write something similar to ” void SendEntityData(par1, par2, par3, par4, par5, par6, par7, par8, par9, par10, entity2par1, entity2par2 …) ” ? Apparently, not.
So what’s Serialization? It is the method of compressing your data into the smaller data which is nothing more than byte. That byte array can be deserialized into the original class which has been compressed previously. There are a lot of serializators, you probably know some of them: JSON, BinaryFormatter, MessagePack, ProtoBuf etc.
Before choosing a serialization method you have to determine what do you need, in case if you need human-readability – I would suggest to use JSON or XML, but be careful with data size and perfomance do not pass this stuff in a Fast-paced multiplayer games. If human-readability is not the factor and you need the data to be encrypted and also high performance and smallest data size; I suggest ProtoBuf-net. You can and read more about it at: https://github.com/mgravell/protobuf-net
If you previously used the BinaryFormatter then I would like to show you a little difference between it and ProtoBuf-net. In the following example, List of Entities has been written and read into and from the two different files over 1000+ times.
As you can see, protobuf-net is 5x times faster and its file size 10x times less than BinaryFormatter.