private void TestJsonSerializationRoundTrip(SerializableWrapper e, TypeNameHandling flag) { Console.WriteLine("Type Name Handling: " + flag.ToString()); StringWriter writer = new StringWriter(); //Create our serializer and set Type Name Handling appropriately JsonSerializer serializer = new JsonSerializer(); serializer.TypeNameHandling = flag; //Do the actual serialization and dump to Console for inspection serializer.Serialize(new JsonTextWriter(writer), e); Console.WriteLine(writer.ToString()); Console.WriteLine(); //Now try to deserialize //Json.Net will cause an error here as it will try and instantiate //the interface directly because it failed to respect the //TypeNameHandling property on serialization SerializableWrapper f = serializer.Deserialize <SerializableWrapper>(new JsonTextReader(new StringReader(writer.ToString()))); //Check Round Trip Assert.AreEqual(e, f, "Objects should be equal after round trip json serialization"); }