当使用XSD.EXE从XML Schema生成类时,它生成给定对象的任何原语的xxxSpecified成员:
<xs:complexType name ="Foo">
<xs:all>
<xs:element name ="Count" type = "xs:integer"/>
</xs:all>
</xs:complexType>
…生成:
public class Foo
{
public int Count { get; set; }
public bool CountSpecified { get; set; }
}
显示JSON的最新版本。. NET可以在反序列化时自动设置这些属性。
string request = "{ Count : 10 }";
var object = JsonConvert.Deserialize<Foo>(request)
Assert.IsTrue(object.Count = 10); // Yup
Assert.IsTrue(object.CountSpecified == true); //Also yup - JSON.NET works!
然而,当采用另一种方式时,xxxSpecified属性包含在JSON输出中,这是不正确的,因为它不是模式的一部分。
string request = JsonConvert.Serialize(object);
//{
// Count: 10,
// CountSpecified : true <-- This is incorrect - should not be output
//}
是否缺少控制是否输出xxxSpecified属性的设置?我怎样才能抑制它呢?
(注意:这是这里回答的问题的排列:
JSON。. NET、XmlSerializer和"指定的"地产
…但它涉及到创建扩展类,这对我来说是不可能的,因为模式中有数百个类,我不能改变继承层次结构。所以这个答案行不通。
您可以创建自己的契约解析器来过滤掉xxxSpecified
属性:
public class SkipSpecifiedContractResolver : DefaultContractResolver
{
// As of 7.0.1, Json.NET suggests using a static instance for "stateless" contract resolvers, for performance reasons.
// http://www.newtonsoft.com/json/help/html/ContractResolver.htm
// http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Serialization_DefaultContractResolver__ctor_1.htm
// "Use the parameterless constructor and cache instances of the contract resolver within your application for optimal performance."
static SkipSpecifiedContractResolver instance;
static SkipSpecifiedContractResolver() { instance = new SkipSpecifiedContractResolver(); }
public static SkipSpecifiedContractResolver Instance { get { return instance; } }
protected override IList<JsonProperty> CreateProperties(Type type, MemberSerialization memberSerialization)
{
var properties = base.CreateProperties(type, memberSerialization);
ILookup<string, JsonProperty> lookup = null;
foreach (var property in properties)
{
if (property.GetIsSpecified != null && property.SetIsSpecified != null)
{
var name = property.UnderlyingName + "Specified";
lookup = lookup ?? properties.ToLookup(p => p.UnderlyingName);
var specified = lookup[name]
// Possibly also check for [XmlIgnore] being applied. While not shown in the question, xsd.exe will always
// apply [XmlIgnore] to xxxSpecified tracking properties.
//.Where(p => p.AttributeProvider.GetAttributes(typeof(System.Xml.Serialization.XmlIgnoreAttribute),true).Any())
.SingleOrDefault();
if (specified != null)
specified.Ignored = true;
}
}
return properties;
}
}
然后像这样使用:
var settings = new JsonSerializerSettings { ContractResolver = SkipSpecifiedContractResolver.Instance };
var object = JsonConvert.DeserializeObject<Foo>(request, settings);
如果您希望始终这样做,您可以在全局JsonConvert.DefaultSettings
中设置契约解析器:
JsonConvert.DefaultSettings = (() =>
{
return new JsonSerializerSettings { ContractResolver = SkipSpecifiedContractResolver.Instance };
});