public void SerializeInterfaceProperties() { var foo = new Foo { A = 1, S = "ilias" }; var bar = new Bar { A = 10 }; var bar2 = new Bar2 { A = "10" }; foo.Bar = bar; var settings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; var j = JsonConvert.SerializeObject(foo); var o = JsonConvert.DeserializeObject <Foo>(j, new DerivedClassConverter <IBar, Bar>());//, new InterfaceConverter<IFoo, Foo>()); Assert.AreEqual(foo.A, o.A); Assert.AreEqual(foo.S, o.S); Assert.AreEqual(((Bar)foo.Bar).A, ((Bar)o.Bar).A); Assert.AreEqual(foo.A, o.A); foo.Bar = bar2; j = JsonConvert.SerializeObject(foo); o = JsonConvert.DeserializeObject <Foo>(j, new DerivedClassConverter <IBar, Bar2>()); Assert.AreEqual(foo.A, o.A); Assert.AreEqual(foo.S, o.S); Assert.AreEqual(((Bar2)foo.Bar).A, ((Bar2)o.Bar).A); Assert.AreEqual(foo.A, o.A); }
public void SerializeInterfaceProperties() { var foo = new Foo {A=1,S="ilias"}; var bar = new Bar { A = 10 }; var bar2 = new Bar2 { A = "10" }; foo.Bar = bar; var settings = new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore}; var j = JsonConvert.SerializeObject(foo); var o = JsonConvert.DeserializeObject<Foo>(j, new DerivedClassConverter<IBar,Bar>());//, new InterfaceConverter<IFoo, Foo>()); Assert.AreEqual(foo.A, o.A); Assert.AreEqual(foo.S, o.S); Assert.AreEqual(((Bar)foo.Bar).A, ((Bar)o.Bar).A); Assert.AreEqual(foo.A, o.A); foo.Bar = bar2; j = JsonConvert.SerializeObject(foo); o = JsonConvert.DeserializeObject<Foo>(j, new DerivedClassConverter<IBar, Bar2>()); Assert.AreEqual(foo.A, o.A); Assert.AreEqual(foo.S, o.S); Assert.AreEqual(((Bar2)foo.Bar).A, ((Bar2)o.Bar).A); Assert.AreEqual(foo.A, o.A); }