Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm curious what solutions game developers have come up with for serializing the different types of data that they deal with for their games. Do you guys use some monolithic GameObject hierarchy that houses a serialization interface for derived types, use sort of custom RTTI-based solution, perform explicit stream serialization for certain classes, or use some of the open source solutions (boost::serialization, s11n, etc).

share|improve this question

closed as off-topic by Josh Petrie Oct 7 at 14:44

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about "how to get started," "what to learn next," or "which technology to use" are discussion-oriented questions which involve answers that are either based on opinion, or which are all equally valid. Those kinds of questions are outside the scope of this site. Visit our help center for more information." – Josh Petrie
If this question can be reworded to fit the rules in the help center, please edit the question.

1  
I think serialization in game development programming is an important topic, but I don't like this question. What are you trying to do? What specific problems are you trying to solve? In lieu of that, I converted it to a community wiki so that it's more of a discussion format. –  Tetrad Aug 5 '10 at 22:39
    
I think it would be good to apply some formating to this, (ala the version control question). –  Noctrine Aug 6 '10 at 17:08

8 Answers 8

up vote 8 down vote accepted

Protocol buffers from Google can be a pretty good approach for serializing c++ objects. You may have to make some intermediated objects as part of the serialization process, but it also works across many platforms and languages.

share|improve this answer
    
I'm using it and it's easy if you can automate the code generation in your build system. Although a really excellent advantage is that it allow you to transfer data on remote machnies without regarding the platform used, and it's optimized for that so the generated objects are not big. –  Klaim Oct 8 '10 at 12:22

We in our game simply use boost.serilization, it's easy to use and very fast, but it's in my opinion just useful for savegames. If you try creating characters, i recommend you something XML'ish or JSON based things, because they are easy to read and editable even if you don't have the editor.

share|improve this answer
    
I've seen boost::serialization used successfully for client/server communication as well. However, AFAIK it's stream-based, so it's not exactly version tolerant. That may not be a deal-breaker for client/server communication, but if you use it for saved games, then any change to the game data structures makes loading older saved games a virtual impossibility (support for loading older object versions becomes a real chore). –  Mike Strobel Aug 9 '10 at 18:16
1  
@MikeStrobel I recently was reviewing a few serialization and json kits and came across this comment. boost::serialization explicitly supports versioning. The serialize calls can receive a version number and then it's up to the user to implement basic branching logic (if( version > 1.0)...). Overall seems pretty robust. –  M2tM Dec 24 '13 at 22:05

I rather like JSON for serialization. It is pretty simple to parse and there are free libraries available such as http://jsoncpp.sourceforge.net/ I've never been a fan of boost or RTTI in C++. Tinyxml also work well for xml serialization and deserialization. http://www.grinninglizard.com/tinyxml/ Ultimately I don't want to have to spend any more time than I have to for serialization.

share|improve this answer

XDS was designed just for this purpose, it gives you the benefits of XML during development and the benefits of a compact binary representation at distribution time.

share|improve this answer
    
I am not really sure what makes XDS different from Google Protocol Buffers? They seem to serve the same purpose, except that XDS was first. –  jacmoe Aug 6 '10 at 19:27
    
You surely mean XSD and not XDS ? codesynthesis.com/products/xsd I wanted to post an answer about it to complete the list. –  v.oddou Oct 7 at 8:55

Both jsonCpp and Protocol buffers are good options. To my knowledge both are only going to allow you to serial tree structures out of the box (please correct me if I am wrong). boost::serialization can handle arbitrary graphs, but doesn't have a nice text format like json (I think there is an xml format)

Personally I think the approach for json serialization that Dojo has taken is the best
http://docs.dojocampus.org/dojox/json/ref

I made my own version of it in c++ using jsoncpp, that also will deserialize typed objects (I have sort of big factory for all my types). It allows me to make a scene from a collection of json files that can be referenced anyway I please.

share|improve this answer

Google FlatBuffers is an efficient cross platform serialization library for C++, with support for Java and Go. It was created at Google specifically for game development and other performance-critical applications.

It is available as open source under the Apache license, v2.

share|improve this answer

If you are on a Linux platform, you can directly use json.h library for serialization. Here is sample code I have come across. Source: Json Serializer

//============================================================================
// Name        : JsonTest.cpp
// Author      : Manis Kumar Khedawat
//============================================================================

#include <iostream>
#include <json/json.h>

using namespace std;

struct objStruct{
    string str;
    int n1;
    int n2;
};

typedef objStruct obj;

void serializeToJson(json_object *jObj,obj* pObj)
{
    /*
    string str;
    int n1;
    int n2;
    */

    // Create json object for every member in struct Obj.

    json_object *jstr = json_object_new_string (pObj->str.c_str());
    json_object *jn1 =json_object_new_int(pObj->n1);
    json_object *jn2 =json_object_new_int(pObj->n2);

    // Add all above created object into jObj

    json_object_object_add(jObj,"str",jstr);
    json_object_object_add(jObj,"n1",jn1);
    json_object_object_add(jObj,"n2",jn2);

    // pObj is Serialzed into jObj
}

void deSerializeToJson(json_object *jObj,obj* pObj)
{
    /*
    string str;
    int n1;
    int n2;
    */

    // Get every member as different json obj from jObj
    json_object *jstr = json_object_object_get (jObj,"str");
    json_object *jn1 =json_object_object_get(jObj,"n1");
    json_object *jn2 =json_object_object_get(jObj,"n2");

    pObj->str=json_object_get_string(jstr);
    pObj->n1=json_object_get_int(jn1);
    pObj->n2=json_object_get_int(jn2);

    // jObj is DeSerialzed into pObj
}

int main() {
    // Lets Create an Object which we will serialze into Json
    obj obj1;
    obj1.n1=3;
    obj1.n2=6;
    obj1.str="This is String";

    // Create a json Object
    json_object* jObj=json_object_new_object();

    // To serialize into Json Object
    // Please Keep in mind , we are passing address of object (pointer) & not object
    serializeToJson(jObj,&obj1);

    obj obj2;
    // To serialize into Json Object
    // Please Keep in mind , we are passing address of object (pointer) & not object
    deSerializeToJson(jObj,&obj2);

    cout<<"String str == "<<obj2.str<<endl;
    cout<<"n1 & n2 : "<<obj2.n1<<" "<<obj2.n2<<endl;

    return 0;
}
share|improve this answer

Message pack is a great alternative too! (http://msgpack.org)

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.