user116587
user116587

Reputation:

Does serializing distinct but equal object graphs with BinaryFormatter produce same binary representation?

I'm looking at hashing an object model, based on a serialization of it.

If I serialize an object graph using the .NET BinaryFormatter, is the serialized representation guaranteed to be the exact same, byte for byte, for another object graph where all the involved objects are composed of the same values?

Intuitively, I'd think so, but I'm unsure if object/reference IDs might somehow influence the actual serialized representation.

Thanks in advance.

Upvotes: 3

Views: 243

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1062550

I do not know that is guaranteed, and with the lack of a documented guarantee I would not personally rely on it. In particular:

  • anything that implements ISerializable (i.e. custom serialization) can do whatever it wants
  • anything involving something like a dictionary may be unreliable - simply because there is no defined ordering inside a dictionary, and it may depend on the spare space in the original vs reconstituted, etc
  • there may be subtle compatible but not identical changes between platforms, revisions etc

However, if you are talking about binary equivalence, I deduce you are talking about storage of BinaryFormatter data, and subsequent comparison at a later date. Allow me to stress: IMO, BinaryFormatter is not suitable for storage; it is OK (ish) for transport of data between running systems that share a platform and application version, but the sheer number of times I've seen people stuck in a hole due to version differences and BinaryFormatter is overwhelming. It is for these reasons that I got interested in protobuf, and wrote my own serializer that would be suitable for storage and rehydration against existing object models that make have "versioned" in the interim.

Upvotes: 1

Related Questions