docs
docs copied to clipboard
Docs and nullability annotations disagree over the nullability of `JsonSerializerOptions.GetConverter`
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
nullif 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.
- ID: 5de37d79-751f-e372-e866-efde28f4908c
- Version Independent ID: 678c4f23-764a-7ccd-0d6d-159a30957477
- Content: How to write custom converters for JSON serialization - .NET
- Content Source: docs/standard/serialization/system-text-json-converters-how-to.md
- Product: dotnet-fundamentals
- GitHub Login: @gewarren
- Microsoft Alias: gewarren