Home > Back-end >  How to compare protobuf message
How to compare protobuf message

Time:09-26

In this post: Google protocol buffers compare, I see a several ways to compare protobuf messages in C .

I tried to use msg.DebugString(), SerializeToString() and MessageDifferencer::Equal() for my payload comparison. And these three methods give different result:

DebugString shows two messages equal, while the other two shows unequal.

Since the messages is too long(>2000bytes), and multiple layers recursion, it's not really straightforward to see the difference.

Does someone know what's the difference of these methods, and which is the best to pick?

CodePudding user response:

MessageDifferencer is probably the safest way to do it as things like maps can have indeterminate order within a message. But you may want to use Equivalent() rather than Equal() since the former is a little less pedantic (allowing for default values in missing fields, for example).

You can process the report entries generated to, for example, ignore fields that have been moved within the message if order is not important to the way you process the data.

CodePudding user response:

Pick MessageDifferencer::Equal().

Serialization is not guaranteed to be deterministic, i.e. equal messages may be serialized differently (e.g. the field order may differ in most cases) for whatever reasons (it can obviously differ across library versions, but e.g. parse-serialize roundtrip might preserve field order and thus won’t cancel the difference).

And DebugString looks like it is intended for debugging, not for comparison, so I’d not even consider it.

  • Related