-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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 Serializer ignores JsonIgnore when generating JsonTypeInfo #76794
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionThe JSON type info generator triggers an exception when trying to serialize a object with a field that is marked as JsonIgnore but has no callable type constructor available. System.ArgumentException: 'The type 'System.ReadOnlySpan`1[System.Byte]&' may not be used as a type argument.' The reason is that the only constructor of class Secret (see code below) has a ref ROS field. And to date, Reflection.Emit cannot invoke such methods. That is all reasonable, but the serializer was explicitly instructed to ignore that field. The documentation is rather strict on stating that such fields are always ignored if attributed that way. The deserializer should also never set this field, therefore never require to actually create any object and invoke any constructor. My understanding is that a field/property marked as JsonIgnore is never touched or set by the serializer/deserializer. And when an object containing such a field is created, the field is never modified either. Reproduction Stepsusing System.Text.Json;
using System.Text.Json.Serialization;
var Obj = new Container();
var ObjText = JsonSerializer.Serialize(Obj, new JsonSerializerOptions { IncludeFields = true });
class Container
{
public ulong A;
public ulong B;
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public Secret? C;
}
class Secret
{
public ulong Code;
public Secret(in ReadOnlySpan<byte> payload)
{
}
} Expected behaviorno exception Actual behaviorSystem.ArgumentException: 'The type 'System.ReadOnlySpan`1[System.Byte]&' may not be used as a type argument.' Bonus: it would be nice if json could aggregate that exception and rethrow it upstream with the problematic field name added.
Regression?not to my knowledge Known Workaroundscreate a dummy, second parameterless public constructor, that triggers an exception when ever being called Configuration7.0.0-rc.1.22427.1 Other informationNo response
|
Appears to be a duplicate of #76807. |
Description
The JSON type info generator triggers an exception when trying to serialize a object with a field that is marked as JsonIgnore but has no callable type constructor available.
The reason is that the only constructor of class Secret (see code below) has a ref ROS field. And to date, Reflection.Emit cannot invoke such methods. That is all reasonable, but the serializer was explicitly instructed to ignore that field. The documentation is rather strict on stating that such fields are always ignored if attributed that way.
The deserializer should also never set this field, therefore never require to actually create any object and invoke any constructor.
My understanding is that a field/property marked as JsonIgnore is never touched or set by the serializer/deserializer. And when an object containing such a field is created, the field is never modified either.
Reproduction Steps
Expected behavior
no exception
Actual behavior
Bonus: it would be nice if json could aggregate that exception and rethrow it upstream with the problematic field name added.
Regression?
not to my knowledge
Known Workarounds
create a dummy, second parameterless public constructor, that triggers an exception when ever being called
Configuration
7.0.0-rc.1.22427.1
Other information
No response
The text was updated successfully, but these errors were encountered: