-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add serialisation compatibility of TimeSpan and DateTime (#16205)
Co-authored-by: Mike Alhayek <[email protected]> Co-authored-by: Hisham Bin Ateya <[email protected]>
- Loading branch information
1 parent
2cc41ce
commit 3d3f172
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/DateTimeJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace OrchardCore.Json.Serialization; | ||
|
||
public class DateTimeJsonConverter : JsonConverter<DateTime> | ||
{ | ||
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)); | ||
} |
28 changes: 28 additions & 0 deletions
28
src/OrchardCore/OrchardCore.Abstractions/Json/Serialization/TimeSpanJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace OrchardCore.Json.Serialization; | ||
|
||
public class TimeSpanJsonConverter : JsonConverter<TimeSpan> | ||
{ | ||
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters