Skip to content

Instantly share code, notes, and snippets.

@zxsanny
Created December 4, 2014 11:07
Show Gist options
  • Save zxsanny/12acf1b23c90c86d2705 to your computer and use it in GitHub Desktop.
Save zxsanny/12acf1b23c90c86d2705 to your computer and use it in GitHub Desktop.
Remove unnecessary properties and so one when you are
var data = JsonConvert.SerializeObject(message, Formatting.None, new JsonSerializerSettings
{
ContractResolver = new ExcludePropertiesContractResolver(new []{"SuccessMessage", "ErrorMessage"})
});
public class ExcludePropertiesContractResolver : DefaultContractResolver
{
private readonly List<string> ExcludedProperties;
public ExcludePropertiesContractResolver(IEnumerable<string> excludedProperties)
{
ExcludedProperties = excludedProperties.Select(x=>x.Trim().ToLower()).ToList();
}
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
return base.CreateProperties(type, memberSerialization)
.Where(x => !ExcludedProperties.Contains(x.PropertyName.Trim().ToLower())).ToList();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment