Skip to content

Latest commit

 

History

History
39 lines (32 loc) · 1.34 KB

JsonPropertyRequired.md

File metadata and controls

39 lines (32 loc) · 1.34 KB

JsonPropertyAttribute required

This sample uses Argon.JsonPropertyAttribute to set Argon.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; }
}

snippet source | anchor

var json = """
    {
      'Name': 'Starcraft III',
      'ReleaseDate': null
    }
    """;

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

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

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

snippet source | anchor