Using reflection, I’m able to filter members based on whether they are inherited, declared, public, private, etc. Is there any way to do the same sort of filtering when serializing an object using JSon.NET?
My code is currently:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
public void addRequestParameters<T>(string key, T SerializableRequestParameters)
{
//Serialize the object
string json = JsonConvert.SerializeObject(SerializableRequestParameters, new JsonSerializerSettings
{
TypeNameHandling = TypeNameHandling.All,
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
//Add it to an existing request (unrelated to this question)
((JObject)JSONRequest).Add(key, JToken.Parse(json));
}
I think you could use a custom
ContractResolverto achieve your goal.Anyway, I found the same question here: Using JSON.net, how do I prevent serializing properties of a derived class, when used in a base class context?