-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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.Serialization: Converters are ignored for types derived from supported base types #30619
Comments
@phizch, thanks for the detailed description - I was able to repro this. This is something we should fix for 3.0, and I've opened dotnet/corefx#40411 to address it.
This has been fixed in dotnet/corefx#40401. |
Re-opening until its fixed for 3.0. |
Closed in 3.0 by dotnet/corefx#40432. |
This bug is marked as fixed, although I am seeing a very similar bug in which my |
@Rauce I only reported the issue, and didn't have anything to do with the PR. That said, I did some testing and I can confirm that the example you gave in the stackoverflow question is not working as it should. It seems like it's ignoring the A workaround is to use class QueryStringToDictionaryJsonConverter : JsonConverter<Dictionary<string,string>>
{
...
}
class Test
{
public Dictionary<string,string> Parameters { get; set; }
}
static void Main( string[] args )
{
string json = "{ \"Parameters\": \"key1=value1&key2=value2\" }";
var options = new JsonSerializerOptions
{
Converters = { new QueryStringToDictionaryJsonConverter() }
};
var result = JsonSerializer.Deserialize<Test>( json, options );
} A potential problem is that all [JsonConverter( typeof( QueryStringDictionaryConverter ) )]
class QueryStringDictionary : Dictionary<string,string> { }
class QueryStringDictionaryConverter : JsonConverter<QueryStringDictionary>
{
...
} Hope this helps. As an aside, it seems like at least one of my cases in this issue still doesn't work; public class A
{
[JsonConverter(typeof(MyCollectionConverter))] // no worky
public MyCollection SpecialList { get; set; }
public List<int> NormalList { get; set; }
} It's not critical though, since the preferred way is to set the attribute on the |
@Rauce, can you please file a separate issue for this so we can track and fix the issue you are observing Thanks! |
@layomia - can you take a look at this:
|
cc: @layomia,@ahsonkhan, @scalablecory, @steveharter
Related:
Description
Pull dotnet/corefx#39001 implemented by @layomia added support for serialization of types that implements natively supported collections without needing to create converters for them.
Unfortunately the implementation treats all derived types as the base type, so it's not possible to create specialized collections with converters. E.g.
class MyCollection : List<int>
gets treated asList<int>
.Specialized collections is necessary if the json has multiple ways of declaring a list, e.g.
Expected priorities for getting the converter:
Current behavior
From
JsonClassInfo.AddProperty
[source]FromJsonClassInfo.AddProperty
source:In my example
GetImplementedCollectionType(typeof(MyCollection))
will returnList<int>
.I'm currently looking into it to see if I can find a fix.
--
Just found another bug: Since
propertyInfo
is ignored anyJsonIgnoreAttribute
will also be ignored.e.g.
The text was updated successfully, but these errors were encountered: