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

Investigate ways to extend deserialization support for arbitrary collection types #38514

Closed
hughbe opened this issue Jun 28, 2020 · 3 comments
Closed
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Text.Json Priority:2 Work that is important, but not critical for the release
Milestone

Comments

@hughbe
Copy link
Contributor

hughbe commented Jun 28, 2020

//var s = JsonSerializer.Deserialize<ConcurrentBag<int>>("[1,2,3]");
//var s = JsonConvert.DeserializeObject<ConcurrentBag<int>>("[1,2,3]");
Console.WriteLine($"Count: {s.Count}");
var t = new List<int>(s);
Console.WriteLine(t[0]);
Console.WriteLine(t[1]);
Console.WriteLine(t[2]);

Output with JsonConvert:

Count: 3
3
2
1

Output with JsonSerializer:

Unhandled exception. System.NotSupportedException: The collection type 'System.Collections.Concurrent.ConcurrentBag`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.
 ---> System.NotSupportedException: The collection type 'System.Collections.Concurrent.ConcurrentBag`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated.
   --- End of inner exception stack trace ---
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException(ReadStack& state, Utf8JsonReader& reader, NotSupportedException ex)
   at System.Text.Json.ThrowHelper.ThrowNotSupportedException_CannotPopulateCollection(Type type, Utf8JsonReader& reader, ReadStack& state)
   at System.Text.Json.Serialization.Converters.IEnumerableOfTConverter`2.CreateCollection(Utf8JsonReader& reader, ReadStack& state, JsonSerializerOptions options)
   at System.Text.Json.Serialization.Converters.IEnumerableDefaultConverter`2.OnTryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, TCollection& value)
   at System.Text.Json.Serialization.JsonConverter`1.TryRead(Utf8JsonReader& reader, Type typeToConvert, JsonSerializerOptions options, ReadStack& state, T& value)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadCore[TValue](Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, Type returnType, JsonSerializerOptions options)
   at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
   at t.Program.Main(String[] args) in /Users/hugh/Documents/GitHub/t/Program.cs:line 13
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Text.Json untriaged New issue has not been triaged by the area owner labels Jun 28, 2020
@layomia layomia removed the untriaged New issue has not been triaged by the area owner label Jun 29, 2020
@layomia layomia added this to the Future milestone Jun 29, 2020
@eiriktsarpalis
Copy link
Member

There are many collection types that STJ can't deserialize out of the box, but we simply can't add converter for every type in BCL. Instead, we should be looking at potential extensibility mechanisms that let users specify deserialization constructors for arbitrary collection types (including ones that are not in BCL).

@eiriktsarpalis eiriktsarpalis modified the milestones: Future, 7.0.0 Oct 15, 2021
@eiriktsarpalis eiriktsarpalis changed the title JsonSerializer doesn't support deserialisation to ConcurrentBag Investigate ways to extend deserialization support for arbitrary collection types Oct 15, 2021
@eiriktsarpalis eiriktsarpalis added api-suggestion Early API idea and discussion, it is NOT ready for implementation api-needs-work API needs work before it is approved, it is NOT ready for implementation labels Oct 15, 2021
@ghost
Copy link

ghost commented Oct 15, 2021

This issue has been marked with the api-needs-work label. This may suggest that the proposal requires further refinement before it can be considered for API review. Please refer to our API review guidelines for a detailed description of the process.

When ready to submit an amended proposal, please ensure that the original post in this issue has been updated, following the API proposal template and examples as provided in the guidelines.

@eiriktsarpalis eiriktsarpalis added the Priority:2 Work that is important, but not critical for the release label Oct 15, 2021
@eiriktsarpalis eiriktsarpalis removed the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 16, 2021
@eiriktsarpalis
Copy link
Member

Closing in favor of #63791.

@ghost ghost locked as resolved and limited conversation to collaborators Feb 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-needs-work API needs work before it is approved, it is NOT ready for implementation area-System.Text.Json Priority:2 Work that is important, but not critical for the release
Projects
None yet
Development

No branches or pull requests

4 participants