Monday, June 18, 2018

c++ - What is Serialization?


I have been around programming for a while as a hobby, but I did not start seeing this concept until recently. I have google'd "what is serialization" numerous times, but I never actually get any sort of definition, usually just examples of how to do it. I am seeing it around the Ogre forums, the Bullet forums, and every other forum I go to, so I think it is about time I actually understand what it is, and why to use it.


Edit


To clarify, I am looking more for why to use it, especially in the sense of game programming. For example, the Bullet Physics API talks a lot about serializing a mesh, so I want to understand why that is.


Thank you :)




Answer



From the boost.serialization documentation: Here, we use the term "serialization" to mean the reversible deconstruction of an arbitrary set of C++ data structures to a sequence of bytes. Such a system can be used to reconstitute an equivalent structure in another program context. Depending on the context, this might used implement object persistence, remote parameter passing or other facility. In this system we use the term "archive" to refer to a specific rendering of this stream of bytes. This could be a file of binary data, text data, XML, or some other created by the user of this library.


In other words, serialization is any process that transforms objects in memory into some kind of bytestream, and deserialization does the reverse, taking a bytestream and transforming it back into objects in memory.


The term "serialization" does not imply anything about the format of the bytestream. It may be an efficiently packed binary format, or a loose XML or YAML description. It may even be source code in the original language itself, or in another programming language, such as JSON, which is a subset of JavaScript. The exact format of the serialized stream should be chosen based on how you intend to use it.


Serialization is a built-in feature of many languages and environments - for example Java and Python. In lower-level languages like C and C++, one must use (or write) serialization libraries, as the mechanism provided by the language is usually not good enough - it cannot follow or serialize pointers and references, and is subject to endianness issues, for example.


Wikipedia has a decent article on serialization.


Serialization is used extensively in games (and all software) for many purposes:



  • Loading the list of all spells in the game from resource files.

  • Saving and loading the game.


  • Recording the state of things (e.g. player positions and inventories) to a SQL or object database.

  • Invoking remote function calls over a network or other IPC link.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...