In my code I want to use ShouldDeserialize method to make my response more cleaner, but ShouldDeserialize{Property} method is not visible for deserialize method. In below code ShouldSerializeItems predicate works.
public class ItemsContainer
{
public string Id { get; set; }
[JsonProperty]
public IEnumerable<Item> Items{ get; set; }
//Working
public bool ShouldSerializeItems()
{
return !Items.All(x =>
string.IsNullOrEmpty(x.ItemName) && string.IsNullOrEmpty(x.ItemId));
}
// Not working
public bool ShouldDeserializeItems()
{
return !Items.All(x =>
string.IsNullOrEmpty(x.ItemName) && string.IsNullOrEmpty(x.ItemId));
}
}
And call deserialize:
JsonConvert.DeserializeObject<ItemsContainer>(json);
In newtonsoft documentation both serialize and deserialize predicate are documented: