-
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 contract customization: not possible to enable deserialization in properties marked JsonIgnoreCondition.Always #71886
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsConsider the following test: [Fact]
public static void CanEnableDeserializationOnAlwaysIgnoreProperty()
{
var options = new JsonSerializerOptions
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver
{
Modifiers =
{
jsonTypeInfo =>
{
if (jsonTypeInfo.Type != typeof(ClassWithJsonIgnoreAlwaysProperty))
{
return;
}
JsonPropertyInfo jsonPropertyInfo = jsonTypeInfo.Properties[0];
jsonPropertyInfo.Set = (obj, value) => ((ClassWithJsonIgnoreAlwaysProperty)obj).Value = (int)value;
}
}
}
};
ClassWithJsonIgnoreAlwaysProperty value = JsonSerializer.Deserialize<ClassWithJsonIgnoreAlwaysProperty>("""{"Value":42}""", options);
Assert.Equal(42, value.Value);
}
public class ClassWithJsonIgnoreAlwaysProperty
{
[JsonIgnore]
public int Value { get; set; }
} This happens because the I would recommend we change this so that all logic in public partial class JsonPropertyInfo
{
public Func<object, object?, bool> ShouldSerialize { get; set; }
+ public Func<object, object?, bool> ShouldDeserialize { get; set; }
} The two delegates would allow mapping all possible
|
The following internal properties are also of interest:
These all map from |
I think when Set is set we should simply replace value of ShouldSerialize to delegate (I think it's already set) and clear |
Why should making changes to |
it won't. if you request |
Consider the following test:
This happens because the
DetermineSerializationCapabilities
method will mark the property as non-serializable/deserializable based on the initial value ofJsonIgnoreCondition
.JsonIgnoreCondition
is not user-settable so no user modification on the contract model can prevent this.I would recommend we change this so that all logic in
DetermineSerializationCapabilities
is done at initialization time and can be modified by users. Additionally, I propose we introduce aShouldDeserialize
method toJsonPropertyInfo
:public partial class JsonPropertyInfo { public Func<object, object?, bool> ShouldSerialize { get; set; } + public Func<object, object?, bool> ShouldDeserialize { get; set; } }
The two delegates would allow mapping all possible
JsonIgnoreCondition
configurations to these two delegates and not require nulling out theGet
andSet
delegates as we currently do forJsonIgnoreCondition.Always
. Contract customization in Json.NET also includes aShouldDeserialize
delegate complementingShouldSerialize
so we should be following a similar approach inSystem.Text.Json
.cc @krwq @jeffhandley
The text was updated successfully, but these errors were encountered: