Unfortunately, there is currently no support "out-of-the-box" in System.Text.Json
to convert nullable enums.
However, there is a solution by using your own custom converter. (see below).
The solution. Use a custom converter.
You would attach can attach it to your property by decorating it with the custom converter:
// using System.Text.Json
[JsonConverter(typeof(StringNullableEnumConverter<UserStatus?>))] // Note the '?'
public UserStatus? Status { get; set; } // Nullable Enum
Here is the converter:
public class StringNullableEnumConverter<T> : JsonConverter<T>
{
private readonly JsonConverter<T> _converter;
private readonly Type _underlyingType;
public StringNullableEnumConverter() : this(null) { }
public StringNullableEnumConverter(JsonSerializerOptions options)
{
// for performance, use the existing converter if available
if (options != null)
{
_converter = (JsonConverter<T>)options.GetConverter(typeof(T));
}
// cache the underlying type
_underlyingType = Nullable.GetUnderlyingType(typeof(T));
}
public override bool CanConvert(Type typeToConvert)
{
return typeof(T).IsAssignableFrom(typeToConvert);
}
public override T Read(ref Utf8JsonReader reader,
Type typeToConvert, JsonSerializerOptions options)
{
if (_converter != null)
{
return _converter.Read(ref reader, _underlyingType, options);
}
string value = reader.GetString();
if (String.IsNullOrEmpty(value)) return default;
// for performance, parse with ignoreCase:false first.
if (!Enum.TryParse(_underlyingType, value,
ignoreCase: false, out object result)
&& !Enum.TryParse(_underlyingType, value,
ignoreCase: true, out result))
{
throw new JsonException(
$"Unable to convert \"{value}\" to Enum \"{_underlyingType}\".");
}
return (T)result;
}
public override void Write(Utf8JsonWriter writer,
T value, JsonSerializerOptions options)
{
writer.WriteStringValue(value?.ToString());
}
}
Hope that helps until there is native support without the need for a custom converter!