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 0000000000000000000000000000000000000000..30a11a76b5c5e0950e5dc36206dcb6cac6016eb1 --- /dev/null +++ b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonDateOnlyJsonConverter.cs @@ -0,0 +1,131 @@ +// 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 NewtonsoftJsonDateOnlyJsonConverter() + { + Format ??= "yyyy-MM-dd"; + } + + /// + /// 构造函数 + /// + /// + public NewtonsoftJsonDateOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 日期格式化格式 + /// + public string Format { get; private set; } + /// + /// 反序列化 + /// + /// + /// + /// + /// + /// + /// + 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(Format)); + } +} +/// +/// DateOnly? 类型序列化 +/// +public class NewtonsoftJsonNullableDateOnlyJsonConverter : JsonConverter +{ + /// + /// + /// + public NewtonsoftJsonNullableDateOnlyJsonConverter() + { + Format ??= "yyyy-MM-dd"; + } + + /// + /// 构造函数 + /// + /// + public NewtonsoftJsonNullableDateOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 日期格式化格式 + /// + public string Format { get; private set; } + /// + /// 反序列化 + /// + /// + /// + /// + /// + /// + /// + 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) + { + 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 new file mode 100644 index 0000000000000000000000000000000000000000..9725e905b4c5033bbc6eed82ea55009fb3fdfcc4 --- /dev/null +++ b/framework/Furion.Pure/JsonSerialization/Converters/NewtonsoftJson/NewtonsoftJsonTimeOnlyJsonConverter.cs @@ -0,0 +1,131 @@ +// 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 NewtonsoftJsonTimeOnlyJsonConverter() + { + Format ??= "HH:mm:ss"; + } + + /// + /// 构造函数 + /// + /// + public NewtonsoftJsonTimeOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 时间格式化格式 + /// + public string Format { get; private set; } + /// + /// 反序列化 + /// + /// + /// + /// + /// + /// + /// + 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(Format)); + } +} +/// +/// TimeOnly? 类型序列化 +/// +public class NewtonsoftJsonNullableTimeOnlyJsonConverter : JsonConverter +{ + /// + /// + /// + public NewtonsoftJsonNullableTimeOnlyJsonConverter() + { + Format ??= "HH:mm:ss"; + } + + /// + /// 构造函数 + /// + /// + public NewtonsoftJsonNullableTimeOnlyJsonConverter(string format) + { + Format = format; + } + + /// + /// 时间格式化格式 + /// + public string Format { get; private set; } + /// + /// 反序列化 + /// + /// + /// + /// + /// + /// + /// + 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) + { + 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 new file mode 100644 index 0000000000000000000000000000000000000000..9aecbf3ce40a00ed2502c0bcccaf40d3b0a017d5 --- /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 0000000000000000000000000000000000000000..7e686c5a2f7d81ba03694b3f7248776b611599ba --- /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 bb0b89f429b7fd80d57512b40b51451bfa4d0ef7..c95410ec82d36ce65370b0ed8ac48b90f21149fc 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 77cd7fa60f59305571fedc8570cee32241f8769d..763bb7abbf4619f3034c356e814b6d56ebb39fb8 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