Json.NET JsonProperty的Converter和MemberConverter属性的区别

前端之家收集整理的这篇文章主要介绍了Json.NET JsonProperty的Converter和MemberConverter属性的区别前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
DefaultContractResolver.CreateProperty返回的JsonProperty类有两个转换器,Converter和MemberConverter的属性.我没有找到足够的解释,所以有什么区别呢?何时使用每一个?在合同解析器中设定什么属性?从 official documentation获取或设置成员转换器是没有帮助的.
NS.Json似乎支持可以覆盖的转换器;大概这是为了使每个单独的属性不必指定一个JsonConverter,但如果需要,它们可以覆盖它.

在源中可以找到提示

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);

并从JsonSerializerInternalReader.cs,line 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;
    }
原文链接:https://www.f2er.com/json/288450.html

猜你在找的Json相关文章