正如@dbc 所说,您可以覆盖CanRead
并CanWrite
返回false
并注册CustomAsDefaultConvertor : JsonConverter
您的财产。
我的情况:
public class AsDefaultConverter : JsonConverter<JObject>
{
public override JObject ReadJson(JsonReader reader, Type objectType, JObject existingValue, bool hasExistingValue, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, JObject value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override bool CanWrite => false;
public override bool CanRead => false;
}
我的模型:
public class MyModel
{
/// <summary>
/// Gets or sets event id
/// </summary>
public string EventId { get; set; }
/// <summary>
/// Gets or sets trigger name
/// </summary>
public string Name { get; set; }
/// <summary>
/// Gets or sets arguments
/// </summary>
[JsonConverter(typeof(AsDefaultConverter))]
public JObject Arguments { get; set; }
/// <summary>
/// Gets or sets trigger creation date
/// </summary>
public DateTimeOffset Created { get; set; }
/// <summary>
/// Gets or sets trigger creator
/// </summary>
public User CreatedBy { get; set; }
}
在启动注册中,我已经覆盖了自定义的默认 JsonSerialization,但是对于属性,JObject Arguments
我已经指定了在这种情况下需要的转换器。