public override bool Equals(object obj) { if ((obj == null) || !this.GetType().Equals(obj.GetType())) { return(false); } PersonV3 other = (PersonV3)obj; return(this.GetHashCode() == other.GetHashCode()); }
static void Intermediate_3_BasicRecursion() { ConsoleTitle("SECTION: Basic Recursion"); PersonV3 p1 = new PersonV3("Bob"); PersonV3 p2 = new PersonV3("Janet", p1); PersonV3 p3 = new PersonV3("Suzette", p2); List <PersonV3> pList1 = new List <PersonV3> { p1, p2, p3 }; JsonSerializerSettings settings1 = new JsonSerializerSettings { ContractResolver = new PrivateSetResolver() }; string json1 = JsonConvert.SerializeObject(pList1, Formatting.Indented, settings: settings1); List <PersonV3> pList2 = JsonConvert.DeserializeObject <List <PersonV3> >(json1, settings: settings1); PersonV3 np1a = pList2[0]; PersonV3 np2a = pList2[1]; PersonV3 np3a = pList2[2]; JsonSerializerSettings settings2 = new JsonSerializerSettings { ContractResolver = new PrivateSetResolver(), PreserveReferencesHandling = PreserveReferencesHandling.Objects }; string json2 = JsonConvert.SerializeObject(pList1, Formatting.Indented, settings: settings2); List <PersonV3> pList3 = JsonConvert.DeserializeObject <List <PersonV3> >(json2, settings: settings2); PersonV3 np1b = pList3[0]; PersonV3 np2b = pList3[1]; PersonV3 np3b = pList3[2]; Console.WriteLine(" Initial Data:"); pList1.ForEach(p => Console.WriteLine($" -> {p}")); Console.WriteLine(" Initial JSON:"); DrawJson(json1, "json1"); DrawJson(json2, "json2"); Console.WriteLine("\n Expected Answers (pre-serialization):"); AreTheseEqual(p1, p2.Parent); AreTheseEqual(p2, p3.Parent); Console.WriteLine("\n Actual Answers (old method - data matches, references don't):"); AreTheseEqual(np1a, np2a.Parent); AreTheseEqual(np2a, np3a.Parent); Console.WriteLine("\n Actual Answers (better method - data and references match):"); AreTheseEqual(np1b, np2b.Parent); AreTheseEqual(np2b, np3b.Parent); }
public PersonV3(string name, PersonV3 parent = null) : base(name) { this.Parent = parent; }