From 2de8d00f8da3eead65dd4baf3669adf923d098cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E7=BA=A2=E9=B9=8F?= Date: Thu, 10 Nov 2022 16:26:01 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0DateOnly=E3=80=81DateOnly?= =?UTF-8?q?=3F=E3=80=81TimeOnly=E3=80=81TimeOnly=3F=E7=B1=BB=E5=9E=8BJson?= =?UTF-8?q?=E8=BD=AC=E6=8D=A2=E6=8B=93=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NewtonsoftJsonDateOnlyJsonConverter.cs | 88 ++++++++++++ .../NewtonsoftJsonTimeOnlyJsonConverter.cs | 88 ++++++++++++ .../SystemTextJsonDateOnlyJsonConverter.cs | 134 ++++++++++++++++++ .../SystemTextJsonTimeOnlyJsonConverter.cs | 134 ++++++++++++++++++ .../Extensions/NewtonsoftJsonExtensions.cs | 27 ++++ .../Extensions/SystemTextJsonExtensions.cs | 28 ++++ 6 files changed, 499 insertions(+) create mode 100644 framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonDateOnlyJsonConverter.cs create mode 100644 framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonTimeOnlyJsonConverter.cs create mode 100644 framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonDateOnlyJsonConverter.cs create mode 100644 framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonTimeOnlyJsonConverter.cs diff --git a/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonDateOnlyJsonConverter.cs b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonDateOnlyJsonConverter.cs new file mode 100644 index 0000000000..c280a14e17 --- /dev/null +++ b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonDateOnlyJsonConverter.cs @@ -0,0 +1,88 @@ +// MIT License +// +// Copyright (c) 2020-2022 百小僧, Baiqian Co.,Ltd and Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#if !NET5_0 +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace Furion.JsonSerialization; +/// +/// DateOnly 类型序列化 +/// +public class NewtonsoftJsonDateOnlyJsonConverter : JsonConverter +{ + /// + /// 反序列化 + /// + /// + /// + /// + /// + /// + /// + public override DateOnly ReadJson(JsonReader reader, Type objectType, DateOnly existingValue, bool hasExistingValue, JsonSerializer serializer) + { + var value = JValue.ReadFrom(reader).Value(); + return DateOnly.Parse(value); + } + /// + /// 序列化 + /// + /// + /// + /// + public override void WriteJson(JsonWriter writer, DateOnly value, JsonSerializer serializer) + { + serializer.Serialize(writer, value.ToString("yyyy-MM-dd")); + } +} +/// +/// DateOnly? 类型序列化 +/// +public class NewtonsoftJsonNullableDateOnlyJsonConverter : JsonConverter +{ + /// + /// 反序列化 + /// + /// + /// + /// + /// + /// + /// + public override DateOnly? ReadJson(JsonReader reader, Type objectType, DateOnly? existingValue, bool hasExistingValue, JsonSerializer serializer) + { + var value = JValue.ReadFrom(reader).Value(); + return !string.IsNullOrWhiteSpace(value) ? DateOnly.Parse(value) : null; + } + /// + /// 序列化 + /// + /// + /// + /// + public override void WriteJson(JsonWriter writer, DateOnly? value, JsonSerializer serializer) + { + serializer.Serialize(writer, value.HasValue ? value.Value.ToString("yyyy-MM-dd") : ""); + } +} +#endif \ No newline at end of file diff --git a/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonTimeOnlyJsonConverter.cs b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonTimeOnlyJsonConverter.cs new file mode 100644 index 0000000000..d2dbe03ef0 --- /dev/null +++ b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonTimeOnlyJsonConverter.cs @@ -0,0 +1,88 @@ +// MIT License +// +// Copyright (c) 2020-2022 百小僧, Baiqian Co.,Ltd and Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#if !NET5_0 +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace Furion.JsonSerialization; +/// +/// TimeOnly 类型序列化 +/// +public class NewtonsoftJsonTimeOnlyJsonConverter : JsonConverter +{ + /// + /// 反序列化 + /// + /// + /// + /// + /// + /// + /// + public override TimeOnly ReadJson(JsonReader reader, Type objectType, TimeOnly existingValue, bool hasExistingValue, JsonSerializer serializer) + { + var value = JValue.ReadFrom(reader).Value(); + return TimeOnly.Parse(value); + } + /// + /// 序列化 + /// + /// + /// + /// + public override void WriteJson(JsonWriter writer, TimeOnly value, JsonSerializer serializer) + { + serializer.Serialize(writer, value.ToString("HH:mm:ss")); + } +} +/// +/// TimeOnly? 类型序列化 +/// +public class NewtonsoftJsonNullableTimeOnlyJsonConverter : JsonConverter +{ + /// + /// 反序列化 + /// + /// + /// + /// + /// + /// + /// + public override TimeOnly? ReadJson(JsonReader reader, Type objectType, TimeOnly? existingValue, bool hasExistingValue, JsonSerializer serializer) + { + var value = JValue.ReadFrom(reader).Value(); + return !string.IsNullOrWhiteSpace(value) ? TimeOnly.Parse(value) : null; + } + /// + /// 序列化 + /// + /// + /// + /// + public override void WriteJson(JsonWriter writer, TimeOnly? value, JsonSerializer serializer) + { + serializer.Serialize(writer, value.HasValue ? value.Value.ToString("HH:mm:ss") : ""); + } +} +#endif \ No newline at end of file diff --git a/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonDateOnlyJsonConverter.cs b/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonDateOnlyJsonConverter.cs new file mode 100644 index 0000000000..bb6b5a6331 --- /dev/null +++ b/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonDateOnlyJsonConverter.cs @@ -0,0 +1,134 @@ +// MIT License +// +// Copyright (c) 2020-2022 百小僧, Baiqian Co.,Ltd and Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#if !NET5_0 +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Furion.JsonSerialization; + + +/// +/// DateOnly 类型序列化 +/// +[SuppressSniffer] +public class SystemTextJsonDateOnlyJsonConverter : JsonConverter +{ + /// + /// 默认构造函数 + /// + public SystemTextJsonDateOnlyJsonConverter() + { + Format ??= "yyyy-MM-dd"; + } + + /// + /// 构造函数 + /// + /// + public SystemTextJsonDateOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 时间格式化格式 + /// + public string Format { get; private set; } + + /// + /// 反序列化 + /// + /// + /// + /// + /// + public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return DateOnly.Parse(reader.GetString()); + } + + /// + /// 序列化 + /// + /// + /// + /// + public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString(Format)); + } +} + +/// +/// DateOnly? 类型序列化 +/// +[SuppressSniffer] +public class SystemTextJsonNullableDateOnlyJsonConverter : JsonConverter +{ + /// + /// 默认构造函数 + /// + public SystemTextJsonNullableDateOnlyJsonConverter() + { + Format ??= "yyyy-MM-dd"; + } + + /// + /// 构造函数 + /// + /// + public SystemTextJsonNullableDateOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 时间格式化格式 + /// + public string Format { get; private set; } + + /// + /// 反序列化 + /// + /// + /// + /// + /// + public override DateOnly? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return DateOnly.TryParse(reader.GetString(), out DateOnly date) ? date : null; + } + + /// + /// 序列化 + /// + /// + /// + /// + public override void Write(Utf8JsonWriter writer, DateOnly? value, JsonSerializerOptions options) + { + if (value == null) writer.WriteNullValue(); + else writer.WriteStringValue(value.Value.ToString(Format)); + } +} +#endif \ No newline at end of file diff --git a/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonTimeOnlyJsonConverter.cs b/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonTimeOnlyJsonConverter.cs new file mode 100644 index 0000000000..7e686c5a2f --- /dev/null +++ b/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonTimeOnlyJsonConverter.cs @@ -0,0 +1,134 @@ +// MIT License +// +// Copyright (c) 2020-2022 百小僧, Baiqian Co.,Ltd and Contributors +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#if !NET5_0 +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace Furion.JsonSerialization; + + +/// +/// TimeOnly 类型序列化 +/// +[SuppressSniffer] +public class SystemTextJsonTimeOnlyJsonConverter : JsonConverter +{ + /// + /// 默认构造函数 + /// + public SystemTextJsonTimeOnlyJsonConverter() + { + Format ??= "HH:mm:ss"; + } + + /// + /// 构造函数 + /// + /// + public SystemTextJsonTimeOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 时间格式化格式 + /// + public string Format { get; private set; } + + /// + /// 反序列化 + /// + /// + /// + /// + /// + public override TimeOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return TimeOnly.Parse(reader.GetString()); + } + + /// + /// 序列化 + /// + /// + /// + /// + public override void Write(Utf8JsonWriter writer, TimeOnly value, JsonSerializerOptions options) + { + writer.WriteStringValue(value.ToString(Format)); + } +} + +/// +/// TimeOnly? 类型序列化 +/// +[SuppressSniffer] +public class SystemTextJsonNullableTimeOnlyJsonConverter : JsonConverter +{ + /// + /// 默认构造函数 + /// + public SystemTextJsonNullableTimeOnlyJsonConverter() + { + Format ??= "HH:mm:ss"; + } + + /// + /// 构造函数 + /// + /// + public SystemTextJsonNullableTimeOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 时间格式化格式 + /// + public string Format { get; private set; } + + /// + /// 反序列化 + /// + /// + /// + /// + /// + public override TimeOnly? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + return TimeOnly.TryParse(reader.GetString(), out TimeOnly time) ? time : null; + } + + /// + /// 序列化 + /// + /// + /// + /// + public override void Write(Utf8JsonWriter writer, TimeOnly? value, JsonSerializerOptions options) + { + if (value == null) writer.WriteNullValue(); + else writer.WriteStringValue(value.Value.ToString(Format)); + } +} +#endif diff --git a/framework/Furion.Pure/JsonSerialization/Extensions/NewtonsoftJsonExtensions.cs b/framework/Furion.Pure/JsonSerialization/Extensions/NewtonsoftJsonExtensions.cs index bb0b89f429..c95410ec82 100644 --- a/framework/Furion.Pure/JsonSerialization/Extensions/NewtonsoftJsonExtensions.cs +++ b/framework/Furion.Pure/JsonSerialization/Extensions/NewtonsoftJsonExtensions.cs @@ -41,4 +41,31 @@ public static class NewtonsoftJsonExtensions return converters; } + +#if !NET5_0 + /// + /// 添加 DateOnly/DateOnly? 类型序列化处理 + /// + /// + /// + public static IList AddDateOnlyConverters(this IList converters) + { + converters.Add(new NewtonsoftJsonDateOnlyJsonConverter()); + converters.Add(new NewtonsoftJsonNullableDateOnlyJsonConverter()); + + return converters; + } + /// + /// 添加 TimeOnly/TimeOnly? 类型序列化处理 + /// + /// + /// + public static IList AddTimeOnlyConverters(this IList converters) + { + converters.Add(new NewtonsoftJsonTimeOnlyJsonConverter()); + converters.Add(new NewtonsoftJsonNullableTimeOnlyJsonConverter()); + + return converters; + } +#endif } \ No newline at end of file diff --git a/framework/Furion.Pure/JsonSerialization/Extensions/SystemTextJsonExtensions.cs b/framework/Furion.Pure/JsonSerialization/Extensions/SystemTextJsonExtensions.cs index 77cd7fa60f..763bb7abbf 100644 --- a/framework/Furion.Pure/JsonSerialization/Extensions/SystemTextJsonExtensions.cs +++ b/framework/Furion.Pure/JsonSerialization/Extensions/SystemTextJsonExtensions.cs @@ -60,4 +60,32 @@ public static class SystemTextJsonExtensions return converters; } + + +#if !NET5_0 + /// + /// 添加 DateOnly/DateOnly? 类型序列化处理 + /// + /// + /// + public static IList AddDateOnlyConverters(this IList converters) + { + converters.Add(new SystemTextJsonDateOnlyJsonConverter()); + converters.Add(new SystemTextJsonNullableDateOnlyJsonConverter()); + + return converters; + } + /// + /// 添加 TimeOnly/TimeOnly? 类型序列化处理 + /// + /// + /// + public static IList AddTimeOnlyConverters(this IList converters) + { + converters.Add(new SystemTextJsonTimeOnlyJsonConverter()); + converters.Add(new SystemTextJsonNullableTimeOnlyJsonConverter()); + + return converters; + } +#endif } \ No newline at end of file -- Gitee From e4e0f3f57ad0d600cd9a9969c2c7d54d894c0d4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E7=BA=A2=E9=B9=8F?= Date: Thu, 10 Nov 2022 16:39:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E5=A4=87=E6=B3=A8=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NewtonsoftJsonDateOnlyJsonConverter.cs | 47 ++++++++++++++++++- .../NewtonsoftJsonTimeOnlyJsonConverter.cs | 47 ++++++++++++++++++- .../SystemTextJsonDateOnlyJsonConverter.cs | 4 +- 3 files changed, 92 insertions(+), 6 deletions(-) diff --git a/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonDateOnlyJsonConverter.cs b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonDateOnlyJsonConverter.cs index c280a14e17..30a11a76b5 100644 --- a/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonDateOnlyJsonConverter.cs +++ b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonDateOnlyJsonConverter.cs @@ -30,6 +30,27 @@ namespace Furion.JsonSerialization; /// public class NewtonsoftJsonDateOnlyJsonConverter : JsonConverter { + /// + /// + /// + public NewtonsoftJsonDateOnlyJsonConverter() + { + Format ??= "yyyy-MM-dd"; + } + + /// + /// 构造函数 + /// + /// + public NewtonsoftJsonDateOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 日期格式化格式 + /// + public string Format { get; private set; } /// /// 反序列化 /// @@ -52,7 +73,7 @@ public class NewtonsoftJsonDateOnlyJsonConverter : JsonConverter /// public override void WriteJson(JsonWriter writer, DateOnly value, JsonSerializer serializer) { - serializer.Serialize(writer, value.ToString("yyyy-MM-dd")); + serializer.Serialize(writer, value.ToString(Format)); } } /// @@ -60,6 +81,27 @@ public class NewtonsoftJsonDateOnlyJsonConverter : JsonConverter /// public class NewtonsoftJsonNullableDateOnlyJsonConverter : JsonConverter { + /// + /// + /// + public NewtonsoftJsonNullableDateOnlyJsonConverter() + { + Format ??= "yyyy-MM-dd"; + } + + /// + /// 构造函数 + /// + /// + public NewtonsoftJsonNullableDateOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 日期格式化格式 + /// + public string Format { get; private set; } /// /// 反序列化 /// @@ -82,7 +124,8 @@ public class NewtonsoftJsonNullableDateOnlyJsonConverter : JsonConverter public override void WriteJson(JsonWriter writer, DateOnly? value, JsonSerializer serializer) { - serializer.Serialize(writer, value.HasValue ? value.Value.ToString("yyyy-MM-dd") : ""); + if (value == null) writer.WriteNull(); + else serializer.Serialize(writer, value.Value.ToString(Format)); } } #endif \ No newline at end of file diff --git a/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonTimeOnlyJsonConverter.cs b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonTimeOnlyJsonConverter.cs index d2dbe03ef0..9725e905b4 100644 --- a/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonTimeOnlyJsonConverter.cs +++ b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonTimeOnlyJsonConverter.cs @@ -30,6 +30,27 @@ namespace Furion.JsonSerialization; /// public class NewtonsoftJsonTimeOnlyJsonConverter : JsonConverter { + /// + /// + /// + public NewtonsoftJsonTimeOnlyJsonConverter() + { + Format ??= "HH:mm:ss"; + } + + /// + /// 构造函数 + /// + /// + public NewtonsoftJsonTimeOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 时间格式化格式 + /// + public string Format { get; private set; } /// /// 反序列化 /// @@ -52,7 +73,7 @@ public class NewtonsoftJsonTimeOnlyJsonConverter : JsonConverter /// public override void WriteJson(JsonWriter writer, TimeOnly value, JsonSerializer serializer) { - serializer.Serialize(writer, value.ToString("HH:mm:ss")); + serializer.Serialize(writer, value.ToString(Format)); } } /// @@ -60,6 +81,27 @@ public class NewtonsoftJsonTimeOnlyJsonConverter : JsonConverter /// public class NewtonsoftJsonNullableTimeOnlyJsonConverter : JsonConverter { + /// + /// + /// + public NewtonsoftJsonNullableTimeOnlyJsonConverter() + { + Format ??= "HH:mm:ss"; + } + + /// + /// 构造函数 + /// + /// + public NewtonsoftJsonNullableTimeOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 时间格式化格式 + /// + public string Format { get; private set; } /// /// 反序列化 /// @@ -82,7 +124,8 @@ public class NewtonsoftJsonNullableTimeOnlyJsonConverter : JsonConverter public override void WriteJson(JsonWriter writer, TimeOnly? value, JsonSerializer serializer) { - serializer.Serialize(writer, value.HasValue ? value.Value.ToString("HH:mm:ss") : ""); + if (value == null) writer.WriteNull(); + else serializer.Serialize(writer, value.Value.ToString(Format)); } } #endif \ No newline at end of file diff --git a/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonDateOnlyJsonConverter.cs b/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonDateOnlyJsonConverter.cs index bb6b5a6331..9aecbf3ce4 100644 --- a/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonDateOnlyJsonConverter.cs +++ b/framework/Furion.Pure/JsonSerialization/Converters/SystemTextJson/SystemTextJsonDateOnlyJsonConverter.cs @@ -51,7 +51,7 @@ public class SystemTextJsonDateOnlyJsonConverter : JsonConverter } /// - /// 时间格式化格式 + /// 日期格式化格式 /// public string Format { get; private set; } @@ -103,7 +103,7 @@ public class SystemTextJsonNullableDateOnlyJsonConverter : JsonConverter - /// 时间格式化格式 + /// 日期格式化格式 /// public string Format { get; private set; } -- Gitee