C# JsonIgnore to basic class
Solution 1:
You could define a custom contract resolver to ignore the properties. For example,
public class ShouldSerializeContractResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
var property = base.CreateProperty(member, memberSerialization);
if (property.DeclaringType == typeof(ViewModelBase) && property.PropertyName == "IsInDesignMode")
{
property.ShouldSerialize = x=> false;
}
return property;
}
}
Now you could serialize your data by specifying the contract resolver.
var result = JsonConvert.SerializeObject(
data,
Formatting.Indented,
new JsonSerializerSettings { ContractResolver = new ShouldSerializeContractResolver() }
);
Solution 2:
By decorating your derived class (SettingsExpires
) with the following attribute:
[JsonObject(MemberSerialization.OptIn)]
you are basically instructing the serializer to include only those properties which are explicitly annotated with JsonProperty
. Everything else will be ignored.
Reference