docs icon indicating copy to clipboard operation
docs copied to clipboard

Docs and nullability annotations disagree over the nullability of `JsonSerializerOptions.GetConverter`

Open tautropfli opened this issue 3 years ago • 0 comments

JsonSerializerOptions.GetConverter returns a non-null JsonConverter according to the nullability attributes.

The XML docs disagree with this:

Returns

The first converter that supports the given type, or null if there is no converter.

The linked guide also disagrees: Both the Read and Write method in the JsonConverterFactory example handle null returns from GetConverter:

// ...

private class DictionaryEnumConverterInner<TKey, TValue> :
    JsonConverter<Dictionary<TKey, TValue>> where TKey : struct, Enum
{
    private readonly JsonConverter<TValue> _valueConverter;

    public DictionaryEnumConverterInner(JsonSerializerOptions options)
    {
        // For performance, use the existing converter if available.
        _valueConverter = (JsonConverter<TValue>)options
            .GetConverter(typeof(TValue));
        // ...
    }

    public override Dictionary<TKey, TValue> Read(
        ref Utf8JsonReader reader,
        Type typeToConvert,
        JsonSerializerOptions options)
    {
        // ...

        if (_valueConverter != null)
        {
            // ...
            _valueConverter.Read(ref reader, _valueType, options)!;
        }
        else
        {
            JsonSerializer.Deserialize<TValue>(ref reader, options)!;
        }

        // ...
    }

    public override void Write(
        Utf8JsonWriter writer,
        Dictionary<TKey, TValue> dictionary,
        JsonSerializerOptions options)
    {
        // ...

        if (_valueConverter != null)
        {
            _valueConverter.Write(...);
        }
        else
        {
            JsonSerializer.Serialize(...);
        }
    }
}

What's the real nullability of this method?


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

tautropfli avatar Sep 12 '22 13:09 tautropfli