From 5773b3a85b695982c9ebc096b64838f6b0ed53c6 Mon Sep 17 00:00:00 2001 From: Joe Feser Date: Mon, 29 Jul 2024 19:07:25 -0500 Subject: [PATCH] feat(): add .net6 and .net8 to core project and get it to compile #680 --- .../Utf8Json/Formatters/DateTimeFormatter.cs | 47 +++++++++++++++++++ .../Utf8Json/Resolvers/BuiltinResolver.cs | 7 +++ 2 files changed, 54 insertions(+) diff --git a/src/OpenSearch.Net/Utf8Json/Formatters/DateTimeFormatter.cs b/src/OpenSearch.Net/Utf8Json/Formatters/DateTimeFormatter.cs index 534eaa9155..f746f58b1f 100644 --- a/src/OpenSearch.Net/Utf8Json/Formatters/DateTimeFormatter.cs +++ b/src/OpenSearch.Net/Utf8Json/Formatters/DateTimeFormatter.cs @@ -948,4 +948,51 @@ public TimeSpan Deserialize(ref JsonReader reader, IJsonFormatterResolver format throw new InvalidOperationException("invalid TimeSpan format. value:" + StringEncoding.UTF8.GetString(str.Array, str.Offset, str.Count)); } } + +#if NET6_0_OR_GREATER + internal sealed class DateOnlyFormatter : IJsonFormatter + { + public static readonly IJsonFormatter Default = new DateOnlyFormatter(); + + private readonly string _formatString; + + public DateOnlyFormatter() => _formatString = "o"; //we are choosing the ISO 8601 format as the default + + public DateOnlyFormatter(string formatString) => _formatString = formatString; + + public void Serialize(ref JsonWriter writer, DateOnly value, IJsonFormatterResolver formatterResolver) => + writer.WriteString(value.ToString(_formatString)); + + public DateOnly Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) + { + var str = reader.ReadString(); + return _formatString == null || _formatString == "o" + ? DateOnly.Parse(str, CultureInfo.InvariantCulture) + : DateOnly.ParseExact(str, _formatString, CultureInfo.InvariantCulture); + } + } + + internal sealed class TimeOnlyFormatter : IJsonFormatter + { + public static readonly IJsonFormatter Default = new TimeOnlyFormatter(); + + private readonly string _formatString; + + public TimeOnlyFormatter() => _formatString = "o"; //if we do not use o, you loose the milliseconds and microseconds + + public TimeOnlyFormatter(string formatString) => _formatString = formatString; + + public void Serialize(ref JsonWriter writer, TimeOnly value, IJsonFormatterResolver formatterResolver) => + writer.WriteString(value.ToString(_formatString)); + + public TimeOnly Deserialize(ref JsonReader reader, IJsonFormatterResolver formatterResolver) + { + var str = reader.ReadString(); + return _formatString == null || _formatString == "o" + ? TimeOnly.Parse(str, CultureInfo.InvariantCulture) + : TimeOnly.ParseExact(str, _formatString, CultureInfo.InvariantCulture); + } + } +#endif + } diff --git a/src/OpenSearch.Net/Utf8Json/Resolvers/BuiltinResolver.cs b/src/OpenSearch.Net/Utf8Json/Resolvers/BuiltinResolver.cs index 5d79a7eb36..bf1b682205 100644 --- a/src/OpenSearch.Net/Utf8Json/Resolvers/BuiltinResolver.cs +++ b/src/OpenSearch.Net/Utf8Json/Resolvers/BuiltinResolver.cs @@ -121,6 +121,13 @@ internal static class BuiltinResolverGetFormatterHelper {typeof(TimeSpan?), new StaticNullableFormatter(ISO8601TimeSpanFormatter.Default)}, {typeof(DateTimeOffset?),new StaticNullableFormatter(ISO8601DateTimeOffsetFormatter.Default)}, +#if NET6_0_OR_GREATER + {typeof(DateOnly), DateOnlyFormatter.Default}, + {typeof(TimeOnly), TimeOnlyFormatter.Default}, + {typeof(DateOnly?), new StaticNullableFormatter(DateOnlyFormatter.Default)}, + {typeof(TimeOnly?), new StaticNullableFormatter(TimeOnlyFormatter.Default)}, +#endif + {typeof(string), NullableStringFormatter.Default}, {typeof(char), CharFormatter.Default}, {typeof(char?), NullableCharFormatter.Default},