简体   繁体   English

如何比较 protobuf 消息

[英]How to compare protobuf message

In this post: Google protocol buffers compare , I see a several ways to compare protobuf messages in C++.在这篇文章: Google protocol buffers compare中,我看到了几种比较 C++ 中 protobuf 消息的方法。

I tried to use msg.DebugString() , SerializeToString() and MessageDifferencer::Equal() for my payload comparison.我尝试使用msg.DebugString()SerializeToString()MessageDifferencer::Equal()进行负载比较。 And these three methods give different result:这三种方法给出了不同的结果:

DebugString shows two messages equal, while the other two shows unequal. DebugString显示两个消息相等,而另外两个显示不相等。

Since the messages is too long(>2000bytes), and multiple layers recursion, it's not really straightforward to see the difference.由于消息太长(>2000 字节),并且是多层递归,因此很难直接看出差异。

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. MessageDifferencer可能是最安全的方法,因为地图之类的东西在消息中可能具有不确定的顺序。 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).但是您可能希望使用Equivalent()而不是Equal() ,因为前者不那么迂腐(例如,允许在缺失字段中使用默认值)。

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() .选择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.DebugString看起来像是用于调试,而不是用于比较,所以我什至不会考虑它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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