hierarchical serialization

Started by
0 comments, last by Zakwayda 16 years, 2 months ago
Is it better to use hierarchical serialization when serializing objects that contain other objects or just to create a function that acceses variables from the object. for example if i were to serialize a face object that stored a certain amount of vertex objects and a texture would i be better off with:

#include <stdio.h>

void Face::serialize(FILE * file) {
   for(int a = 0; a < number of vertices; a++) {
      vertex[a].serialize(file);
   }
   texture.serialize(file);
}


or with a function like:

#include <stdio.h>

void Face::serialize(FILE * file) {

   for(int a = 0; a < number of vertices; a++) {
      fputc(vertex[a].x, file);
      fputc(vertex[a].y, file);
      fputc(vertex[a].z, file);
   }

   // however you would serialize a texture
}


this is just an example, you probaly wouldn't use one byte for each vertex's x, y and z, but you get the idea.
Advertisement
Of the two examples you gave, I'd go with the first (although I'd recommend using file stream objects rather than C-style file IO, since you appear to be using C++).

Also, you might take a look at the Boost Serialization library - it provides basically the same functionality as in your example, and takes care of many of the details of the serialization process for you.

This topic is closed to new replies.

Advertisement