Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two .NET object graphs for differences?

In our Client/Server Application we've been using BinaryFormatter for the serialization process. For performance reasons we are trying to migrate to protobuf-net ( http://code.google.com/p/protobuf-net/ ).

Our software transmits huge graphs with cycles between Client and Server.

Now I am looking for a way to make sure that the data which was serialized and deserialized using protobuf is exactly the same as the one which was usually processed by BinaryFormatter.

A bit by bit comparison is simple: I serialize using BinaryFormatter to a file. Deserialize this file again using BinaryFormatter. Then I serialize using ProtoBuf into a file. Deserialize using ProtoBuf from that file. Serialize again using BinaryFormatter into a file.

Then i can simply compare that file with the original file.

However, those two files are not 100% equal. So now I need to find a way to track the differences down.

Is there maybe some tool out there which visualizes data that was serialized by BinaryFormatter? Or do you know some other helper which does a deep comparison and tells me where the differences are?

Using XMLSerializer and comparing two XML files is not possible as BinaryFormatter is able to serialize way more data than the XMLSerializer - even without marking fields explicitly.

Thank you, TH

like image 310
TwinHabit Avatar asked Jul 12 '11 08:07

TwinHabit


People also ask

How do I compare two graphs?

How to 'compare' graphs. In questions where you are asked to compare, you need to comment on both the similarities and differences. For example, to compare the graph in Figure 1 with the graph in Figure 2 you would say that as both independent variables increase so does the rate of photosynthesis.

How do you compare objects in C #?

The most common way to compare objects in C# is to use the == operator. For predefined value types, the equality operator (==) returns true if the values of its operands are equal, false otherwise. For reference types other than string, == returns true if its two operands refer to the same object.


2 Answers

How about using DataContractSerializer with object-tracking enabled (preserveObjectReferences in the constructor). That should allow you to serialize them to xml (of a sort, at least), where you might be able to compare the differences.

like image 137
Marc Gravell Avatar answered Oct 22 '22 13:10

Marc Gravell


We had the same problem. We Json serialize the two objects (with formatting, indentation, new lines etc) and then use a simple text diff. It will tell you not only that they're different, but exactly what the difference is.

like image 40
nganju Avatar answered Oct 22 '22 13:10

nganju