简体   繁体   中英

How to compare protobuf message

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?

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.

Pick MessageDifferencer::Equal() .

Serialization is not guaranteed to be deterministic, ie equal messages may be serialized differently (eg the field order may differ in most cases) for whatever reasons (it can obviously differ across library versions, but eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM