-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Behavior difference in JsonSerializer between .NET7 and .NET8 p4 #86419
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsWhile testing .NET 8, I got an exception when deserializing a json payload. Here's a repro code: var obj = JsonSerializer.Deserialize<Sample>("""{ "A": 1 }""");
Console.WriteLine(obj);
record Sample
{
public int A { get; set; }
// Encoding is not serializable, but it is never set in the json data
public Encoding? Dummy { get; set; }
}
full stacktrace
The fix is to decorate the unused property with I don't know if this is a bug or expected behavior (both would make sense). If this is the expected behavior, it would be worth adding a note in the Breaking change section.
|
This was introduced intentionally in #81576, in order to support #71933. This essentially forces eager evaluation of the entire type graph before serialization begins, irrespective of what data is present in the deserialized payload. I had not anticipated that users might have taken a dependency on that behavior, since it should be pointed out that serializing such a type will always fail even in .NET 7.
That's an excellent point, I'll make sure that this is noted adequately in the upcoming release's documentation. |
Added
Tagging @dotnet/compat for awareness of the breaking change. |
I suggest to use JsonConverter for encoding (i.e. you can serialize it as int - code page, or string - encoding name, i.e. |
Breaking change issue filed, we can now close this issue. |
While testing .NET 8, I got an exception when deserializing a json payload. Here's a repro code:
Sample { A = 1, Dummy = }
System.InvalidOperationException: The type 'System.ReadOnlySpan`1[System.Byte]' of property 'Preamble' on type 'System.Text.Encoding' is invalid for serialization or deserialization because it is a pointer type, is a ref struct, or contains generic parameters that have not been replaced by specific types.
full stacktrace
The fix is to decorate the unused property with
[JsonIgnore]
.I don't know if this is a bug or expected behavior (both would make sense). If this is the expected behavior, it would be worth adding a note in the Breaking change section.
The text was updated successfully, but these errors were encountered: