Skip to content

System.Text.Json contract customization: not possible to enable deserialization in properties marked JsonIgnoreCondition.Always #71886

Closed

Description

@eiriktsarpalis

Consider 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 DetermineSerializationCapabilities method will mark the property as non-serializable/deserializable based on the initial value of JsonIgnoreCondition. 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 a ShouldDeserialize method to JsonPropertyInfo:

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 the Get and Set delegates as we currently do for JsonIgnoreCondition.Always. Contract customization in Json.NET also includes a ShouldDeserialize delegate complementing ShouldSerialize so we should be following a similar approach in System.Text.Json.

cc @krwq @jeffhandley

Activity

  1. added this to the 7.0.0 milestone on Jul 9, 2022
  2. eiriktsarpalis commented on Jul 9, 2022

    @eiriktsarpalis
    MemberAuthor

    The following internal properties are also of interest:

    • IgnoreDefaultValuesOnWrite
    • IgnoreDefaultValuesOnRead
    • CanSerialize
    • CanDeserialize

    These all map from JsonIgnoreCondition but are not user modifiable. Their behavior should be mapped to ShouldSerialize/ShouldDeserialize delegates.

  3. ghost added
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 10, 2022
  4. krwq commented on Jul 11, 2022

    @krwq
    Member

    I think when Set is set we should simply replace value of ShouldSerialize to delegate (I think it's already set) and clear _ignoreCondition and that should already be enough for that to work correctly.

  5. eiriktsarpalis commented on Jul 11, 2022

    @eiriktsarpalis
    MemberAuthor

    I think when Set is set we should simply replace value of ShouldSerialize to delegate

    Why should making changes to Set have any bearing on the value of ShouldSerialize? One concerns serialization configuration and the other concerns deserialization. While they are related by virtue of the fact that JsonIgnoreCondition feeds into their initial population, from a user's perspective they should be independent.

  6. krwq commented on Jul 11, 2022

    @krwq
    Member

    it won't. if you request ShouldSerialize value it will be the same regardless of touching Set - the only thing which will change is perf since we can't use internal enum which allows us to not having to touch delegate

  7. ghost removed
    in-prThere is an active PR which will close this issue when it is merged
    on Jul 11, 2022
  8. ghost locked as resolved and limited conversation to collaborators on Aug 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions