System.Text.Json 中 Newtonsoft 的 WriteStartConstructor 的等效函数
Equivalent function of Newtonsoft's WriteStartConstructor in System.Text.Json
我们使用Newtonsoft的WriteStartConstructor构造了日期对象。但是我们不能对 System.Text.Json.
做同样的事情
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text.Json;
var myObj = new { DOB = DateTime.Now.AddYears(-20) };
Console.WriteLine("Newtonsoft (WriteStartConstructor) ->\t\t" + JsonConvert.SerializeObject(myObj, new NewtonsoftDateTimeConverter()));
var options = new JsonSerializerOptions();
options.Converters.Add(new SystemTextJsonDateTimeConverter());
Console.WriteLine("System.Text.Json (WriteStartObject) ->\t\t" + System.Text.Json.JsonSerializer.Serialize(myObj, options));
Console.ReadKey();
public class NewtonsoftDateTimeConverter : DateTimeConverterBase
{
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
DateTime val = (DateTime)value;
writer.WriteStartConstructor("Date");
writer.WriteValue(val.Year);
writer.WriteValue(val.Month - 1);
writer.WriteValue(val.Day);
writer.WriteValue(val.Hour);
writer.WriteValue(val.Minute);
writer.WriteValue(val.Second);
writer.WriteValue(0);
writer.WriteEndConstructor();
}
}
public class SystemTextJsonDateTimeConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStartObject("Date");
writer.WriteNumberValue(value.Year);
writer.WriteNumberValue(value.Month - 1);
writer.WriteNumberValue(value.Day);
writer.WriteNumberValue(value.Hour);
writer.WriteNumberValue(value.Minute);
writer.WriteNumberValue(value.Second);
writer.WriteNumberValue(0);
writer.WriteEndObject();
}
}
您可以使用 WriteRawValue
并将 skipInputValidation
参数设置为 true
。例如:
public class SystemTextJsonDateTimeConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteRawValue($"new Date({value.Year},{value.Month - 1},{value.Day},{value.Hour},{value.Minute},{value.Second},0)", skipInputValidation: true);
}
}
我用过它,你的提琴手的输出是:
Newtonsoft -> {"DOB":new Date(2001,10,27,5,57,10,0)}
System.Text.Json -> {"DOB":new Date(2001,10,27,5,57,10,0)}
我们使用Newtonsoft的WriteStartConstructor构造了日期对象。但是我们不能对 System.Text.Json.
做同样的事情using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.Text.Json;
var myObj = new { DOB = DateTime.Now.AddYears(-20) };
Console.WriteLine("Newtonsoft (WriteStartConstructor) ->\t\t" + JsonConvert.SerializeObject(myObj, new NewtonsoftDateTimeConverter()));
var options = new JsonSerializerOptions();
options.Converters.Add(new SystemTextJsonDateTimeConverter());
Console.WriteLine("System.Text.Json (WriteStartObject) ->\t\t" + System.Text.Json.JsonSerializer.Serialize(myObj, options));
Console.ReadKey();
public class NewtonsoftDateTimeConverter : DateTimeConverterBase
{
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
throw new NotImplementedException();
}
public override void WriteJson(JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
DateTime val = (DateTime)value;
writer.WriteStartConstructor("Date");
writer.WriteValue(val.Year);
writer.WriteValue(val.Month - 1);
writer.WriteValue(val.Day);
writer.WriteValue(val.Hour);
writer.WriteValue(val.Minute);
writer.WriteValue(val.Second);
writer.WriteValue(0);
writer.WriteEndConstructor();
}
}
public class SystemTextJsonDateTimeConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStartObject("Date");
writer.WriteNumberValue(value.Year);
writer.WriteNumberValue(value.Month - 1);
writer.WriteNumberValue(value.Day);
writer.WriteNumberValue(value.Hour);
writer.WriteNumberValue(value.Minute);
writer.WriteNumberValue(value.Second);
writer.WriteNumberValue(0);
writer.WriteEndObject();
}
}
您可以使用 WriteRawValue
并将 skipInputValidation
参数设置为 true
。例如:
public class SystemTextJsonDateTimeConverter : System.Text.Json.Serialization.JsonConverter<DateTime>
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
throw new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteRawValue($"new Date({value.Year},{value.Month - 1},{value.Day},{value.Hour},{value.Minute},{value.Second},0)", skipInputValidation: true);
}
}
我用过它,你的提琴手的输出是:
Newtonsoft -> {"DOB":new Date(2001,10,27,5,57,10,0)}
System.Text.Json -> {"DOB":new Date(2001,10,27,5,57,10,0)}