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

Unable to deserialize ienumerables that is not a list #61993

Closed
musictopia2 opened this issue Nov 23, 2021 · 3 comments
Closed

Unable to deserialize ienumerables that is not a list #61993

musictopia2 opened this issue Nov 23, 2021 · 3 comments
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner

Comments

@musictopia2
Copy link

musictopia2 commented Nov 23, 2021

Description

If I have a custom list like this

public class SecondList<T> : IEnumerable<T>
{
    protected List<T> PrivateList = new();
    public SecondList()
    {

    }
    protected bool IsStart = true;
    public void Add(T item)
    {
        PrivateList.Add(item);
    }
    public IEnumerator<T> GetEnumerator()
    {
        return PrivateList.GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator()
    {
        return PrivateList.GetEnumerator();
    }
}

then when i try to deserialize it, then it gives me a runtime error saying "The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'"

that is obviously incorrect though.

I am able to deserialize no problem with newtonsofts. I need to transfer to system.json for better performance.
Is there any way to make it work with custom classes that implement ienumerable that behind the scenes use the standard list?

Reproduction Steps

in a global class, use something like this
global using jj = System.Text.Json;

then in main, use code like this

SecondList list = new()
{
1,
2,
3,
4
};

text = jj.JsonSerializer.Serialize(list);
Console.WriteLine(text);

SecondList? list2 = jj.JsonSerializer.Deserialize<SecondList>(text);
foreach (var item in list2!)
{
Console.WriteLine(item);
}

Expected behavior

The second set of code shows the deserialized and the proper numbers print.

Actual behavior

The runtime error with this message
The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'

There needs to be a way to deserialize even custom classes that implement ienumerable like newtonsoft has.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-issue-labeler dotnet-issue-labeler bot added area-System.Linq untriaged New issue has not been triaged by the area owner labels Nov 23, 2021
@ghost
Copy link

ghost commented Nov 23, 2021

Tagging subscribers to this area: @dotnet/area-system-linq
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

If I have a custom list like this
public class SecondList : IEnumerable
{
protected List PrivateList = new();
public SecondList()
{

}
protected bool IsStart = true;
public void Add(T item)
{
    PrivateList.Add(item);
}
public IEnumerator<T> GetEnumerator()
{
    return PrivateList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
    return PrivateList.GetEnumerator();
}

}

then when i try to deserialize it, then it gives me a runtime error saying "The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'"

that is obviously incorrect though.

I am able to deserialize no problem with newtonsofts. I need to transfer to system.json for better performance.
Is there any way to make it work with custom classes that implement ienumerable that behind the scenes use the standard list?

Reproduction Steps

in a global class, use something like this
global using jj = System.Text.Json;

then in main, use code like this

SecondList list = new()
{
1,
2,
3,
4
};

text = jj.JsonSerializer.Serialize(list);
Console.WriteLine(text);

SecondList? list2 = jj.JsonSerializer.Deserialize<SecondList>(text);
foreach (var item in list2!)
{
Console.WriteLine(item);
}

Expected behavior

The second set of code shows the deserialized and the proper numbers print.

Actual behavior

The runtime error with this message
The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'

There needs to be a way to deserialize even custom classes that implement ienumerable like newtonsoft has.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: musictopia2
Assignees: -
Labels:

area-System.Linq, untriaged

Milestone: -

@ghost
Copy link

ghost commented Nov 24, 2021

Tagging subscribers to this area: @dotnet/area-system-text-json
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

If I have a custom list like this
public class SecondList : IEnumerable
{
protected List PrivateList = new();
public SecondList()
{

}
protected bool IsStart = true;
public void Add(T item)
{
    PrivateList.Add(item);
}
public IEnumerator<T> GetEnumerator()
{
    return PrivateList.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
    return PrivateList.GetEnumerator();
}

}

then when i try to deserialize it, then it gives me a runtime error saying "The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'"

that is obviously incorrect though.

I am able to deserialize no problem with newtonsofts. I need to transfer to system.json for better performance.
Is there any way to make it work with custom classes that implement ienumerable that behind the scenes use the standard list?

Reproduction Steps

in a global class, use something like this
global using jj = System.Text.Json;

then in main, use code like this

SecondList list = new()
{
1,
2,
3,
4
};

text = jj.JsonSerializer.Serialize(list);
Console.WriteLine(text);

SecondList? list2 = jj.JsonSerializer.Deserialize<SecondList>(text);
foreach (var item in list2!)
{
Console.WriteLine(item);
}

Expected behavior

The second set of code shows the deserialized and the proper numbers print.

Actual behavior

The runtime error with this message
The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'

There needs to be a way to deserialize even custom classes that implement ienumerable like newtonsoft has.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: musictopia2
Assignees: -
Labels:

area-System.Text.Json, untriaged

Milestone: -

@eiriktsarpalis
Copy link
Member

Duplicate of #38514. One workaround is to implement a custom converter for your collection.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 24, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Text.Json untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants