Skip to content

System.Text.Json fails on deserializing into sub-subclasses of system collections #31009

Closed
dotnet/corefx
#41482

Description

@FyiurAmron

When trying to deserialize into a class that has a class being a valid deserialization target for a collection higher in the class inheritance graph than its direct parent, the deserialization fails.

Example code:

namespace JsonProblem {

using System.Collections.Generic;
using System.Text.Json;

class ListString : List<string> {
}

class MyList<T> : List<T> {
}

class MyMyList<T> : MyList<T> {
}

class MyListString : MyList<string> {
}

class Program {
    const string JSON = "[\"test\"]";

    static void Main( string[] args ) {
        JsonSerializer.Deserialize( JSON, typeof(string[]) ); // works
        JsonSerializer.Deserialize( JSON, typeof(List<string>) ); // works
        JsonSerializer.Deserialize( JSON, typeof(ListString) ); // also works
        JsonSerializer.Deserialize( JSON, typeof(MyList<string>) ); // works
        JsonSerializer.Deserialize( JSON, typeof(MyMyList<string>) ); // doesn't work
        JsonSerializer.Deserialize( JSON, typeof(MyListString) ); // doesn't work
    }
}

}

All the above classes worked OK in Json.NET - IMVHO, the deserializer should handle them, which it doesn't.

Activity

  1. steveharter commented on Sep 30, 2019

    @steveharter
    Contributor

    cc @layomia it appears that deserialization only works for one additional level of inheritance with generics.

  2. self-assigned this
    on Oct 16, 2019
  3. ahsonkhan commented on Oct 21, 2019

    @ahsonkhan
    Contributor

    Re-opening until fixed for 3.1

  4. ahsonkhan commented on Oct 24, 2019

    @ahsonkhan
    Contributor

    This issue has been fixed in release/3.1 (dotnet/corefx#41523).

    @FyiurAmron, can you please use the 3.1-preview1 (or master) build and verify the issue has been fixed?

    https://www.nuget.org/packages/System.Text.Json/4.7.0-preview1.19504.10

    You can reference the latest master System.Text.Json NuGet package from https://dotnetfeed.blob.core.windows.net/dotnet-core/index.json

  5. FyiurAmron commented on Oct 27, 2019

    @FyiurAmron
    ContributorAuthor
  6. ericstj commented on Oct 29, 2019

    @ericstj
    Member

    Closing as fix is in 3.1, some tests are remaining to be merged once branch opens but we can let the PR track those.

  7. transferred this issue fromdotnet/corefxon Feb 1, 2020
  8. added this to the 3.1 milestone on Feb 1, 2020
  9. ghost locked as resolved and limited conversation to collaborators on Dec 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions