使用JsonConverter在C#中自定义JSON反序列化
.Net Core中有两个类
I have two classes in .Net Core
类所有权
namespace CustomStoreDatabase.Models
{
public class Ownership
{
public string OwnershipId { get; set; }
public List<string> TextOutput { get; set; }
public DateTime DateTime { get; set; }
public TimeSpan MeanInterval { get; set; }// Like long ticks, TimeSpan.FromTicks(Int64), TimeSpan.Ticks
}
}
我需要使用方法 TimeSpan.FromTicks(Int64)
和 TimeSpan.Ticks
像长刻度一样显示 MeanInterval
.
I need to show MeanInterval
like long ticks, using the methods TimeSpan.FromTicks(Int64)
and TimeSpan.Ticks
.
我的自定义JsonConverter
My custom JsonConverter
using CustomStoreDatabase.Models;
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace CustomStoreDatabase.Util
{
public class OwnershipJSonConverter : JsonConverter<Ownership>
{
public override bool CanConvert(Type typeToConvert)
{
return true;
}
public override Ownership Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.StartObject)
{
throw new JsonException();
}
//*******************
// HOW TO IMPLEMENT?
//*******************
//throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, Ownership value, JsonSerializerOptions options)
{
writer.WriteStartObject();
if (value != null)
{
writer.WriteString("OwnershipId", value.OwnershipId);
writer.WriteString("TextOutput", JsonSerializer.Serialize(value.TextOutput));
writer.WriteString("DateTime", JsonSerializer.Serialize(value.DateTime));
if (value.MeanInterval != null)
{
writer.WriteNumber("MeanInterval", (long)value.MeanInterval.Ticks);
}
else
{
writer.WriteNull("MeanInterval");
}
}
writer.WriteEndObject();
}
}
}
我不知道如何实现 Read
方法.如何实现覆盖Read方法的自定义反序列化?
I don't know how to implement the Read
method.
How can I implement the custom Deserialization overriding the Read method?
如果可能的话,我向我提议了 CanConvert
方法的另一种实现,非常感谢.
If is possible you guys proposal to me another implementation for CanConvert
method, I thank you very much.
似乎您只想对 TimeSpan
进行自定义序列化,因为它属于 Ownership
,因此为什么不只为 TimeSpan
创建转换器,并避免手动序列化所有其他类属性呢?
It seems like you only want to perform custom serialization of TimeSpan
as it belongs to Ownership
, so why not make a converter for TimeSpan
only and save yourself from manually serializing all of the other class properties?:
public class TimeSpanConverter : JsonConverter<TimeSpan>
{
public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return TimeSpan.FromTicks(reader.GetInt64());
}
public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Ticks);
}
}
然后用 JsonConverterAttribute
装饰您的 MeanInterval
属性:
public class Ownership
{
public string OwnershipId { get; set; }
public List<string> TextOutput { get; set; }
public DateTime DateTime { get; set; }
[JsonConverter(typeof(TimeSpanConverter))]
public TimeSpan MeanInterval { get; set; }// Like long ticks, TimeSpan.FromTicks(Int64), TimeSpan.Ticks
}