views:

139

answers:

2

Are there any good existing C++ serialization libraries that support partial serialization? By partial serialization I mean that I might want to say, save the values of 3 specific members, and later be able to apply that saved copy to a different instance, only updating those 3 members and leaving the others intact. This would be useful for example for sending data over the network -- I have some object on a client and a server, and when a member changes on the server I want to send a message to the client containing the updated value for that member and that member only, I don't want to send a copy of the whole object over the wire. boost::serialization at a glance looks like it only supports all or nothing.

A: 

Although I'm not familiar with them, you could also check out Google's Protocol Buffers .

Stephen Nutt
+3  A: 

You're clearly not looking for serialization here.

Serialization is about saving an object and then recreating it from the stream of bytes. Think video games saves or the session context for a webserver.

Here what you need is messaging. Google's Protocol Buffers is nice for that. Specify a message that will contain every single field as optional, upon reception of the message, update your object with the fields that do exist and leave the others untouched.

The great thing with protobuf is that it handles forward and backward compatibility nicely, as well as text and binary encoding (text being great for debugging and binary being better for pure performance), ...

And you can even decode the messages with another language (say python or ruby) if you save them somewhere and want to throw together a html gui to inspect it!

Matthieu M.
I wouldn't say I'm "clearly not looking for serialization." Perhaps serialization libraries don't usually consider this use case, but I think it's a pretty natural extension of full serialization to wish to be able to save only a subset of the members of an object. Sending data over the network is only one possible use case.
Joseph Garvin
I don't mean to seem pedantic, but Serialization is about saving enough information that you will be able to rebuild a semantically identical clone from it. A subset of the attributes might be sufficient for that (if you can compute the rest). Here however we have a different case: it's more of an Observer Pattern really. And an Observer Pattern is really about communication... and so we are talking about events (or messages). Whether a network is involved does not really matter (though it complicates things a bit).
Matthieu M.
I don't mean to seem pedantic either, but if serialization is saving enough information to create a semantically identical clone, partial serialization can be saving enough information to create a partially semantically identical 'clone' ;) Or rather, identical from some (narrower than the original) perspective.
Joseph Garvin