在.Net Core 3.X版本下默认使用的是System.Text.Json来序列化的
System.Text.Json 命名空间提供高性能、低分配以及符合标准的功能来处理 JavaScript 对象表示法 (JSON),其中包括将对象序列化为 JSON 文本以及将 JSON 文本反序列化为对象(内置 UTF-8 支持)。 它还提供类型以用于读取和写入编码为 UTF-8 的 JSON 文本,以及用于创建内存中文档对象模型 (DOM) 以在数据的结构化视图中随机访问 JSON 元素。
Controllers配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new SystemTextJsonConvert.DateTimeConverter());
options.JsonSerializerOptions.Converters.Add(new SystemTextJsonConvert.DateTimeNullableConverter());
});
}
public class WeatherForecast
{
public DateTime DateTime { get; set; }
//yyyy-MM-dd HH:mm:ss.fff
[JsonConverter(typeof(MillisecondTimeConverter))]
public DateTime DateNow { get; set; }
//yyyy-MM-dd
[JsonConverter(typeof(DateConverter))]
public DateTime Date { get; set; }
//忽略
[System.Text.Json.Serialization.JsonIgnore]
public string Time { get { return Date.ToString("yyyy-MM-dd HH:mm:ss.fff"); } }
//指定序列化和反序列化时 JSON 中存在的属性名称
[System.Text.Json.Serialization.JsonPropertyName(name: "TimeNow")]
public string Now { get { return Date.ToString("yyyy-MM-dd HH:mm:ss.fff"); } }
}
Controller如下:
[HttpGet]
public IEnumerable Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
DateTime = DateTime.Now.AddDays(index),
DateNow = DateTime.Now,
Date = DateTime.Now
})
.ToArray();
}
输出Json:
[
{
"dateTime": "2020-07-25 23:54:05",
"dateNow": "2020-07-24 23:54:05.857",
"date": "2020-07-24",
"TimeNow": "2020-07-24 23:54:05.857"
},
{
"dateTime": "2020-07-26 23:54:05",
"dateNow": "2020-07-24 23:54:05.857",
"date": "2020-07-24",
"TimeNow": "2020-07-24 23:54:05.857"
},
{
"dateTime": "2020-07-27 23:54:05",
"dateNow": "2020-07-24 23:54:05.857",
"date": "2020-07-24",
"TimeNow": "2020-07-24 23:54:05.857"
},
{
"dateTime": "2020-07-28 23:54:05",
"dateNow": "2020-07-24 23:54:05.857",
"date": "2020-07-24",
"TimeNow": "2020-07-24 23:54:05.857"
},
{
"dateTime": "2020-07-29 23:54:05",
"dateNow": "2020-07-24 23:54:05.857",
"date": "2020-07-24",
"TimeNow": "2020-07-24 23:54:05.857"
}
]
可以看到默认没标记的DateTime输出格式为yyyy-MM-dd HH:mm:ss,因为配置了默认的JsonConverter(DateTimeConverter)
DateNow输出格式为yyyy-MM-dd HH:mm:ss.fff 配置了自定义的JsonConverter(MillisecondTimeConverter)
Date输出格式为yyyy-MM-dd 配置了自定义的JsonConverter(DateConverter)
Time设置了[System.Text.Json.Serialization.JsonIgnore] 序列化不输出
Now设置了[System.Text.Json.Serialization.JsonPropertyName(name: "TimeNow")] 序列化输出的属性名为TimeNow
附自定义的JsonConverter代码
public class SystemTextJsonConvert
{
public class DateConverter : JsonConverter
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (DateTime.TryParse(reader.GetString(), out DateTime date))
return date;
}
return reader.GetDateTime();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd"));
}
}
public class DateTimeConverter : JsonConverter
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (DateTime.TryParse(reader.GetString(), out DateTime date))
return date;
}
return reader.GetDateTime();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
public class MillisecondTimeConverter : JsonConverter
{
public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (DateTime.TryParse(reader.GetString(), out DateTime date))
return date;
}
return reader.GetDateTime();
}
public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToString("yyyy-MM-dd HH:mm:ss.fff"));
}
}
public class DateTimeNullableConverter : JsonConverter
{
public override DateTime? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.String)
{
if (DateTime.TryParse(reader.GetString(), out DateTime date)) return date;
return default(DateTime?);
}
return reader.GetDateTime();
}
public override void Write(Utf8JsonWriter writer, DateTime? value, JsonSerializerOptions options)
{
writer.WriteStringValue(value?.ToString("yyyy-MM-dd HH:mm:ss"));
}
}
}
参考文档:
System.Text.Json https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json?view=netcore-3.0
System.Text.Json.Serialization
https://docs.microsoft.com/zh-cn/dotnet/api/system.text.json.serialization?view=netcore-3.0