在自定义中
JsonConverter,可以通过
Path从中选择属性来找到要反序列化的JSON属性的名称
JsonReader。
string propertyName = reader.Path.Split('.').Last();但是,这不能解决您的整体问题。假设JSON属性的名称与您的目标类属性匹配,那么您仍然需要一种获取父对象类型的方法,以便从中获取自定义属性。不幸的是,此信息在转换器内部不可用。转换器仅负责它说可以转换的对象类型(在您的情况下为字符串)以及该对象的子属性(在这种情况下,因为字符串是基元,所以不负责)。因此,要使其正常工作,需要编写转换器以在
父级上运行
类,然后需要处理该类的所有字符串属性。由于您的目标似乎是将HTML编码行为应用于所有类中的所有字符串,因此您将需要一个通用转换器来处理所有非原始类型,这可能会变得非常混乱,具体取决于您尝试的内容反序列化。
幸运的是,有更好的方法。除了使用之外
JsonConverter,您还可以
IContractResolver结合使用自定义项和
IValueProvider来解决此问题。A
ContractResolver更适合于您想广泛应用某种行为的此类问题。
下面是您需要的代码示例。的
CustomResolver类扩展
DefaultContractResolver由Json.Net提供。该
CreateProperties()方法检查
JsonProperty由基础解析器创建的对象,并将内部
HtmlEncodingValueProvider类的实例附加到未
[AllowHtml]应用属性的任何字符串属性。每个值提供者随后都通过
SetValue()方法处理其目标字符串属性的实际编码。
public class CustomResolver : DefaultContractResolver{ protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization) { IList<JsonProperty> props = base.CreateProperties(type, memberSerialization); // Find all string properties that do not have an [AllowHtml] attribute applied // and attach an HtmlEncodingValueProvider instance to them foreach (JsonProperty prop in props.Where(p => p.PropertyType == typeof(string))) { PropertyInfo pi = type.GetProperty(prop.UnderlyingName); if (pi != null && pi.GetCustomAttribute(typeof(AllowHtmlAttribute), true) == null) { prop.ValueProvider = new HtmlEncodingValueProvider(pi); } } return props; } protected class HtmlEncodingValueProvider : IValueProvider { PropertyInfo targetProperty; public HtmlEncodingValueProvider(PropertyInfo targetProperty) { this.targetProperty = targetProperty; } // SetValue gets called by Json.Net during deserialization. // The value parameter has the original value read from the JSON; // target is the object on which to set the value. public void SetValue(object target, object value) { var enpred = System.Web.Security.AntiXss.AntiXssEnprer.HtmlEnpre((string)value, useNamedEntities: true); targetProperty.SetValue(target, enpred); } // GetValue is called by Json.Net during serialization. // The target parameter has the object from which to read the string; // the return value is the string that gets written to the JSON public object GetValue(object target) { // if you need special handling for serialization, add it here return targetProperty.GetValue(target); } }}要使用解析器,请创建一个新
JsonSerializerSettings实例,然后将其
ContractResolver属性设置为自定义解析器的新实例,并将设置传递给该
JsonConvert.DeserializeObject()方法。
这是一个简短的演示:
class Program{ static void Main(string[] args) { string json = @" { ""Name"" : ""<b>Foo Bar</b>"", ""Description"" : ""<p>Bada Boom Bada Bing</p>"", }"; JsonSerializerSettings settings = new JsonSerializerSettings { ContractResolver = new CustomResolver() }; Foo foo = JsonConvert.DeserializeObject<Foo>(json, settings); Console.WriteLine("Name: " + foo.Name); Console.WriteLine("Desc: " + foo.Description); }}class Foo{ public string Name { get; set; } [AllowHtml] public string Description { get; set; }}class AllowHtmlAttribute : Attribute { }这是输出。请注意,该
Name属性获取HTML编码,而该
Description属性未获取HTML编码。
Name: <b>Foo Bar</b>Desc: <p>Bada Boom Bada Bing</p>
小提琴:https :
//dotnetfiddle.net/cAg4NC



