Skip to content

System.Text.Json: Serializing IDictionary with nullable value type throws "Specified method is not supported." #30799

@Daniel15

Description

@Daniel15

Repro:

IDictionary<string, float?> dict = new Dictionary<string, float?>
{
	{"Y2016", 42.0f}
};

JsonSerializer.Serialize(new
{
	Foo = dict
});

Throws:

NotSupportedException: Specified method is not supported.

    System.Text.Json.JsonSerializer.WriteDictionary<TProperty>(JsonConverter<TProperty> converter, JsonSerializerOptions options, ref WriteStackFrame current, Utf8JsonWriter writer)
    System.Text.Json.JsonPropertyInfoNotNullable<TClass, TDeclaredProperty, TRuntimeProperty, TConverter>.OnWriteDictionary(ref WriteStackFrame current, Utf8JsonWriter writer)
    System.Text.Json.JsonPropertyInfo.WriteDictionary(ref WriteStack state, Utf8JsonWriter writer)
    System.Text.Json.JsonSerializer.HandleDictionary(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, ref WriteStack state)
    System.Text.Json.JsonSerializer.HandleObject(JsonPropertyInfo jsonPropertyInfo, JsonSerializerOptions options, Utf8JsonWriter writer, ref WriteStack state)
    System.Text.Json.JsonSerializer.WriteObject(JsonSerializerOptions options, Utf8JsonWriter writer, ref WriteStack state)
    System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, int originalWriterDepth, int flushThreshold, JsonSerializerOptions options, ref WriteStack state)
    System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, PooledByteBufferWriter output, object value, Type type, JsonSerializerOptions options)
    System.Text.Json.JsonSerializer.WriteCore(PooledByteBufferWriter output, object value, Type type, JsonSerializerOptions options)
    System.Text.Json.JsonSerializer.WriteCoreString(object value, Type type, JsonSerializerOptions options)
    System.Text.Json.JsonSerializer.Serialize<TValue>(TValue value, JsonSerializerOptions options)

Using Dictionary instead of IDictionary fixes it.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions