Reuse JsonSerializerOptions (#5283)

This commit is contained in:
Sipke Schoorstra 2024-04-26 15:29:16 +02:00 committed by GitHub
parent fefb077678
commit 4f2baec322
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 30 additions and 22 deletions

View file

@ -7,6 +7,7 @@ using Elsa.Common.Converters;
using Elsa.Expressions.Contracts;
using Elsa.Expressions.Helpers;
using Elsa.Expressions.Models;
using Elsa.Extensions;
using JetBrains.Annotations;
namespace Elsa.Workflows.Expressions;
@ -17,6 +18,20 @@ namespace Elsa.Workflows.Expressions;
[UsedImplicitly]
public class ObjectExpressionHandler : IExpressionHandler
{
private JsonSerializerOptions? _serializerOptions;
private JsonSerializerOptions SerializerOptions =>
_serializerOptions ??= new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReferenceHandler = ReferenceHandler.Preserve,
PropertyNameCaseInsensitive = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
}.WithConverters(
new IntegerJsonConverter(),
new DecimalJsonConverter(),
new JsonStringEnumConverter());
/// <inheritdoc />
[RequiresUnreferencedCode("The type is not known at compile time.")]
public ValueTask<object?> EvaluateAsync(Expression expression, Type returnType, ExpressionExecutionContext context, ExpressionEvaluatorOptions options)
@ -26,18 +41,7 @@ public class ObjectExpressionHandler : IExpressionHandler
if (string.IsNullOrWhiteSpace(value))
return ValueTask.FromResult(default(object?));
var serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReferenceHandler = ReferenceHandler.Preserve,
PropertyNameCaseInsensitive = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
serializerOptions.Converters.Add(new IntegerJsonConverter());
serializerOptions.Converters.Add(new DecimalJsonConverter());
serializerOptions.Converters.Add(new JsonStringEnumConverter());
var converterOptions = new ObjectConverterOptions(serializerOptions);
var converterOptions = new ObjectConverterOptions(SerializerOptions);
var model = value.ConvertTo(returnType, converterOptions);
return ValueTask.FromResult(model);
}

View file

@ -17,6 +17,19 @@ namespace Elsa.Extensions;
/// </summary>
public static class VariableExtensions
{
private static JsonSerializerOptions? _serializerOptions;
private static JsonSerializerOptions SerializerOptions =>
_serializerOptions ??= new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReferenceHandler = ReferenceHandler.Preserve,
PropertyNameCaseInsensitive = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
}.WithConverters(
new JsonStringEnumConverter(),
new ExpandoObjectConverterFactory());
/// <summary>
/// Configures the variable to use the <see cref="WorkflowStorageDriver"/>.
/// </summary>
@ -58,16 +71,7 @@ public static class VariableExtensions
public static object? ParseValue(this Variable variable, object? value)
{
var genericType = variable.GetType().GenericTypeArguments.FirstOrDefault();
var serializerOptions = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
ReferenceHandler = ReferenceHandler.Preserve,
PropertyNameCaseInsensitive = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
serializerOptions.Converters.Add(new JsonStringEnumConverter());
serializerOptions.Converters.Add(new ExpandoObjectConverterFactory());
var converterOptions = new ObjectConverterOptions(serializerOptions);
var converterOptions = new ObjectConverterOptions(SerializerOptions);
return genericType == null ? value : value?.ConvertTo(genericType, converterOptions);
}