This seems to be a bug in the serializer. In .NET Core 3.1 there is some code in CreateDerivedEnumerableInstance(ref ReadStack state, JsonPropertyInfo collectionPropertyInfo, IList sourceList) to create a stack from a deserialized list:
else if (instance is Stack<TDeclaredProperty> instanceOfStack)
{
foreach (TDeclaredProperty item in sourceList)
{
instanceOfStack.Push(item);
}
return instanceOfStack;
}
However, it pushes them on in the wrong order. Thus a custom JsonConverter<Stack<T>> will be required to correctly deserialize a Stack<T>. In addition, a JsonConverterFactory can be used to manufacture an appropriate converter for every stack type Stack<T>:
public class StackConverterFactory : JsonConverterFactory
{
public override bool CanConvert(Type typeToConvert)
{
return GetStackItemType(typeToConvert) != null;
}
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options)
{
var itemType = GetStackItemType(typeToConvert);
var converterType = typeof(StackConverter<,>).MakeGenericType(typeToConvert, itemType);
return (JsonConverter)Activator.CreateInstance(converterType);
}
static Type GetStackItemType(Type type)
{
while (type != null)
{
if (type.IsGenericType)
{
var genType = type.GetGenericTypeDefinition();
if (genType == typeof(Stack<>))
return type.GetGenericArguments()[0];
}
type = type.BaseType;
}
return null;
}
}
public class StackConverter<TItem> : StackConverter<Stack<TItem>, TItem>
{
}
public class StackConverter<TStack, TItem> : JsonConverter<TStack> where TStack : Stack<TItem>, new()
{
public override TStack Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var list = JsonSerializer.Deserialize<List<TItem>>(ref reader, options);
if (list == null)
return null;
var stack = typeToConvert == typeof(Stack<TItem>) ? (TStack)new Stack<TItem>(list.Count) : new TStack();
for (int i = list.Count - 1; i >= 0; i--)
stack.Push(list[i]);
return stack;
}
public override void Write(Utf8JsonWriter writer, TStack value, JsonSerializerOptions options)
{
writer.WriteStartArray();
foreach (var item in value)
JsonSerializer.Serialize(writer, item, options);
writer.WriteEndArray();
}
}
Then use it in JsonSerializerOptions as follows:
var stack = new Stack<int>(new [] { 1, 2, 3 });
var options = new JsonSerializerOptions
{
Converters = { new StackConverterFactory() },
};
var json = JsonSerializer.Serialize(stack, options);
var stack2 = JsonSerializer.Deserialize<Stack<int>>(json, options);
var json2 = JsonSerializer.Serialize(stack2, options);
Assert.IsTrue(stack.SequenceEqual(stack2)); // Passes
Assert.IsTrue(json == json2); // Passes
The converter could also be applied directly to some data model using JsonConverterAttribute
public class Model
{
[JsonConverter(typeof(StackConverter<int>))]
public Stack<int> Stack { get; set; }
}
Demo fiddle here.
Update: Looks round-tripping of Stack<T> will not be built into JsonSerializer. See (De)serializing stacks with JsonSerializer should round-trip #41887 (Closed):
We shouldn't do this. There's no standard on which side to reverse the items (serialization or deserialization) in order to roundtrip, so it is a non-starter as a breaking change candidate. The current behavior is compatible with Newtonsoft.Json behavior.
There's a work item to provide a sample converter showing how to roundtrip in the JSON docs which I think should suffice as a resolution for this issue: dotnet/docs#16690. Here's what this converter could look like - dotnet/docs#16225 (comment).