Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Declare known-converters and use them in DocumentJsonSerializerOptions #16417

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Text.Json.Serialization;
using Microsoft.Extensions.Options;
using OrchardCore.Json;
using OrchardCore.Json.Serialization;

namespace OrchardCore.Extensions;

Expand All @@ -25,7 +24,10 @@ public void Configure(DocumentJsonSerializerOptions options)
options.SerializerOptions.WriteIndented = JOptions.Base.WriteIndented;

options.SerializerOptions.TypeInfoResolverChain.Add(new PolymorphicJsonTypeInfoResolver(_derivedTypesOptions));
options.SerializerOptions.Converters.Add(DynamicJsonConverter.Instance);
options.SerializerOptions.Converters.Add(PathStringJsonConverter.Instance);

foreach (var converter in JOptions.KnownConverters)
{
options.SerializerOptions.Converters.Add(converter);
}
}
}
17 changes: 13 additions & 4 deletions src/OrchardCore/OrchardCore.Abstractions/Json/JOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ namespace System.Text.Json;
/// </summary>
public static class JOptions
{
public static readonly JsonConverter[] KnownConverters =
[
DynamicJsonConverter.Instance,
PathStringJsonConverter.Instance,
TimeSpanJsonConverter.Instance,
DateTimeJsonConverter.Instance,
];

public static readonly JsonSerializerOptions Base = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
Expand All @@ -34,10 +42,11 @@ public static class JOptions
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());

foreach (var converter in KnownConverters)
{
Default.Converters.Add(converter);
}

Indented = new JsonSerializerOptions(Default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ namespace OrchardCore.Json.Serialization;

public class DateTimeJsonConverter : JsonConverter<DateTime>
{
public static readonly DateTimeJsonConverter Instance = new();

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))
if (!reader.TryGetDateTime(out var value) && DateTime.TryParse(reader.GetString()!, out value))
{
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace OrchardCore.Json.Serialization;

public class TimeSpanJsonConverter : JsonConverter<TimeSpan>
{
public static readonly TimeSpanJsonConverter Instance = new();

public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType != JsonTokenType.String)
Expand All @@ -14,7 +16,7 @@ public override TimeSpan Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
}

var stringValue = reader.GetString();

if (TimeSpan.TryParse(stringValue, out var timeSpan))
{
return timeSpan;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static IServiceCollection AddUsers(this IServiceCollection services)

services.Configure<DocumentJsonSerializerOptions>(options =>
{
options.SerializerOptions.Converters.Add(new LoginInfoJsonConverter());
options.SerializerOptions.Converters.Add(LoginInfoJsonConverter.Instance);
});

return services;
Expand Down