Handle unmapped members during deserialization
By default, if the JSON payload you're deserializing contains properties that don't exist in the plain old CLR object (POCO) type, they're simply ignored. Starting in .NET 8, you can specify that all payload properties must exist in the POCO. If they're not, a JsonException exception is thrown during deserialization. You can configure this behavior in one of three ways:
Annotate your POCO type with the JsonUnmappedMemberHandlingAttribute attribute, specifying either to Skip or Disallow unmapped 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.)