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.
I can repro this issue, ~but I believe nullable types are not supported, out-of-the-box~. As a workaround, you would have to create/register your own JsonConverter to handle nullable types.
The expected output, presumably, should be the following (which you would get if you had a dictionary/idictionary with non-nullable values): {"Foo":{"Y2016":42}}
Using
Dictionaryinstead ofIDictionaryfixes it.
That doesn't throw NSE, but it wouldn't do what you expect. The output is: {"Foo":{}}
cc @steveharter, @layomia
Why are nullable types not supported? That seems like a really odd omission.
In general I'm hitting many odd quirks with this new JSON serializer (many of which are already documented) so I think I'm just going to switch back to using Json.NET
Why are nullable types not supported?
but I believe nullable types are not supported, out-of-the-box.
Nevermind, I was mistaken. Nullable types should work. The scenario you shared highlights a bug specific to handling nullable types within dictionaries.
In general I'm hitting many odd quirks with this new JSON serializer (many of which are already documented)
Can you please share some of the issues you are hitting against? I would be interested in hearing that feedback.
Can you please share some of the issues you are hitting against?
I was trying to use an enum as the key in an IDictionary, and the serializer didn't like that (I think that's dotnet/runtime#30524). There were a few other issues but I forgot to write them down... I'm about to go to bed but I'll take a look at my apps/notes again tomorrow and see what the issues were.
Given where we are at for 3.0, I don't think this issue meets the bar. Moving to 3.0.x for now.
Fixed for master/5.0. Re-opening for consideration in 3.1.
Fixed for 3.1 in https://github.com/dotnet/corefx/pull/41210.
From @electricessence in https://github.com/dotnet/corefx/pull/40236#issuecomment-536148005:
After the release of .NET Core 3, I've been using
System.Text.Json. Mostly via .NET Standard 2.0.
Other than by adding custom JsonConverters, I am unable to escape nullable errors related toIDictionary<string,T>where T is eitherdouble?ordecimal?. I hate that I've had to make this work around. This is happening during de-serialization.This PR seems old enough that we should have seen these updates already. Any help here?
@ahsonkhan Thanks for referencing. So you're suggesting that this will be a non issue after 3.1 release. Until then I have to continue with my work around? When is 3.1 sited for release?
Most helpful comment
Why are nullable types not supported? That seems like a really odd omission.
In general I'm hitting many odd quirks with this new JSON serializer (many of which are already documented) so I think I'm just going to switch back to using Json.NET