BO4E/meta/LenientConverters/NullableEnumConverter.cs
/// <summary>
///     Read from the json reader.
///     <see cref="System.Text.Json.Serialization.JsonConverter{T}.Read(ref Utf8JsonReader, Type, JsonSerializerOptions)" />
/// </summary>
/// <param name="reader"></param>
/// <param name="typeToConvert"></param>
/// <param name="options"></param>
/// <returns></returns>
Read from the json reader.
Read from the json reader .
public override T Read(ref Utf8JsonReader reader,
            Type typeToConvert, JsonSerializerOptions options)
        {
            if (_converter != null) return _converter.Read(ref reader, _underlyingType, options);
            if (reader.TokenType == JsonTokenType.Null)
                return default;
            if (reader.TokenType == JsonTokenType.String)
            {
                var value = reader.GetString();
                if (string.IsNullOrEmpty(value)) return default;
                try
                {
                    var result = Enum.Parse(_underlyingType, value, true);
                    return (T)result;
                }
                catch (Exception)
                {
                    return default;
                }
            }
            if (reader.TokenType == JsonTokenType.Number)
            {
                return (T)Enum.ToObject(_underlyingType, reader.GetInt32());
            }
            return default;
        }
public override T Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options ) { if ( _converter != null ) return _converter . Read ( ref reader , _underlyingType , options ) ; if ( reader . TokenType == JsonTokenType . Null ) return default ; if ( reader . TokenType == JsonTokenType . String ) { var value = reader . GetString ( ) ; if ( string . IsNullOrEmpty ( value ) ) return default ; try { var result = Enum . Parse ( _underlyingType , value , true ) ; return ( T ) result ; } catch ( Exception ) { return default ; } } if ( reader . TokenType == JsonTokenType . Number ) { return ( T ) Enum . ToObject ( _underlyingType , reader . GetInt32 ( ) ) ; } return default ; }
Read from the json reader.
Read from the json reader .
// for performance, parse with ignoreCase:false first.
None
Utf8JsonReader
ref
typeToConvert
Type
None
options
JsonSerializerOptions
None
reader
None