Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google protocol buffers compare

I want to compare two Messages or (two sub parameters) of Google protocol buffers. I don't find an API to achieve it.

Any ideas?

like image 573
dimba Avatar asked Jul 12 '10 11:07

dimba


People also ask

What is faster than protobuf?

Cap'n Proto is an insanely fast data interchange format and capability-based RPC system. Think JSON, except binary. Or think Protocol Buffers, except faster. In fact, in benchmarks, Cap'n Proto is INFINITY TIMES faster than Protocol Buffers.

Does Google use protobuf?

Protocol buffers, or Protobuf, is a binary format created by Google to serialize data between different services. Google made this protocol open source and now it provides support, out of the box, to the most common languages, like JavaScript, Java, C#, Ruby and others.

Are protocol buffers still used?

In particular, it was designed to be smaller and faster than XML. Protocol Buffers are widely used at Google for storing and interchanging all kinds of structured information.

Is protobuf faster than JSON?

Benchmark — stringsIn one – protobufjs was faster, and in the second — JSON was faster. Looking at the schemas, the immediate suspect was the number of strings. We ran the benchmark with this payload (10,000 strings, of length 10 each).


2 Answers

You can use the class google::protobuf::util::MessageDifferencer for this. I think it's only available since v3.0.2:

Introduced new utility functions/classes in the google/protobuf/util directory:

  • MessageDifferencer: compare two proto messages and report their differences.
#include <google/protobuf/util/message_differencer.h>  MessageDifferencer::Equals(msg1, msg2); 
like image 144
Amiga Avatar answered Sep 21 '22 15:09

Amiga


You can rely on the fact that all of your protobuf messages inherit from the google::protobuf::MesageLite type, which in turn has everything you need to compare any two protobuf messages, regardless of if they are even of the same derived type:

bool operator==(const google::protobuf::MessageLite& msg_a,                 const google::protobuf::MessageLite& msg_b) {   return (msg_a.GetTypeName() == msg_b.GetTypeName()) &&       (msg_a.SerializeAsString() == msg_b.SerializeAsString()); } 

EDIT

As was pointed out in the comments below, and especially for map fields, this answer is incorrect. map elements have non-deterministic ordering. Use MessageDifferencer if map fields might be present in your messages.

like image 26
Jake Askeland Avatar answered Sep 23 '22 15:09

Jake Askeland