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

Reuse JsonSerializerOptions #5283

Merged
merged 1 commit into from
Apr 26, 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 @@ -7,6 +7,7 @@
using Elsa.Expressions.Contracts;
using Elsa.Expressions.Helpers;
using Elsa.Expressions.Models;
using Elsa.Extensions;
using JetBrains.Annotations;

namespace Elsa.Workflows.Expressions;
Expand All @@ -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)
Expand All @@ -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);
}
Expand Down
24 changes: 14 additions & 10 deletions src/modules/Elsa.Workflows.Core/Extensions/VariableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -58,16 +71,7 @@ public static Variable WithStorage(this Variable variable, Type storageDriverTyp
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);
}

Expand Down
Loading