diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs index 727e2feb931..cde64d5a524 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs @@ -36,8 +36,6 @@ static JOptions() Default = new JsonSerializerOptions(Base); Default.Converters.Add(new DynamicJsonConverter()); Default.Converters.Add(new PathStringJsonConverter()); - Default.Converters.Add(new TimeSpanJsonConverter()); - Default.Converters.Add(new DateTimeJsonConverter()); Indented = new JsonSerializerOptions(Default) { diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs deleted file mode 100644 index 9c2915aa2dc..00000000000 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System; -using System.Globalization; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace OrchardCore.Json.Serialization; - -public class DateTimeJsonConverter : JsonConverter -{ - public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (typeToConvert != typeof(DateTime)) - { - throw new ArgumentException("Unexpected type to convert.", nameof(typeToConvert)); - } - - if (!reader.TryGetDateTime(out DateTime value) && DateTime.TryParse(reader.GetString()!, out value)) - { - return value; - } - - return value; - } - - public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) - => writer.WriteStringValue(value.ToString("yyyy-MM-ddTHH:mm:ssZ", CultureInfo.InvariantCulture)); -} diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/TimeSpanJsonConverter.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/TimeSpanJsonConverter.cs deleted file mode 100644 index f7811abb64a..00000000000 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/TimeSpanJsonConverter.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace OrchardCore.Json.Serialization; - -public class TimeSpanJsonConverter : JsonConverter -{ - public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - if (reader.TokenType != JsonTokenType.String) - { - throw new JsonException($"Unexpected token parsing TimeSpan. Expected a string, got '{reader.TokenType}'."); - } - - var stringValue = reader.GetString(); - - if (TimeSpan.TryParse(stringValue, out var timeSpan)) - { - return timeSpan; - } - - throw new JsonException($"Unable to convert '{stringValue}' to TimeSpan."); - } - - public override void Write(Utf8JsonWriter writer, TimeSpan value, JsonSerializerOptions options) - => writer.WriteStringValue(value.ToString()); -} diff --git a/test/OrchardCore.Tests/Data/ContentItemTests.cs b/test/OrchardCore.Tests/Data/ContentItemTests.cs index 91494ff1605..8d61337943c 100644 --- a/test/OrchardCore.Tests/Data/ContentItemTests.cs +++ b/test/OrchardCore.Tests/Data/ContentItemTests.cs @@ -1,58 +1,12 @@ using System.Text.Json; using System.Text.Json.Dynamic; using System.Text.Json.Nodes; -using OrchardCore.ContentFields.Fields; using OrchardCore.ContentManagement; -using OrchardCore.Json.Serialization; namespace OrchardCore.Tests.Data { public class ContentItemTests { - /// - /// To validate - /// and - /// - [Fact] - public void JsonNode_WhenParseCalled_ConvertShortTimeFormatToTimeField() - { - // Arrange - var jsonStr = """ - { - "TimeFieldTest": { - "Value": "13:05" - }, - "DateTimeFieldTest": { - "Value": "2024-5-31 13:05" - }, - "TimezoneDateTimeFieldTest": { - "Value": "2022-12-13T21:02:18.399-05:00" - }, - "DateFieldTest": { - "Value": "2024-5-31" - } - } - """; - - // Act - var jobject = JsonNode.Parse(jsonStr); - var timeField = jobject.SelectNode("TimeFieldTest").ToObject(); - var dateField = jobject.SelectNode("DateFieldTest").ToObject(); - var dateTimeField = jobject.SelectNode("DateTimeFieldTest").ToObject(); - var timezoneDateTimeFieldTest = jobject.SelectNode("TimezoneDateTimeFieldTest").ToObject(); - - // Assert - Assert.Equal("13:05:00", timeField.Value.Value.ToString()); - Assert.Equal("2024-05-31", dateField.Value.Value.ToString("yyyy-MM-dd")); - Assert.Equal("2024-05-31 13:05", dateTimeField.Value.Value.ToString("yyyy-MM-dd HH:mm")); - Assert.Equal("13:05:00", JObject.FromObject(timeField).SelectNode("Value").ToString()); - Assert.Equal("2024-05-31T00:00:00Z", JObject.FromObject(dateField).SelectNode("Value").ToString()); - Assert.Equal("2024-05-31T13:05:00Z", JObject.FromObject(dateTimeField).SelectNode("Value").ToString()); - - var utcTime = TimeZoneInfo.ConvertTimeToUtc(timezoneDateTimeFieldTest.Value.Value); - Assert.Equal("2022-12-14 02:02:18", utcTime.ToString("yyyy-MM-dd HH:mm:ss")); - } - [Fact] public void ShouldSerializeContent() { @@ -209,7 +163,7 @@ public void ContentShouldStoreDateTimeFields() var json = JConvert.SerializeObject(contentItem); - Assert.Contains(@"""MyPart"":{""Text"":""test"",""myField"":{""Value"":""2024-01-01T10:42:00Z""}}", json); + Assert.Contains(@"""MyPart"":{""Text"":""test"",""myField"":{""Value"":""2024-01-01T10:42:00""}}", json); } [Fact]