Skip to content

Instantly share code, notes, and snippets.

@kshyju
Created July 25, 2025 20:41
Show Gist options
  • Save kshyju/04bd2c8d2891bd8df8b2e0d711276984 to your computer and use it in GitHub Desktop.
Save kshyju/04bd2c8d2891bd8df8b2e0d711276984 to your computer and use it in GitHub Desktop.
STJ throws different serialization error in net9+ compared to net8.0
using System.Text.Json;
namespace STJTFMTest
{
internal class Program
{
static void Main(string[] args)
{
var personCollection = new[]
{
new
{
Name = "Jane",
Age = 25,
Address = new
{
Street = "456 Elm St"
}
}
};
using var memoryStream = new MemoryStream();
System.Text.Json.JsonSerializer.Serialize(memoryStream, personCollection);
memoryStream.Position = 0;
Deserialize(memoryStream, typeof(Stream[]), CancellationToken.None);
}
// Below method has code as Azure.Core.Serialization.JsonObjectSerializer
public static object? Deserialize(Stream stream, Type returnType, CancellationToken cancellationToken)
{
using MemoryStream memoryStream = new MemoryStream();
stream.CopyTo(memoryStream);
return JsonSerializer.Deserialize(memoryStream.ToArray(), returnType, new JsonSerializerOptions { });
// net8.0 exception:
// System.NotSupportedException: 'Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with
// net9.0+ exception
// System.NotSupportedException: 'Deserialization of interface or abstract types is not supported. Type 'System.IO.Stream'. Path: $[0] | LineNumber: 0 | BytePositionInLine: 2.'
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment