Say I have two classes: A and B
public class A
{
public string Name { get; set; }
public List<int> IntList { get; set; }
public List<B> BList { get; set; }
}
public class B
{
public string Id { get; set; }
}
And I want to shallowly* serialize them using a Json.Net ContractResolver. *By shallow I mean, only value types and lists of value types so the json of a serialized class A would contain both Name and IntList but not BList.
So here Is what I have so far (adapted from this question):
public class ShallowResolver : DefaultContractResolver
{
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
{
JsonProperty prop = base.CreateProperty(member, memberSerialization);
if (prop.PropertyType.IsClass)
{
prop.ShouldSerialize = obj => false;
}
return prop;
}
}
This resolver handles the case of references, however it does nothing to address my list requirement. I was unable to find something like prop.PropertyType.IsList
and prop.PropertyType.IsArray
didn't seem to do the trick either.
Is there a way to, inside of my ShallowResolver, identify if a property is a list? and if so, is there a way to identify if the list is of primitive types?
Aucun commentaire:
Enregistrer un commentaire