Update expression serialization and deserialization
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.
This commit is contained in:
parent
4e78aab800
commit
a6783256f4
|
|
@ -1,3 +1,4 @@
|
|||
using System.Text.Json;
|
||||
using Elsa.Expressions.Contracts;
|
||||
|
||||
namespace Elsa.Expressions.Models;
|
||||
|
|
@ -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>
|
||||
|
|
@ -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!;
|
||||
}
|
||||
|
|
@ -2,4 +2,7 @@ using System.Text.Json;
|
|||
|
||||
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);
|
||||
|
|
@ -47,7 +47,7 @@ public class InputJsonConverter<T> : JsonConverter<Input<T>>
|
|||
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)!;
|
||||
|
|
|
|||
|
|
@ -62,9 +62,9 @@ public class DefaultExpressionDescriptorProvider : IExpressionDescriptorProvider
|
|||
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);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
@ -86,16 +86,12 @@ public class DefaultExpressionDescriptorProvider : IExpressionDescriptorProvider
|
|||
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
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue