Skip to content

Commit

Permalink
Update expression serialization and deserialization
Browse files Browse the repository at this point in the history
Refined the expression serialization context and its deserialization method for more extensive usage. Also, simplified the handling of value retrieval in the DefaultExpressionDescriptorProvider. This allows greater control over serialization processes and makes the code more concise.
  • Loading branch information
sfmskywalker committed Feb 12, 2024
1 parent 4e78aab commit a678325
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
19 changes: 17 additions & 2 deletions src/modules/Elsa.Expressions/Models/ExpressionDescriptor.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using Elsa.Expressions.Contracts;

namespace Elsa.Expressions.Models;
Expand All @@ -7,6 +8,20 @@ namespace Elsa.Expressions.Models;
/// </summary>
public class ExpressionDescriptor
{
/// <summary>
/// Initializes a new instance of the <see cref="ExpressionDescriptor"/> class.
/// </summary>
public ExpressionDescriptor()
{
// Default deserialization function.
Deserialize = context =>
{
return context.JsonElement.ValueKind == JsonValueKind.Object
? context.JsonElement.Deserialize<Expression>((JsonSerializerOptions?)context.Options)!
: new Expression(context.ExpressionType, null!);
};
}

/// <summary>
/// Gets or sets the syntax name.
/// </summary>
Expand Down Expand Up @@ -41,9 +56,9 @@ public class ExpressionDescriptor
/// Gets or sets the memory block reference factory.
/// </summary>
public Func<MemoryBlockReference> MemoryBlockReferenceFactory { get; set; } = () => new MemoryBlockReference();

/// <summary>
/// Gets or sets the expression deserialization function.
/// </summary>
public Func<ExpressionSerializationContext, Expression> Deserialize { get; set; } = default!;
public Func<ExpressionSerializationContext, Expression> Deserialize { get; set; } = default!;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

namespace Elsa.Expressions.Models;

public record ExpressionSerializationContext(JsonElement JsonElement, JsonSerializerOptions Options, Type MemoryBlockType);
/// <summary>
/// Defines the context for expression serialization.
/// </summary>
public record ExpressionSerializationContext(string ExpressionType, JsonElement JsonElement, JsonSerializerOptions Options, Type MemoryBlockType);
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public override Input<T> Read(ref Utf8JsonReader reader, Type typeToConvert, Jso
return default!;

var memoryBlockType = memoryBlockReference.GetType();
var context = new ExpressionSerializationContext(expressionElement, options, memoryBlockType);
var context = new ExpressionSerializationContext(expressionTypeName!, expressionElement, options, memoryBlockType);
var expression = expressionDescriptor!.Deserialize(context);

return (Input<T>)Activator.CreateInstance(typeof(Input<T>), expression, memoryBlockReference)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ private ExpressionDescriptor CreateVariableDescriptor()
memoryBlockReferenceFactory: () => new Variable(),
deserialize: context =>
{
var expressionValueElement = context.JsonElement.TryGetProperty("value", out var expressionElementValueValue) ? expressionElementValueValue : default;
var expressionValue = expressionValueElement.Deserialize(context.MemoryBlockType, context.Options);
return new Expression("Variable", expressionValue);
var valueElement = context.JsonElement.TryGetProperty("value", out var v) ? v : default;
var value = valueElement.Deserialize(context.MemoryBlockType, context.Options);
return new Expression("Variable", value);
}
);
}
Expand All @@ -86,16 +86,12 @@ private static ExpressionDescriptor CreateDescriptor<THandler>(
IsSerializable = isSerializable,
IsBrowsable = isBrowsable,
HandlerFactory = sp => ActivatorUtilities.GetServiceOrCreateInstance<THandler>(sp),
MemoryBlockReferenceFactory = memoryBlockReferenceFactory ?? (() => new MemoryBlockReference()),
Deserialize = deserialize ??
(context =>
{
return context.JsonElement.ValueKind == JsonValueKind.Object
? context.JsonElement.Deserialize<Expression>((JsonSerializerOptions?)context.Options)!
: new Expression(expressionType, null!);
})
MemoryBlockReferenceFactory = memoryBlockReferenceFactory ?? (() => new MemoryBlockReference())
};

if (deserialize != null)
descriptor.Deserialize = deserialize;

if (monacoLanguage != null)
descriptor.Properties = new
{
Expand Down

0 comments on commit a678325

Please sign in to comment.