Muokkaa

Jaa


JsonSerializerOptions.RespectNullableAnnotations Property

Definition

Gets or sets a value that indicates whether nullability annotations should be respected during serialization and deserialization.

public:
 property bool RespectNullableAnnotations { bool get(); void set(bool value); };
public bool RespectNullableAnnotations { get; set; }
member this.RespectNullableAnnotations : bool with get, set
Public Property RespectNullableAnnotations As Boolean

Property Value

Exceptions

This property is set after serialization or deserialization has occurred.

Remarks

Configures the serializer to throw an exception when trying to serialize a null value from a non-nullable property getter, or when deserializing a null value into a non-nullable property setter or constructor parameter. Nullability annotations are resolved from the properties, fields, and constructor parameters that are used by the serializer. This includes annotations stemming from attributes such as NotNullAttribute, MaybeNullAttribute, AllowNullAttribute and DisallowNullAttribute.

Due to restrictions in how nullable reference types are represented at run time, this setting only governs nullability annotations of non-generic properties, fields, and constructor parameters. It cannot be used to enforce nullability annotations of root-level types, collection elements, or generic parameters. The default setting for this property can be toggled application-wide using the "System.Text.Json.Serialization.RespectNullableAnnotationsDefault" feature switch.

Nullability validation functions independently of required-ness validation, that is to say the setting has no effect in scenarios where the payload is missing a required property. This can be configured independently using the required keyword, the JsonRequiredAttribute attribute, or the RespectRequiredConstructorParameters property.

It is recommended that new applications always set this property to true, in combination with the closely related RespectRequiredConstructorParameters property.

Applies to