Skip to content

Instantly share code, notes, and snippets.

@hesenger
Last active April 4, 2019 15:01
Show Gist options
  • Save hesenger/88e4b4f10b3cabf15147b379c8f193b2 to your computer and use it in GitHub Desktop.
Save hesenger/88e4b4f10b3cabf15147b379c8f193b2 to your computer and use it in GitHub Desktop.
public class OnlyRootContractResolver : CamelCasePropertyNamesContractResolver
{
protected Type _root;
protected List<string> _included;
public OnlyRootContractResolver(Type root, IEnumerable<string> included)
{
_root = root;
_included = new List<string>(included ?? new string[0]);
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var res = base.CreateProperties(type, memberSerialization);
if (type == _root)
return res;
var qry = from t in res
where _included.Contains(t.PropertyName) || Type.GetTypeCode(t.PropertyType) != TypeCode.Object
select t;
return qry.ToList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment