Skip to content

JsonConverter.Read/WriteAsPropertyName methods throw NotSupportedException when invoked in built-in converters #77326

@eiriktsarpalis

Description

@eiriktsarpalis

The code

var options = new JsonSerializerOptions();
var writer = new Utf8JsonWriter(new MemoryStream());

writer.WriteStartObject();
JsonConverter<int> converter = JsonMetadataServices.Int32Converter;
converter.WriteAsPropertyName(writer, 42, options);

throws the following exception

Unhandled exception. System.NotSupportedException: The type 'System.Int32' is not a supported dictionary key using converter of type 'System.Text.Json.Serialization.Converters.Int32Converter'.
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DictionaryKeyTypeNotSupported(Type keyType, JsonConverter converter)
   at System.Text.Json.Serialization.JsonConverter`1.WriteAsPropertyName(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
   at Program.<Main>$(String[] args)

Even though the converter does support serializing to dictionary keys.

The issue does not manifest when the serializer calls into the built-in converters because it follows uses the Read/WriteAsPropertyNameCore methods instead. For consistency, we should make sure that the public methods behave as expected when invoked by user code.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions