The following code
string json = System.Text.Json.JsonSerializer.Serialize(new Dictionary<int, int>());
Throws an exception
System.NotSupportedException
HResult=0x80131515
Message=The collection type 'System.Collections.Generic.Dictionary`2[System.Int32,System.Int32]' is not supported.
Source=System.Text.Json
StackTrace:
at System.Text.Json.JsonClassInfo.GetElementType(Type propertyType, Type parentType, MemberInfo memberInfo, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, Type implementedPropertyType, PropertyInfo propertyInfo, Type parentClassType, JsonConverter converter, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.AddProperty(Type propertyType, PropertyInfo propertyInfo, Type classType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo.AddPolicyProperty(Type propertyType, JsonSerializerOptions options)
at System.Text.Json.JsonClassInfo..ctor(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type classType)
at System.Text.Json.WriteStackFrame.Initialize(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCore(Utf8JsonWriter writer, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCore(PooledByteBufferWriter output, Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.WriteCoreString(Object value, Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)
at ConsoleApp18.Program.Main(String[] args) in C:\Users\x\source\repos\ConsoleApp18\ConsoleApp18\Program.cs:line 11
The Serialize method has an optional parameter JsonSerializerOptions.
You can provide a "Converter": https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-converters-how-to?view=netcore-3.1#registration-sample---converters-collection
My understanding is that this is currently by design in 3.x.
There's a good discussion of this issue you can follow in #30524. This may be a duplicate of that issue (though the issue title could be updated for clarity).
The current state is that there is a proposal in progress, see https://github.com/dotnet/runtime/pull/32676
Most helpful comment
My understanding is that this is currently by design in 3.x.
There's a good discussion of this issue you can follow in #30524. This may be a duplicate of that issue (though the issue title could be updated for clarity).
The current state is that there is a proposal in progress, see https://github.com/dotnet/runtime/pull/32676