JsonPropertyAttribute required |
This sample uses JsonPropertyAttribute to set Required which is used during deserialization to validate the presence of required JSON properties.
public class Videogame { [JsonProperty(Required = Required.Always)] public string Name { get; set; } [JsonProperty(Required = Required.AllowNull)] public DateTime? ReleaseDate { get; set; } }
string json = @"{ 'Name': 'Starcraft III', 'ReleaseDate': null }"; Videogame starcraft = JsonConvert.DeserializeObject<Videogame>(json); Console.WriteLine(starcraft.Name); // Starcraft III Console.WriteLine(starcraft.ReleaseDate); // null