Json.NET MissingMemberHandling setting

前端 未结 1 1004
灰色年华
灰色年华 2020-12-18 22:45

I would like Json.NET to throw a JsonSerializationException when the Json string is missing a property that the C# class requires.

相关标签:
1条回答
  • 2020-12-18 23:08

    You have to set the P2 property to mandatory with the JsonPropertyAttribute

    public class ApiMessage
    {
        public string P1 { get; set; }
        [JsonProperty(Required = Required.Always)]
        public string P2 { get; set; }
    }
    

    With your example, you will get an JsonSerializationException.

    Hope it helps!

    0 讨论(0)
提交回复
热议问题