.NET Core 中不调用 OnDeserialized()
OnDeserialized() is not called in .NET Core
我有一个具有三个属性的请求 class,其中两个将从 json 请求正文反序列化,最后一个将在反序列化后基于其他两个属性创建。我正在使用 OnDeserialized()
注释来实现这一点。但是,OnDeserialized
注解好像一点作用都没有。以下是我的代码。请指教!
public class BindUserRequest : IRequest<BindUserResponseDto>
{
[JsonIgnore]
public PartitionKeyType Id { get; set; }
[Required]
public string DeviceType { get; set; }
[Required]
public string DeviceId { get; set; }
[OnSerializing()]
void OnSerializingMethod(StreamingContext context)
{
DeviceType = Id.DeviceType;
DeviceId = Id.DeviceId;
}
[OnDeserialized()]
void OnDeserializedMethod(StreamingContext context)
{
Id = new PartitionKeyType { DeviceId = "123", DeviceType = "123" };
System.Console.WriteLine("****OnDeserialized get called");
}
}
您看到的问题归结为从 ASP.NET Core 2.x 中使用 JSON.NET
作为默认序列化器到在较新版本中使用 System.Text.Json
的变化。 JSON.NET
可以使用这些回调,但 System.Text.Json
它们不是序列化过程的一部分。
一种解决方案是添加适当的 NuGet 包并继续使用 JSON.NET(请参阅 ). Alternatively, Microsoft have produced a migration documentation page 以处理从 JSON.NET 到 System.Text.Json 的更改。
文档解释说,将回调与 System.Text.Json 一起使用需要编写自定义转换器:
In System.Text.Json
, you can simulate callbacks by writing a custom converter. The following example shows a custom converter for a POCO. The converter includes code that displays a message at each point that corresponds to a Newtonsoft.Json
callback.
提供以下示例代码:
public class WeatherForecastCallbacksConverter : JsonConverter<WeatherForecast>
{
public override WeatherForecast Read(
ref Utf8JsonReader reader,
Type type,
JsonSerializerOptions options)
{
// Place "before" code here (OnDeserializing),
// but note that there is no access here to the POCO instance.
Console.WriteLine("OnDeserializing");
// Don't pass in options when recursively calling Deserialize.
WeatherForecast forecast = JsonSerializer.Deserialize<WeatherForecast>(ref reader);
// Place "after" code here (OnDeserialized)
Console.WriteLine("OnDeserialized");
return forecast;
}
public override void Write(
Utf8JsonWriter writer,
WeatherForecast forecast, JsonSerializerOptions options)
{
// Place "before" code here (OnSerializing)
Console.WriteLine("OnSerializing");
// Don't pass in options when recursively calling Serialize.
JsonSerializer.Serialize(writer, forecast);
// Place "after" code here (OnSerialized)
Console.WriteLine("OnSerialized");
}
}
然后您可以在此处添加额外的序列化代码来处理您的用例。请注意,这需要使用 [JsonConverter(type)]
属性在 class 本身上注册,或者通过序列化程序的 Converters 集合进行注册。
我有一个具有三个属性的请求 class,其中两个将从 json 请求正文反序列化,最后一个将在反序列化后基于其他两个属性创建。我正在使用 OnDeserialized()
注释来实现这一点。但是,OnDeserialized
注解好像一点作用都没有。以下是我的代码。请指教!
public class BindUserRequest : IRequest<BindUserResponseDto>
{
[JsonIgnore]
public PartitionKeyType Id { get; set; }
[Required]
public string DeviceType { get; set; }
[Required]
public string DeviceId { get; set; }
[OnSerializing()]
void OnSerializingMethod(StreamingContext context)
{
DeviceType = Id.DeviceType;
DeviceId = Id.DeviceId;
}
[OnDeserialized()]
void OnDeserializedMethod(StreamingContext context)
{
Id = new PartitionKeyType { DeviceId = "123", DeviceType = "123" };
System.Console.WriteLine("****OnDeserialized get called");
}
}
您看到的问题归结为从 ASP.NET Core 2.x 中使用 JSON.NET
作为默认序列化器到在较新版本中使用 System.Text.Json
的变化。 JSON.NET
可以使用这些回调,但 System.Text.Json
它们不是序列化过程的一部分。
一种解决方案是添加适当的 NuGet 包并继续使用 JSON.NET(请参阅
文档解释说,将回调与 System.Text.Json 一起使用需要编写自定义转换器:
In
System.Text.Json
, you can simulate callbacks by writing a custom converter. The following example shows a custom converter for a POCO. The converter includes code that displays a message at each point that corresponds to aNewtonsoft.Json
callback.
提供以下示例代码:
public class WeatherForecastCallbacksConverter : JsonConverter<WeatherForecast>
{
public override WeatherForecast Read(
ref Utf8JsonReader reader,
Type type,
JsonSerializerOptions options)
{
// Place "before" code here (OnDeserializing),
// but note that there is no access here to the POCO instance.
Console.WriteLine("OnDeserializing");
// Don't pass in options when recursively calling Deserialize.
WeatherForecast forecast = JsonSerializer.Deserialize<WeatherForecast>(ref reader);
// Place "after" code here (OnDeserialized)
Console.WriteLine("OnDeserialized");
return forecast;
}
public override void Write(
Utf8JsonWriter writer,
WeatherForecast forecast, JsonSerializerOptions options)
{
// Place "before" code here (OnSerializing)
Console.WriteLine("OnSerializing");
// Don't pass in options when recursively calling Serialize.
JsonSerializer.Serialize(writer, forecast);
// Place "after" code here (OnSerialized)
Console.WriteLine("OnSerialized");
}
}
然后您可以在此处添加额外的序列化代码来处理您的用例。请注意,这需要使用 [JsonConverter(type)]
属性在 class 本身上注册,或者通过序列化程序的 Converters 集合进行注册。