C# 如何使用Json.NET中的JsonSerializerSettings在属性中指定时禁用TypeNameHandling?

C# 如何使用Json.NET中的JsonSerializerSettings在属性中指定时禁用TypeNameHandling?,c#,json,json.net,C#,Json,Json.net,有时,我需要禁止Json.NET输出“$type”属性,即使是由指定的。如何做到这一点 我的根类如下所示: public class DomainResource { [JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto)] public List<Extension> Extensions { get; set; } } 在序列化过程中的某些场景中,我想忽略ItemTypeNameHandling,但

有时,我需要禁止Json.NET输出
“$type”
属性,即使是由指定的。如何做到这一点

我的根类如下所示:

public class DomainResource
{
    [JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto)]
    public List<Extension> Extensions { get; set; }
}
在序列化过程中的某些场景中,我想忽略
ItemTypeNameHandling
,但我找不到这样做的方法。 我尝试使用TypeNameHandling设置JsonSerializerSettings。当我不想使用下面的代码使用
“$type”
属性时,无作为jsonconvert的输入:

public static string SerializeObject(object value)
{
    JsonSerializerSettings jsonSettings = new JsonSerializerSettings
    {
        ContractResolver = new CamelCasePropertyNamesContractResolver(),
        NullValueHandling = NullValueHandling.Ignore,
        TypeNameHandling = TypeNameHandling.None,

    };
    jsonSettings.Converters.Add(new StringEnumConverter
    {
        CamelCaseText = true
    });
    return JsonConvert.SerializeObject(value, Formatting.None, jsonSettings);
}
然后按如下方式使用:

var res = new DomainResource();
res.Extensions = new List<Extension>();
res.Extensions.Add(new IntegerExtension("ewwer"){Value = 3});

var x = CustomJsonConvert.SerializeObject(res);
public static class CustomJsonConvert
{
    // You may want to cache the contract resolver for best performance, see
    // https://stackoverflow.com/questions/33557737/does-json-net-cache-types-serialization-information
    static readonly JsonSerializerSettings jsonSettings;
    static CustomJsonConvert()
    {
        jsonSettings = new JsonSerializerSettings
        {
            ContractResolver = new NoTypeNameHandlingContractResolver
            {
                NamingStrategy = new CamelCaseNamingStrategy
                {
                    // These are the settings used by CamelCasePropertyNamesContractResolver by default.
                    // Change them if this is not what you want.
                    OverrideSpecifiedNames = true,
                    ProcessDictionaryKeys = true,
                },
            },
            NullValueHandling = NullValueHandling.Ignore,
            TypeNameHandling = TypeNameHandling.None,
            Converters = { new StringEnumConverter { CamelCaseText = true } },
        };
    }

    public static string SerializeObject(object value)
    {
        return JsonConvert.SerializeObject(value, Formatting.None, jsonSettings);
    }
}
var res=newdomainresource();
res.Extensions=新列表();
Add(新的IntegerExtension(“ewwer”){Value=3});
var x=CustomJsonConvert.SerializeObject(res);
我想要的JSON是:

{“extensions”:[{“valueInteger”:3,“url”:“ewwer”}]}

但它包含
“$type”
属性,如下所示:

{“extensions”:[{“$type”:“DicomtoJsonConverter.IntegerExtension, DicomtoJsonConverter,“valueInteger”:3,“url:“ewwer”}]}

如何在不更改
域资源
类的情况下禁止输出
“$type”
属性

即使由或指定,也可以使用来禁止输出类型信息。首先,定义以下合同解析程序:

public class NoTypeNameHandlingContractResolver : DefaultContractResolver
{
    protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
    {
        var property = base.CreateProperty(member, memberSerialization);
        // Suppress JsonPropertyAttribute.TypeNameHandling
        property.TypeNameHandling = null;
        // Suppress JsonPropertyAttribute.ItemTypeNameHandling
        property.ItemTypeNameHandling = null;
        return property;
    }

    protected override JsonContract CreateContract(Type objectType)
    {
        var contract = base.CreateContract(objectType);
        if (contract is JsonContainerContract)
        {
            // Suppress JsonContainerAttribute.ItemTypeNameHandling
            ((JsonContainerContract)contract).ItemTypeNameHandling = null;
        }
        return contract;
    }
}
然后,修改
CustomJsonConvert.SerializeObject()
,如下所示:

var res = new DomainResource();
res.Extensions = new List<Extension>();
res.Extensions.Add(new IntegerExtension("ewwer"){Value = 3});

var x = CustomJsonConvert.SerializeObject(res);
public static class CustomJsonConvert
{
    // You may want to cache the contract resolver for best performance, see
    // https://stackoverflow.com/questions/33557737/does-json-net-cache-types-serialization-information
    static readonly JsonSerializerSettings jsonSettings;
    static CustomJsonConvert()
    {
        jsonSettings = new JsonSerializerSettings
        {
            ContractResolver = new NoTypeNameHandlingContractResolver
            {
                NamingStrategy = new CamelCaseNamingStrategy
                {
                    // These are the settings used by CamelCasePropertyNamesContractResolver by default.
                    // Change them if this is not what you want.
                    OverrideSpecifiedNames = true,
                    ProcessDictionaryKeys = true,
                },
            },
            NullValueHandling = NullValueHandling.Ignore,
            TypeNameHandling = TypeNameHandling.None,
            Converters = { new StringEnumConverter { CamelCaseText = true } },
        };
    }

    public static string SerializeObject(object value)
    {
        return JsonConvert.SerializeObject(value, Formatting.None, jsonSettings);
    }
}

如果您使用的Json.NET版本早于该版本,则需要对
CamelCasePropertyNamesContractResolver
进行子类化,而不是对该版本中引入的
DefaultContractResolver
进行子类化。

不清楚您到底在问什么?你能分享更多关于你期望从代码中得到什么以及实际发生了什么的细节吗?@ChetanRanpariya编辑的问题