I am writing test automation against a RESTful web service (JSON payload) in .NET and would like to validate that the objects sent to me have exactly the fields in a DTO I define, not more or less.
However, it seems that the serialization method I am using (System.Web.Script.Serialization) doesn't mind when object types don't match.
private class Dog
{
public string name;
}
private class Cat
{
public int legs;
}
static void Main(string[] args)
{
var dog = new Dog {name = "Fido"};
var serializer = new JavaScriptSerializer();
String jsonString = serializer.Serialize(dog);
var deserializer = new JavaScriptSerializer();
Cat cat = (Cat)deserializer.Deserialize(jsonString, typeof(Cat));
//No Exception Thrown! Cat has 0 legs.
}
Is there a .NET serialization library that supports this requirement? Other approaches?