Repository navigation
Closed
Labels
Milestone
Description
Using .NET Core 3.0 or 3.1.100-preview3-014645, an ICollection<string> property with a private backing field assigned a custom implementation fails to serialize.
public class Client
{
private ICollection<string> _allowedGrantTypes = new GrantTypeValidatingHashSet();
// this works
// public GrantTypeValidatingHashSet AllowedGrantTypes { get; set; }
// this works
// public ICollection<string> AllowedGrantTypes { get; set; } = new GrantTypeValidatingHashSet();
// this errors
public ICollection<string> AllowedGrantTypes
{
get { return _allowedGrantTypes; }
set
{
_allowedGrantTypes = new GrantTypeValidatingHashSet();
}
}
}
Error message
System.InvalidCastException: 'Unable to cast object of type 'SerializationSample.GrantTypeValidatingHashSet' to type 'System.Collections.Generic.IList`1[System.String]'.'
Stack trace
at System.Text.Json.JsonSerializer.ApplyValueToEnumerable[TProperty](TProperty& value, ReadStack& state)
at System.Text.Json.JsonPropertyInfoNotNullable`4.OnReadEnumerable(ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonPropertyInfo.ReadEnumerable(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonPropertyInfo.Read(JsonTokenType tokenType, ReadStack& state, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)
at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)
at System.Text.Json.JsonSerializer.Deserialize(String json, Type returnType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at SerializationSample.Program.Main() in C:\SerializationSample\SerializationSample\Program.cs:line 13
This was discovered while attempting to serialize a Client model from IdentityServer4. I've created a bare-bones project duplicating the error. Serialization occurs correctly with Newtonsoft.Json.
Reacted by Sebastian Gingter and Giovanni Quarella
Activity
Fr1z2r commentedon Jan 30, 2020
Here is another simpler example. This code throws an exception when IgnoreNullValues=true, and doesn't throw when IgnoreNullValues=false:
gingters commentedon Feb 18, 2020
This should also be fixed in 3.0 (still in Maintenance) and 3.1 (LTS). I reproduced that issue also in 3.1.101 and in 3.1.200-preview-014883.
GioviQ commentedon Mar 27, 2020
After this upgrade https://devblogs.microsoft.com/aspnet/blazor-webassembly-3-2-0-preview-3-release-now-available/
I still have the issue of @folkcoder
benedict1986 commentedon Mar 29, 2020
I am using 3.1.0 and also having this issue.
GioviQ commentedon Mar 29, 2020
In fact System.Text.Json.JsonSerializer does not support HashSet<>
am11 commentedon Mar 29, 2020
Seems like it has been fixed in the master branch for .NET 5 - https://github.com/dotnet/installer/blob/master/README.md#installers-and-binaries:
benedict1986 commentedon Mar 29, 2020
Does it mean I won't be able to use it in my Asp.Net Core 3.1 project? That sounds terrible since .net core 5.0 is in preview and some other dependencies may have some issue.
GioviQ commentedon Mar 29, 2020
Until then
benedict1986 commentedon Mar 30, 2020
Thank you @GioviQ for your tip. I eventually make it working by installing
Microsoft.AspNetCore.Mvc.NewtonsoftJsonand then use.AddNewtonsoftJson()in the following location: