porting java to C++ tackling serialization

rukiman
rukiman used Ask the Experts™
on
Ok I have a game that is written in Java and need to port it to C++. How can I tackle serilization? In java it is really easy, simply make your objects implement Serializable and when serializing the object, java is smart enough to transverse through all the objects in memory and serialize them taking into account that there might be multiple references to the same object so not to serialize multiplle times.

How can I handle this in C++ ? Is there a library that works simlilar to the java serilization? Ofcourse I can manually serialize all the objects myself, but I want to avoid doing that as that will take time and not so elegant. And does these libraries handle multiple pointers to the same objects as Java does?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Boost::Serialization http://www.boost.org/doc/libs/1_39_0/libs/serialization/doc/index.html

But don't expect such comfort/features as in Java or .NET.

Commented:
You can also try Google Protocol Buffers
http://code.google.com/p/protobuf/
Is this really necessary?  In Java, I thought Serialization is really just so that you can read/write your classes to memory/files, but this is already available in C++:

MyClass myClass;
std::fstream file("game.lvl");
file.read((char *)&myClass,sizeof(myClass));
file.write((char *)&myClass,sizeof(myClass));
file.close();

If you need the more advanced features of being able to read old/new files, then you could just read each data member individually, and when you add a new data member, you just add that new line of code.  However, I'm not sure why you would need this for reading game objects.  Why are you porting the game to C++?  Can you not keep it in Java?
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

> Is this really necessary?  In Java, I thought Serialization is really just so that you can read/write your classes to memory/files, but this is already available in C++:

C++ "serialization" as you present it isn't portable at all, while Java serialization isn't portable only between different versions.
Oh ok, that's right; I forgot about that, thanks.  If you need that type of portability, then I hope you're also using a portable game engine, else if this is just being made for Windows, then I wouldn't see the need (as much).  Still if you are making a cross-platform game, just stick with your java code.  You can make Java applets and Java web start applications.  The games on these two websites (made by Jagex) were written in Java (as Java applets):


http://www.runescape.com/
http://www.funorb.com/
WhiteMage: this is not only about platform but also about compiler versions (and of course system version). Not to mention that your example won't work with 98% of classes.

Author

Commented:
Yes that example will not work, you cannot assume you will have the same memory space availabel to you  when you deserialize, all your pointers will be pointing to no man's world after you deserialize.


Boost library, I already came across that, but seemed too complicated to use. I was hoping for something easier.

Author

Commented:
The game must be ported to C++, iphone doesn't run Java unfortunately.
Boost is complicated? Actually boost is the simplest serialization library :-) You can try s11n http://www.s11n.net/ (it has more features, but is less "user friendly").

Check the tutorial (in the documentation). You just add one method for each of your objects (you want to serialize) and you are done.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial