C#JsonConvert使用默认转换器而不是自定义转换器 [英] C# JsonConvert using the default converter instead of the custom converter
问题描述
我有一个具有自定义JsonConverter
的类,如下所示:
I have a class as follows that has a custom JsonConverter
:
[JsonConverter(typeof(TheShapeSerializer))]
public class TheShape : IShape {
//....
}
我无法更改课程.自定义序列化程序的工作方式不适合我的需求.
I cannot change the class. The way the custom serializer works is not appropriate for my needs.
是否可以使用默认的序列化程序而不是TheShapeSerializer序列化TheShape的实例?
Is there a way to serialize an instance of TheShape using the default serializer instead of TheShapeSerializer?
沿着同一行,是否有一种方法可以根据给定条件在序列化时间选择多个转换器?
Along the same lines, is there a way to have multiple converters that can be selected at serialization time based on a given condition?
推荐答案
The order in which JsonConverters are selected is documented as follows:
使用JsonConverter的优先级是成员属性,然后是class属性,最后是传递给JsonSerializer的所有转换器.
The priority of which JsonConverter is used is member attribute, then class attribute, and finally any converters passed to the JsonSerializer.
因此,您不能禁用通过 JsonConverterAttribute
应用的JsonConverter
>使用 JsonSerializerSettings.Converters
.相反,您有以下选择.
Thus you cannot disable a JsonConverter
applied via JsonConverterAttribute
using JsonSerializerSettings.Converters
. Instead, you have the following options.
首先,如果您控制的某种类型直接引用了您的TheShape
,则可以从此答案中获取NoConverter
到 选择性地使用默认JSON转换器 ,然后使用JsonConverterAttribute
或 JsonPropertyAttribute.ItemConverterType
,例如如下:
Firstly, if your TheShape
is being referred to directly by some type you control, you could grab NoConverter
from this answer to Selectively use default JSON converter and apply it to the referring members using JsonConverterAttribute
or JsonPropertyAttribute.ItemConverterType
, e.g. as follows:
public class ShapeContainer
{
[JsonConverter(typeof(NoConverter))]
public TheShape Shape { get; set; }
[JsonProperty(ItemConverterType = typeof(NoConverter))]
public List<TheShape> Shapes { get; set; }
}
现在NoConverter
将取代TheShapeSerializer
的应用属性,并导致Json.NET退回默认序列化.
Now NoConverter
will supersede TheShapeSerializer
for the properties where it is applied, and cause Json.NET to fall back on default serialization.
其次,如果无法将成员属性添加到使用TheShape
的类型,则可以创建 DefaultContractResolver.ResolveContractConverter
并为TheShape
返回null
.首先定义以下合同解析器:
Secondly, if you cannot add member attributes to types where TheShape
is used, you could create a custom contract resolver that overrides DefaultContractResolver.ResolveContractConverter
and returns null
for TheShape
. First define the following contract resolver:
public class ConverterDisablingContractResolver : DefaultContractResolver
{
readonly HashSet<Type> types;
public ConverterDisablingContractResolver(IEnumerable<Type> types)
{
if (types == null)
throw new ArgumentNullException();
this.types = new HashSet<Type>(types);
}
bool ContainsType(Type type)
{
return types.Contains(type);
}
protected override JsonConverter ResolveContractConverter(Type objectType)
{
// This could be enhanced to deal with inheritance. I.e. if TBase is in types and has a converter then
// its converter should not be used for TDerived -- but if TDerived has its own converter then it should still be
// used, so simply returning null for TDerived would be wrong.
if (types.Contains(objectType))
return null;
return base.ResolveContractConverter(objectType);
}
}
然后,出于性能原因,在此处:
Then, define a static member somewhere as follows, for performance reasons described here:
static IContractResolver shapeResolver = new ConverterDisablingContractResolver(new[] { typeof(TheShape) });
并序列化如下:
var settings = new JsonSerializerSettings
{
ContractResolver = shapeResolver,
};
var json = JsonConvert.SerializeObject(root, settings);
演示小提琴,同时显示两个选项此处.
Demo fiddle showing both options here.
沿着同一行,是否有一种方法可以根据给定条件在序列化时间选择多个转换器?
显然,您可以根据某些运行时条件向JsonSerializerSettings.Converters
添加不同的转换器.但是,如果您想将静态应用的转换器替换为运行时转换器,则需要适当地设置类型,例如通过使用此答案中的OverridableJsonConverterDecorator
到
Obviously you could add different converters to JsonSerializerSettings.Converters
depending on some runtime condition. But if you want to supersede statically applied converters with runtime converters, you would need to set up your types appropriately, e.g. by using OverridableJsonConverterDecorator
from this answer to Why Json.net does not use customized IsoDateTimeConverter?.
这篇关于C#JsonConvert使用默认转换器而不是自定义转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!