Skip to content

Commit

Permalink
Merge pull request #739 from leancodepl/feature/share-jsonoptions
Browse files Browse the repository at this point in the history
Share `JsonOptions` with the wider ASP.NET Core ecosystem
  • Loading branch information
jakubfijalkowski authored Jan 3, 2025
2 parents 0bdde88 + 0e812af commit 57619a3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ but this project DOES NOT adhere to [Semantic Versioning](http://semver.org/).
* Remove LeanCode.ExternalIdentityProviders
* Remove LeanCode.PdfRocket
* Remove StyleCop completely
* JSON serializer for CQRS now shares options with ASP.NET Core's `JsonOptions` by default

## 8.1

Expand Down
39 changes: 16 additions & 23 deletions src/CQRS/LeanCode.CQRS.AspNetCore/Serialization/ISerializer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json;
using LeanCode.Serialization;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.Extensions.Options;

namespace LeanCode.CQRS.AspNetCore.Serialization;

Expand All @@ -9,35 +11,26 @@ public interface ISerializer
ValueTask<object?> DeserializeAsync(Stream utf8Json, Type returnType, CancellationToken cancellationToken);
}

public sealed class Utf8JsonSerializer : ISerializer
public sealed class Utf8JsonSerializer(IOptions<JsonOptions> options) : ISerializer
{
public static readonly JsonSerializerOptions DefaultOptions = new()
{
Converters =
{
new JsonLaxDateOnlyConverter(),
new JsonLaxTimeOnlyConverter(),
new JsonLaxDateTimeOffsetConverter(),
},
};

private readonly JsonSerializerOptions? options;

public Utf8JsonSerializer(JsonSerializerOptions? options)
{
this.options = options;
}

public Utf8JsonSerializer()
: this(null) { }

public ValueTask<object?> DeserializeAsync(Stream utf8Json, Type returnType, CancellationToken cancellationToken)
{
return JsonSerializer.DeserializeAsync(utf8Json, returnType, options, cancellationToken);
return JsonSerializer.DeserializeAsync(
utf8Json,
returnType,
options.Value.SerializerOptions,
cancellationToken
);
}

public Task SerializeAsync(Stream utf8Json, object value, Type inputType, CancellationToken cancellationToken)
{
return JsonSerializer.SerializeAsync(utf8Json, value, inputType, options, cancellationToken);
return JsonSerializer.SerializeAsync(
utf8Json,
value,
inputType,
options.Value.SerializerOptions,
cancellationToken
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using LeanCode.CQRS.Execution;
using LeanCode.CQRS.Security;
using LeanCode.CQRS.Validation;
using LeanCode.Serialization;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace LeanCode.CQRS.AspNetCore;

Expand All @@ -18,10 +20,22 @@ public static class ServiceCollectionCQRSExtensions
public static CQRSServicesBuilder AddCQRS(
this IServiceCollection serviceCollection,
TypesCatalog contractsCatalog,
TypesCatalog handlersCatalog
TypesCatalog handlersCatalog,
bool configureJsonOptions = true
)
{
serviceCollection.AddSingleton<ISerializer>(_ => new Utf8JsonSerializer(Utf8JsonSerializer.DefaultOptions));
if (configureJsonOptions)
{
serviceCollection.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.Converters.Add(new JsonLaxDateOnlyConverter());
options.SerializerOptions.Converters.Add(new JsonLaxTimeOnlyConverter());
options.SerializerOptions.Converters.Add(new JsonLaxDateTimeOffsetConverter());
options.SerializerOptions.PropertyNamingPolicy = null;
});
}

serviceCollection.AddSingleton<ISerializer, Utf8JsonSerializer>();

var objectsSource = new CQRSObjectsRegistrationSource(serviceCollection, new ObjectExecutorFactory());
objectsSource.AddCQRSObjects(contractsCatalog, handlersCatalog);
Expand Down

0 comments on commit 57619a3

Please sign in to comment.