1
public class Output
{
    public string Property { get; set; }
}

string json = "{\"Property\":\"foo\", \"P2\":\"foo\"}";

var output = JsonConvert.DeserializeObject(json, typeof(Output), /*settings?*/); //should throw error "P2" not found in class "Output"

I need to throw error if JSON not exactly maps into class (i.e. not strong typed). In this case "P2" is not present in Output class, and is not valid.

UPDATE:

People mention about duplicate but this is not duplicate. Field is not required it is simply DOES NOT EXIST in output class. Nothing to be marked with required attribute, please, read more carefully next time.

eocron
  • 6,885
  • 1
  • 21
  • 50
  • 4
    You want [`JsonSerializerSettings.MissingMemberHandling = MissingMemberHandling.Error;`](http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_MissingMemberHandling.htm). See [Can you detect if an object you deserialized was missing a field with the JsonConvert class in Json.NET](http://stackoverflow.com/a/21032130/3744182). – dbc Mar 03 '17 at 22:02
  • You, sir, saved my day =) – eocron Mar 03 '17 at 22:04

0 Answers0