I am parsing the following JSON:
{"names":{"organizationNames":[{"name":"apple"}]}}
into the schema defined in C# Code as shown below.
public class QueryJson
{
#region Properties
[JsonProperty("organization")]
public HeOrganization Organization { get; set; }
[JsonProperty("names")]
public HeName Names { get; set; }
[JsonProperty("emails")]
public List<HeEmailAddress> Emails { get; set; }
#endregion
#region Linked Classes
public class HeOrganization
{
[JsonProperty("id")]
public Guid? ID { get; set; }
}
public class HeName
{
[JsonProperty("organizationNames")]
[Required(ErrorMessage = "Organization Name is Missing")]
public List<HeOrganizationName> OrganizationName { get; set; }
public class HeOrganizationName
{
[JsonProperty("name")]
[Required(ErrorMessage = "Name is Missing")]
public string Name { get; set; }
}
}
public class HeEmailAddress
{
[JsonProperty("address")]
[Required]
[EmailAddress]
public string Address { get; set; }
}
#endregion
}
If I were to pass an obviously invalid JSON:
{"names":{"organizationNames":[{"user":"apple"}]}}
I was expecting DeserializeObject() to fail or throw an Error, but instead it simply assigns 'Name' to null.
var myJson = JsonConvert.DeserializeObject<T>(jsonFilter);
Where T is the instance of the class.
Any suggestion on how to perform such Validations?