在.Net Core 3.X版本下默认使用的是System.Text.Json来序列化的
System.Text.Json 命名空间提供高性能、低分配以及符合标准的功能来处理 JavaScript 对象表示法 (JSON),其中包括将对象序列化为 JSON 文本以及将 JSON 文本反序列化为对象(内置 UTF-8 支持)。 它还提供类型以用于读取和写入编码为 UTF-8 的 JSON 文本,以及用于创建内存中文档对象模型 (DOM) 以在数据的结构化视图中随机访问 JSON 元素。
Controllers配置:
1 2 3 4 5 6 7 8 |
public void ConfigureServices(IServiceCollection services) { services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.Converters.Add(new SystemTextJsonConvert.DateTimeConverter()); options.JsonSerializerOptions.Converters.Add(new SystemTextJsonConvert.DateTimeNullableConverter()); }); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
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如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
[HttpGet] public IEnumerable<WeatherForecast> 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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
[ { "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代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
public class SystemTextJsonConvert { public class DateConverter : JsonConverter<DateTime> { 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<DateTime> { 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<DateTime> { 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<DateTime?> { 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
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/qq_27559331/article/details/107571781