【发布时间】:2021-08-25 13:36:18
【问题描述】:
我编写了一个名为 CompententsConverter 的自定义 JsonConverter,它工作正常,但是我很好奇是否有一种方法可以使用备用构造函数,该构造函数接受 params object[] converterParameters 并从相应的属性。
同样,我不确定如何实际检索 JsonConverter 类定义中的参数,或者是否可以使用 JsonConverter 属性来执行此操作。
在模型内部,带有理论参数的属性,其中some_parameter_here 是constant expression 的占位符:
[JsonProperty("components")]
[JsonConverter(typeof(ComponentsConverter), some_parameter_here)]
public List<ComponentModel> Components { get; set; }
ComponentsConverter自定义JsonConverter:
public class ComponentsConverter : JsonConverter
{
public override bool CanConvert (Type t) => t == typeof(List<ComponentModel>);
public override object ReadJson (JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// ... any way to access params object[] customParameters here?
}
public override void WriteJson (JsonWriter writer, object value, JsonSerializer serializer)
{
// ...
}
}
如果能够使用这些额外的参数为特定模型属性定义一些自定义转换行为,那就太好了。
【问题讨论】:
-
[JsonConverter(typeof(ComponentsConverter), "custom_string_value", someCustomObject)]无法编译,因为属性参数must be a constant expression。那么您的问题是如何在JsonConverterAttribute.ConverterParameters中传递非常量值? 还是您的问题是如何从转换器内部访问JsonConverterAttribute.ConverterParameters?跨度> -
@dbc 我可以接受它们是常量,这些只是占位符示例(已更新为更清晰),用于显示我想要传递东西的位置,是的,我不确定如何访问它们从转换器内部
-
好吧,Json.NET 只是调用
Activator.CreateInstance(ConverterType, ConverterParameters),因此转换器参数被传递到转换器的构造函数中。您可以在那里记住它们并在 ReadJson() 和 WriteJson() 中使用它们。参见例如Customising Json.NET serialisation based on compile time type 和 dotnetfiddle.net/CjRRtn -
@dbc 太棒了,如果您将此作为答案发布,我会奖励给您,最好也将您的代码粘贴到答案中
标签: .net-core json.net model-binding custom-attributes jsonconvert