Add support for JsonObject to target type conversion

This commit is contained in:
Sipke Schoorstra 2023-11-29 15:09:00 +01:00
parent 3352cbee40
commit 213f114cfc

View file

@ -3,6 +3,7 @@ using System.ComponentModel;
using System.Dynamic;
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Elsa.Expressions.Contracts;
using Elsa.Expressions.Exceptions;
@ -70,15 +71,22 @@ public static class ObjectConverter
var underlyingTargetType = Nullable.GetUnderlyingType(targetType) ?? targetType;
var underlyingSourceType = Nullable.GetUnderlyingType(sourceType) ?? sourceType;
if (value is JsonElement jsonNumber && jsonNumber.ValueKind == JsonValueKind.Number && underlyingTargetType == typeof(string))
if (value is JsonElement { ValueKind: JsonValueKind.Number } jsonNumber && underlyingTargetType == typeof(string))
return jsonNumber.ToString().ConvertTo(underlyingTargetType);
if (value is JsonElement jsonObject)
if (value is JsonElement jsonElement)
{
if (jsonObject.ValueKind == JsonValueKind.String && underlyingTargetType != typeof(string))
return jsonObject.GetString().ConvertTo(underlyingTargetType);
if (jsonElement.ValueKind == JsonValueKind.String && underlyingTargetType != typeof(string))
return jsonElement.GetString().ConvertTo(underlyingTargetType);
return jsonObject.Deserialize(targetType, options);
return jsonElement.Deserialize(targetType, options);
}
if (value is JsonObject jsonObject)
{
return underlyingTargetType == typeof(string)
? jsonObject.ToString()
: jsonObject.Deserialize(targetType, options);
}
if (underlyingSourceType == typeof(string) && !underlyingTargetType.IsPrimitive && underlyingTargetType != typeof(object))