using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace XFEWorkflow.Abstractions;
public static class WorkflowLiteralValues
{
public static readonly string[] TypeNames = ["文本", "数字", "布尔", "对象", "数组", "空值"];
public static int TypeIndex(JsonNode? value) => value?.GetValueKind() switch
{ JsonValueKind.String => 0, JsonValueKind.Number => 1, JsonValueKind.True or JsonValueKind.False => 2, JsonValueKind.Object => 3, JsonValueKind.Array => 4, _ => 5 };
public static JsonNode? Default(int type) => type switch { 0 => JsonValue.Create(""), 1 => JsonValue.Create(0), 2 => JsonValue.Create(false), 3 => new JsonObject(), 4 => new JsonArray(), 5 => null, _ => throw new ArgumentOutOfRangeException(nameof(type)) };
public static JsonNode ParseNumber(string text)
{
JsonNode? value;
try { value = JsonNode.Parse(text); } catch (JsonException) { throw new ArgumentException("请输入有效数字,使用小数点分隔小数"); }
if (value?.GetValueKind() != JsonValueKind.Number || !decimal.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out _)) throw new ArgumentException("请输入 decimal 范围内的数字;联系人 ID 请使用文本类型");
return value;
}
}
using System.Globalization;
using System.Text.Json;
using System.Text.Json.Nodes;
namespace XFEWorkflow.Abstractions;
public static class WorkflowLiteralValues
{
public static readonly string[] TypeNames = ["文本", "数字", "布尔", "对象", "数组", "空值"];
public static int TypeIndex(JsonNode? value) => value?.GetValueKind() switch
{ JsonValueKind.String => 0, JsonValueKind.Number => 1, JsonValueKind.True or JsonValueKind.False => 2, JsonValueKind.Object => 3, JsonValueKind.Array => 4, _ => 5 };
public static JsonNode? Default(int type) => type switch { 0 => JsonValue.Create(""), 1 => JsonValue.Create(0), 2 => JsonValue.Create(false), 3 => new JsonObject(), 4 => new JsonArray(), 5 => null, _ => throw new ArgumentOutOfRangeException(nameof(type)) };
public static JsonNode ParseNumber(string text)
{
JsonNode? value;
try { value = JsonNode.Parse(text); } catch (JsonException) { throw new ArgumentException("请输入有效数字,使用小数点分隔小数"); }
if (value?.GetValueKind() != JsonValueKind.Number || !decimal.TryParse(text, NumberStyles.Float, CultureInfo.InvariantCulture, out _)) throw new ArgumentException("请输入 decimal 范围内的数字;联系人 ID 请使用文本类型");
return value;
}
}