# What are good solutions for serialization in C++? [closed]

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).

• 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). – Jesse Dorsey Aug 6 '10 at 17:08

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.

• 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.

• 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
• @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
• Shame it doesn't seem to support custom allocator/deleter. – JamesAMD Aug 19 '16 at 23:06
• I have just ported from boost serialization to cereal. The port was remarkably smooth. It worked like a charm. Cereal supports xml, json, binary and portable binary. The reason why I ported to cereal was the last one. I needed portable binary archives because I run a server to which clients (for now a mac, soon iOS and Android) connect. I was quite happy with boost serialization but I think some of the features of cereal take it a step further in a better way, such as the mentioned portable binary serialization. For language interop protocol buffers and such is better. – Germán Diago Dec 2 '17 at 9:55
• Per docs boost.serialization is not thread-safe. Neither is Cereal that uses a similar API. – Hi-Angel May 7 '18 at 7:16

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.

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.

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

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.

• 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 '14 at 8:55

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

// 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;
}


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.