序列化时如何强制单个属性格式化为日期而不是日期时间 [英] How to force a single property to format as a Date and not a Date time when serializing
问题描述
在序列化为 Json 时如何强制将单个属性格式化为 Date 而不是 DateTime?我使用的是 System.Text.Json.Serialization
而不是 newtonsoft json,任何要求我使用 NewtonSoft json 的解决方案都无济于事.
How to force a single property to format as a Date and not a DateTime when serializing to Json? I am using System.Text.Json.Serialization
NOT newtonsoft json, any solutions requiring that I use NewtonSoft jSon will not help.
我有一个对象,它有两个日期时间属性,一个实际上是日期而不是日期时间.
I have an object which has two datetime properties in it, one is actually a date though and not a datetime.
[JsonPropertyName("startDate")]
public DateTime StartDate { get; set; }
[JsonPropertyName("startDate")]
public DateTime ActionDateTime { get; set; }
当我设置它时,我指定它实际上是一个日期.
When I set it I specify that it is in fact a date.
StarDate = DateTime.Now.Date,
但是当我尝试将其序列化为 JSon 时
However when I try to serialize it to JSon
var json = JsonSerializer.Serialize(this);
我明白
{"startDate":"2021-05-20T00:00:00+02:00"}
问题在于,我将它发送到的 API 端点抱怨它是日期时间,它需要序列化为日期.我知道我可以使用 JsonSerializerOptions,但这会序列化所有日期,而我不能这样做.
The problem being that the API endpoint I am sending this to is complaining about it being a datetime, it needs to serialize as a date. I know I could use JsonSerializerOptions but this will serialize all of the dates and I cant have that.
完整的代码示例,让您享受测试的乐趣
full code sample for your testing enjoyment
public class Dummy
{
[JsonPropertyName("startDate")]
public DateTime StartDate { get; set; }
[JsonPropertyName("actiondatetime ")]
public DateTime ActionDateTime { get; set; }
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
var hold = new Dummy();
hold.StartDate = DateTime.Now.Date;
hold.ActionDateTime = DateTime.Now
var json = JsonSerializer.Serialize(hold);
Console.WriteLine(json);
Console.ReadLine();
}
}
我已经搜索了高低,必须有一种方法可以添加一个属性来强制将单个属性格式化为日期.
I have searched high and low there must be a way of adding an attribute to force the single property to format as a date.
推荐答案
它看起来像一个 自定义转换器 使用 JsonConverter
属性应用可以做你想做的事.以下是链接文章中的示例代码:
It looks like a custom converter applied using the JsonConverter
attribute can do what you want. The following is sample code from the linked article:
转换器:
public class DateTimeOffsetJsonConverter : JsonConverter<DateTimeOffset>
{
public override DateTimeOffset Read(
ref Utf8JsonReader reader,
Type typeToConvert,
JsonSerializerOptions options) =>
DateTimeOffset.ParseExact(reader.GetString(),
"MM/dd/yyyy", CultureInfo.InvariantCulture);
public override void Write(
Utf8JsonWriter writer,
DateTimeOffset dateTimeValue,
JsonSerializerOptions options) =>
writer.WriteStringValue(dateTimeValue.ToString(
"MM/dd/yyyy", CultureInfo.InvariantCulture));
}
使用属性应用:
public class WeatherForecastWithConverterAttribute
{
[JsonConverter(typeof(DateTimeOffsetJsonConverter))]
public DateTimeOffset Date { get; set; }
public int TemperatureCelsius { get; set; }
public string Summary { get; set; }
}
这篇关于序列化时如何强制单个属性格式化为日期而不是日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!