有时"$type"
,即使由指定,我也需要抑制Json.NET的属性输出JsonPropertyAttribute.ItemTypeNameHandling
。如何才能做到这一点?
我的根类如下所示:
public class DomainResource
{
[JsonProperty(ItemTypeNameHandling = TypeNameHandling.Auto)]
public List<Extension> Extensions { get; set; }
}
此外,我还有一个如下的类层次结构Extension
:
public class Extension
{
readonly string url;
public string Url { get { return url; } }
public Extension(string url)
{
this.url = url;
}
}
public class IntegerExtension : Extension
{
public IntegerExtension(string url) : base(url) { }
[JsonProperty("ValueInteger")]
public int Value { get; set; }
}
我想ItemTypeNameHandling
在序列化过程中的某些情况下忽略,但是我找不到解决方法。当我不想要"$type"
使用以下代码的属性时,我尝试使用TypeNameHandling.None将JsonSerializerSettings设置为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);
我想要的JSON是:
{“扩展名”:[{“ valueInteger”:3,“ url”:“ ewwer”}]}
但是,它包含"$type"
如下所示的属性:
{“ extensions”:[{“ $ type”:“ DicomtoJsonConverter.IntegerExtension,DicomtoJsonConverter”,“ valueInteger”:3,“ url”:“ ewwer”}]}}
如何在不更改类的情况下抑制"$type"
属性的输出?DomainResource
即使由或指定,您也可以使用自定义ContractResolver
禁止显示类型信息。首先,定义以下合同解析器:JsonPropertyAttribute.TypeNameHandling
JsonPropertyAttribute.ItemTypeNameHandling
JsonContainerAttribute.ItemTypeNameHandling
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()
如下:
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版本早于9.0.1,则由于该版本中引入了Json.NET,因此您需要继承CamelCasePropertyNamesContractResolver
而不是继承。DefaultContractResolver
NamingStrategy
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句