Skip to content

Commit

Permalink
Revert "Add serialisation compatibility of TimeSpan and DateTime (Orc…
Browse files Browse the repository at this point in the history
…hardCMS#16205)"

This reverts commit 3d3f172.
  • Loading branch information
hyzx86 authored Jun 11, 2024
1 parent 3d3f172 commit 3241577
Show file tree
Hide file tree
Showing 4 changed files with 1 addition and 104 deletions.
2 changes: 0 additions & 2 deletions src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

This file was deleted.

This file was deleted.

48 changes: 1 addition & 47 deletions test/OrchardCore.Tests/Data/ContentItemTests.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// To validate <see cref="DateTimeJsonConverter"/>
/// and <seealso cref="TimeSpanJsonConverter"/>
/// </summary>
[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<TimeField>();
var dateField = jobject.SelectNode("DateFieldTest").ToObject<DateField>();
var dateTimeField = jobject.SelectNode("DateTimeFieldTest").ToObject<DateTimeField>();
var timezoneDateTimeFieldTest = jobject.SelectNode("TimezoneDateTimeFieldTest").ToObject<DateTimeField>();

// 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()
{
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 3241577

Please sign in to comment.