Click or drag to resize
Json.NET

JsonPropertyAttribute required

 

This sample uses JsonPropertyAttribute to set Required which is used during deserialization to validate the presence of required JSON properties.

Sample
Types
public class Videogame
{
    [JsonProperty(Required = Required.Always)]
    public string Name { get; set; }

    [JsonProperty(Required = Required.AllowNull)]
    public DateTime? ReleaseDate { get; set; }
}
Usage
string json = @"{
  'Name': 'Starcraft III',
  'ReleaseDate': null
}";

Videogame starcraft = JsonConvert.DeserializeObject<Videogame>(json);

Console.WriteLine(starcraft.Name);
// Starcraft III

Console.WriteLine(starcraft.ReleaseDate);
// null