Handle missing members during deserialization

By default, if the JSON payload you're deserializing contains properties that don't exist in the deserialized plain old CLR object (POCO) type, they're simply ignored. Starting in .NET 8, you can specify that all members must be present in the payload. If they're not, a JsonException exception is thrown. You can configure this behavior in one of three ways:

  • Annotate your POCO type with the [<xref:System.Text.Json.Serialization.JsonUnmappedMemberHandlingAttribute>] attribute, specifying either to Skip or Disallow missing members.

    [JsonUnmappedMemberHandling(JsonUnmappedMemberHandling.Disallow)]
    public class MyPoco
    {
         public int Id { get; set; }
    }
    
    JsonSerializer.Deserialize<MyPoco>("""{"Id" : 42, "AnotherId" : -1 }"""); 
    // JsonException : The JSON property 'AnotherId' could not be mapped to any .NET member contained in type 'MyPoco'.
    
  • Set JsonSerializerOptions.UnmappedMemberHandling to either Skip or Disallow.

  • Customize the JsonTypeInfo contract for the relevant type. (For information about customizing a contract, see Customize a JSON contract.)