Json.NET的JsonProperty的转换器和MemberConverter性能之间的差异 [英] The difference between Converter and MemberConverter properties of Json.NET's JsonProperty
问题描述
按 DefaultContractResolver.CreateProperty
返回 JsonProperty
类有大约转换器两个属性,转换器
和 MemberConverter
。我没有找到足够的解释他们,有啥它们之间的区别是什么?当使用每一个?什么性质的合同解析器设置? 获取或设置成员转换器从<一个href="http://james.newtonking.com/json/help/index.html?topic=html/P_Newtonsoft_Json_Serialization_JsonProperty_MemberConverter.htm">official文档是没有帮助的。
The JsonProperty
class returned by DefaultContractResolver.CreateProperty
has two properties about converter, Converter
and MemberConverter
. I don't find enough explanation about them, so what's the difference between them? When to use each one? What property to set in the contract resolver? "Gets or sets the member converter" from the official documentation isn't helpful.
推荐答案
这似乎是NS.Json支持重写器; presumably这是为了让每一个人财产没有指定JsonConverter,但他们可能忽略它,如果需要的话。
It would seem that NS.Json supports overridable converters; presumably this is so that each individual property doesn't have to specify a JsonConverter, but they may override it if need be.
提示可以在源代码中找到:
Hints can be found in the source:
从<一个href="https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs"相对=nofollow> DefaultContractResolver.cs 中,线路1254:
private void SetPropertySettingsFromAttributes(JsonProperty property, object attributeProvider, string name, Type declaringType, MemberSerialization memberSerialization, out bool allowNonPublicAccess)
{
...
// resolve converter for property
// the class type might have a converter but the property converter takes presidence
property.Converter = JsonTypeReflector.GetJsonConverter(attributeProvider);
property.MemberConverter = JsonTypeReflector.GetJsonConverter(attributeProvider);
和从<一个href="https://github.com/JamesNK/Newtonsoft.Json/blob/master/Src/Newtonsoft.Json/Serialization/JsonSerializerInternalReader.cs"相对=nofollow> JsonSerializerInternalReader.cs,行348 :
private JsonConverter GetConverter(JsonContract contract, JsonConverter memberConverter, JsonContainerContract containerContract, JsonProperty containerProperty)
{
JsonConverter converter = null;
if (memberConverter != null)
{
// member attribute converter
converter = memberConverter;
}
else if (containerProperty != null && containerProperty.ItemConverter != null)
{
converter = containerProperty.ItemConverter;
}
else if (containerContract != null && containerContract.ItemConverter != null)
{
converter = containerContract.ItemConverter;
}
else if (contract != null)
{
JsonConverter matchingConverter;
if (contract.Converter != null)
// class attribute converter
converter = contract.Converter;
else if ((matchingConverter = Serializer.GetMatchingConverter(contract.UnderlyingType)) != null)
// passed in converters
converter = matchingConverter;
else if (contract.InternalConverter != null)
// internally specified converter
converter = contract.InternalConverter;
}
return converter;
}
这篇关于Json.NET的JsonProperty的转换器和MemberConverter性能之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!