I am wondering if serialization and deserialization of classes should always be treated as an "atomic transaction?"
What I mean is, if an error were to occur during the process of serializing or deserializing a member of an object, should the whole serialization/deserialization of the object be considered to have failed?
For a more concrete example, I am going to use C++. Suppose a very basic structure as follows:
struct RGB
{
uint8_t r;
uint8_t g;
uint8_t b
};
Suppose I have an RBG instance defined as so:
RGB myRGB{0x10, 0x20, 0x30};
Which, if serialized into a raw binary stream would look like:
0x10 0x20 0x30
Suppose, the one of the bytes gets lost during transmission so that the "deserializer" is fed only:
0x10 0x20
I can see two options here.
a) Because the 'b' member of the struct cannot be deserialized, the whole struct cannot be deserialized.
b) 'r' and 'g' can be deserialized, and will we just use the default value for 'b'
Both have their merits. The problem I can see with (b) is that, while it ensures you at least get "something" it is not actually an accurate reconstruction of the thing that was serialized which (for a more complex example) could result in further errors down the line.
I suppose an option (c) would be:
c) It depends on the application. If the object in question can be default constructed, then option (b) is fine. If the object cannot be default constructed (i.e., requires values in its constructor), all of the values required for constructed need to be deserialized atomically.
0x10 0x20
is theR
andG
channels? It could be possible that aG
value of0x15
went missing in the middle and what you have is actually theR
andB
channels? Or does this question assume that the data loss occurs in e.g. a truncated file, where it will only ever miss data from the end?