Created
December 4, 2014 11:07
-
-
Save zxsanny/12acf1b23c90c86d2705 to your computer and use it in GitHub Desktop.
Remove unnecessary properties and so one when you are
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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