Closed
Description
//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