我想为json创建一个动态合同解析器,它将在运行时中排除字段.这个想法是传递给构造器 something ,该构造器将排除CreateProperties覆盖内的某些字段.
I want to create a dynamic contract resolver for json which will exclude fields in runtime. The idea is to pass into constructor something which will exclude certain fields inside CreateProperties override.
到目前为止,我想出了传递PropertyInfo[]的方法,该方法依赖于Json/Class属性名称的相等性,这从长远来看是不好的(即,我想将json属性名称重写为更短的名称).解决方案的另一个问题是我需要通过PropertyInfo[],这在我看来并不直观.
So far i came up with passing PropertyInfo[] which relies on Json / Class properties name equality which is not good in long run ( ie. i want to override json property name to something shorter ). Another issue with solution is that i need to pass PropertyInfo[] which is not intuitive in my opinion.
也许有一种方法可以使用LINQ表达式以更好的方式重写此类.例如,像传递List<Func<T,TOut>>然后通过反射编译和提取参数.它将更加动态,但无法解决Json/Class属性名称相等的问题.
Maybe there is a way to use LINQ expressions to rewrite this class in better way. For example like passing List<Func<T,TOut>> then compiling and extracting parameters via reflection. It will be more dynamic but would not solve the issue with Json / Class property name equality.
任何建议,我都被卡住了....
Any suggestions, i'm stuck....
public class DynamicContractResolver : DefaultContractResolver { private readonly PropertyInfo[] m_propertiesExclusion; public DynamicContractResolver(PropertyInfo[] propertiesExclusion) { m_propertiesExclusion = propertiesExclusion; } protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { IList<JsonProperty> jsonProperties = base.CreateProperties(type, memberSerialization); IEnumerable<string> filteredOutProperties = m_propertiesExclusion.Select(i => i.Name); jsonProperties = jsonProperties .Where(i => !filteredOutProperties.Contains(i.PropertyName)) .ToList(); return jsonProperties; } }推荐答案
在这里,该实现采用任意数量的Expression<Func<T, object>>并排除它们引用的属性.从表达式中提取属性名称的代码来自此答案.. >
Here's an implementation that takes any number of Expression<Func<T, object>>s and excludes the properties they reference. The code for extracting property names from expressions was taken from this answer.
public class DynamicContractResolver<T> : DefaultContractResolver { private readonly HashSet<string> propertiesToExclude; public DynamicContractResolver( params Expression<Func<T, object>>[] propertyExpressions) { this.propertiesToExclude = new HashSet<string>(); foreach (Expression<Func<T, object>> expression in propertyExpressions) { string propertyName = GetPropertyNameFromExpression(expression); this.propertiesToExclude.Add(propertyName); } } protected override IList<JsonProperty> CreateProperties( Type type, MemberSerialization memberSerialization) { IList<JsonProperty> jsonProperties = base.CreateProperties(type, memberSerialization); if (typeof(T).IsAssignableFrom(type)) { jsonProperties = jsonProperties .Where(pr => !this.propertiesToExclude.Contains(pr.PropertyName)) .ToList(); } return jsonProperties; } // stackoverflow/a/2916344/497356 private string GetPropertyNameFromExpression( Expression<Func<T, object>> expression) { MemberExpression body = expression.Body as MemberExpression; if (body == null) { UnaryExpression ubody = (UnaryExpression)expression.Body; body = ubody.Operand as MemberExpression; } return body.Member.Name; } }以下是使用它的示例:
var resolver = new DynamicContractResolver<MyClass>( mc => mc.MyIntegerProperty, mc => mc.MyBoolProperty); var myClass = new MyClass { MyIntegerProperty = 4, MyStringProperty = "HELLO", MyBoolProperty = true }; var settings = new JsonSerializerSettings { ContractResolver = resolver, Formatting = Newtonsoft.Json.Formatting.Indented }; string serialized = JsonConvert.SerializeObject( myClass, settings); Console.WriteLine(serialized);更多推荐
通过反射进行动态json序列化过滤
发布评论