Skip to content
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

MissingMemberHandling.Error doesn't throw when property is set with jsonignore #2931

Open
vsudhini opened this issue Jan 29, 2024 · 1 comment

Comments

@vsudhini
Copy link

vsudhini commented Jan 29, 2024

public class Account
{
public string FullName { get; set; }
public bool Deleted { get; set; }
[JsonIgnore]
public bool DeletedDate{ get; set; }
}

string json = @"{
'FullName': 'Dan Deleted',
'Deleted': true,
'DeletedDate': '2013-01-20T00:00:00'
}";

try
{
JsonConvert.DeserializeObject(json, new JsonSerializerSettings
{
MissingMemberHandling = MissingMemberHandling.Error
});
}
catch (JsonSerializationException ex) //No Error returned
{

Console.WriteLine(ex.Message);

}

after setting MissingMemberHandling.Error does not seem to throw serialization exception for a property set with jsonignore
how to handle this?

Source/destination types

// Put the types you are serializing or deserializing here

Source/destination JSON

{"message":"Place your serialized or deserialized JSON here"}

Expected behavior

Actual behavior

Steps to reproduce

// Your calls to Newtonsoft.Json here
@elgonzo
Copy link

elgonzo commented Jan 30, 2024

That there is no exception being thrown for ignored members is logical and expected behavior and no bug. Because the member is not missing, it is just ignored.

One relatively easy way to make an ignored member go "missing" is to create your own contract resolver derived from DefaultContractResolver, which removes ignored properties from the list of properties for an object contract. Something like this, for example:

class IgnoredMembersGoMissingContractResolver : DefaultContractResolver
{
  protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
    => base.CreateProperties(type, memberSerialization)
           .Where(p => !p.Ignored)
           .ToList();
}

I haven't tested this code myself (just written down from the top of my head, and therefore might contain typos or other errors), but it can serve as an illustration of the approach.

Note that the JsonProperty.Ignored value tested here is not only influenced by the JsonIgnoreAttribute, but also by other attributes.

Especially noteworthy is that JsonProperty.Ignored is also set by the JsonExtensionDataAttribute. If you are using this attribute, you will have to test thoroughly whether "blindly" removing the extension data property from the list of serializable properties (as my code example does) has any averse side effects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants