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

System.Text.Json roundtrips Stack<T> instances in reverse order #83086

Open
eiriktsarpalis opened this issue Mar 7, 2023 · 3 comments
Open

Comments

@eiriktsarpalis
Copy link
Member

The code

var stack  = new Stack<int>(new int[] { 3, 2, 1 });

for (int i = 0; i < 5; i++)
{
    string json = JsonSerializer.Serialize(stack);
    Console.WriteLine(json);
    stack = JsonSerializer.Deserialize<Stack<int>>(json);
}

Produces

[1,2,3]
[3,2,1]
[1,2,3]
[3,2,1]
[1,2,3]

Even though this can be expected given the nature of stacks in general, it certainly violates expectations when it comes to marshalling collections. FWIW this is not specific to STJ as similar behavior can be observed when applying a stack to its own IEnumerable constructor.

@eiriktsarpalis eiriktsarpalis added this to the Future milestone Mar 7, 2023
@ghost ghost added the untriaged New issue has not been triaged by the area owner label Mar 7, 2023
@ghost
Copy link

ghost commented Mar 7, 2023

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

Issue Details

The code

var stack  = new Stack<int>(new int[] { 3, 2, 1 });

for (int i = 0; i < 5; i++)
{
    string json = JsonSerializer.Serialize(stack);
    Console.WriteLine(json);
    stack = JsonSerializer.Deserialize<Stack<int>>(json);
}

Produces

[1,2,3]
[3,2,1]
[1,2,3]
[3,2,1]
[1,2,3]

Even though this can be expected given the nature of stacks in general, it certainly violates expectations when it comes to marshalling collections. FWIW this is not specific to STJ as similar behavior can be observed when applying a stack to its own IEnumerable constructor.

Author: eiriktsarpalis
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Mar 7, 2023
@eiriktsarpalis
Copy link
Member Author

eiriktsarpalis commented Mar 7, 2023

FWIW I'm not necessarily proposing that we fix this (users might rely on the current behavior 😱) although we should make sure that it is at least documented given that the type is officially supported.

@gregsdennis
Copy link
Contributor

My opinion is that if people are depending upon bug behavior, they shouldn't be surprised when that big is fixed, even if they don't know it's a bug.

I think fix it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants