Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[System.Text.Json] Dictionary is not supported #32710

Closed
mrpmorris opened this issue Feb 23, 2020 · 2 comments
Closed

[System.Text.Json] Dictionary is not supported #32710

mrpmorris opened this issue Feb 23, 2020 · 2 comments

Comments

@mrpmorris
Copy link

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



@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Feb 23, 2020
@artelk
Copy link

artelk commented Feb 23, 2020

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

@alanisaac
Copy link
Contributor

alanisaac commented Feb 23, 2020

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 #32676

@jozkee jozkee closed this as completed Feb 23, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Feb 24, 2020
@layomia layomia added this to the 5.0 milestone Feb 24, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants