-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Comments
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. |
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
{
}
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
Expected behavior
Actual behavior
Steps to reproduce
// Your calls to Newtonsoft.Json here
The text was updated successfully, but these errors were encountered: