title: Compare two objects using serialization C#
body: <p>Why it is not a good practice to compare two objects by serializing them and then compare the strings like in the following example?</p>
  
  <pre><code>public class Obj
  {
      public int Prop1 { get; set; }
      public string Prop2 { get; set; }
  }
  
  public class Comparator&lt;T&gt; : IEqualityComparer&lt;T&gt;
  {
      public bool Equals(T x, T y)
      {
          return JsonConvert.SerializeObject(x) == JsonConvert.SerializeObject(y);
      }
  
      public int GetHashCode(T obj)
      {
          return JsonConvert.SerializeObject(obj).GetHashCode();
      }
  }
  
  Obj o1 = new Obj { Prop1 = 1, Prop2 = "1" };
  Obj o2 = new Obj { Prop1 = 1, Prop2 = "2" };
  
  bool result = new Comparator&lt;Obj&gt;().Equals(o1, o2);
  </code></pre>
  
  <p>I have tested it and it works, it is generic so it could stand for a great diversity of objects, but what I am asking is which are the downsides of this approach for comparing objects?  </p>
  
  <p>I have seen it has been suggested in <a href="https://stackoverflow.com/questions/10454519/best-way-to-compare-two-complex-object">this question</a> and it received some upvotes but I can't figure it out why this is not considered the best way, if somebody wants to compare just the values of the properties of two objects?</p>
  
  <p><strong>EDIT :</strong> I am strictly talking about Json serialize, not XML.</p>
  
  <p>I am asking this because I want to create a simple and generic <code>Comparator</code> for a Unit Test project, so the performance of comparison does not bother me so much, as I know this may be one of the biggest down-sides. Also the typeless problem can be handled using in case of Newtonsoft.Json the <code>TypeNameHandling</code> property set to <code>All</code>. </p>