1

Why is Protocol Buffers so much better than .NET binary serialization? I can only find comparisons which talk about how much better it is (in terms of performance and size), but I could not find why. Can it be at least partly explained without getting into too much detail?

2 Answers 2

3

Because BinaryFormatter stores type and property information in the serialized data, it is larger, hence relatively slower.

ProtoBuf moves this to the application side, so both serializer and deserializer have to know exactly what they're doing and specify in code which type and what properties they want to deserialize into.

Sign up to request clarification or add additional context in comments.

Comments

2

The most important reason is that the binary serialization (BinaryFormatter) is brittle. It encodes exact type names etc, making it useless for archiving data such as in a document format for an application for example.

Other reasons include performance, platform availability etc.

If you don't have any performance problems (i.e. messages are small), and you only want to temporarily store or pass some blob of data, then it can be useful. For example - for passing small messages between two instances of a desktop application on the same computer, it can be useful since you don't have to add another a library just for that task.

3 Comments

I wouldn't call that brittle, but rigid. You need to have the exact same type loaded in order to be able to deserialize a serialized object, which can be considered a good thing. It's not as typeless, schemaless as other forms of serialization, where bytes or entire fields can end up in the wrong place because you use an incompatible type, "but hey, it deserializes without complaining!".
Ok one can call it rigid. The problem is that the extremely rigid schema is entirely implicit so it's very easy to break. e.g. by spelling a namespace differently. This brittleness is good if you want to pass messages between two instances of the application (different versions then can't communicate), but isn't exactly ideal if you were making a word processor document format...
So, that's explicit: the entire type as used to serialize, including its namespace, is required for succesful deserialization. You need to explicitly load the exact same type, nothing is implied for you. Again, depending on context, that may or may not be what you want. Versioning serialization types is a complex subject anyway. I can't think of a non-textual serializer that allows the insertion of arbitrary fields in the middle of a message, for example. With more implicit serialization, for example fixed-length fields, that will mess up deserialization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.