using System.Text.Json.Nodes; using XFEWorkflow.Abstractions; namespace XFEWorkflow.Core.Runtime; public static partial class WorkflowBasicNodes { private static void AddObjects(Dictionary nodes) { const string category = "对象与 JSON"; Add(nodes, "object.set", "设置对象字段", category, "obj[key] = value;key 是完整字段名(点和斜杠按原文),返回新对象,不修改来源。创建或替换字段,null 也可存入。", [O(), S("key", "字段名", "name"), V("field", "字段值")], [Out(WorkflowValueType.Object)], (x, _) => { var result = WorkflowJson.Copy(Object(x)); result[Str(x, "key")] = x["field"]?.DeepClone(); return Value(result); }); Add(nodes, "object.remove", "删除对象字段", category, "Dictionary.Remove;删除顶层完整字段名,返回新对象与是否删除。", [O(), S("key", "字段名", "name")], [Out(WorkflowValueType.Object), Out(WorkflowValueType.Boolean, "removed", "是否删除")], (x, _) => { var result = WorkflowJson.Copy(Object(x)); var removed = result.Remove(Str(x, "key")); return new() { ["value"] = result, ["removed"] = removed }; }); Add(nodes, "object.has", "对象字段存在判断", category, "ContainsKey;支持点路径与 JSON Pointer。字段存在且为 null 也返回 true。", [O(), S("path", "字段路径", "id")], [Out(WorkflowValueType.Boolean)], (x, _) => Value(WorkflowObjectFields.Read(Object(x), Str(x, "path")).Found)); foreach (var (id, name) in new[] { ("keys", "对象字段名数组"), ("values", "对象字段值数组"), ("entries", "对象键值对数组") }) Add(nodes, "object." + id, name, category, "Dictionary.Keys / Values / KeyValuePair;按字段原有顺序输出,entries 每项为 {key,value}。", [O()], [Out(WorkflowValueType.Array)], (x, ct) => Value(Pack(Object(x).Select(pair => id switch { "keys" => (JsonNode?)JsonValue.Create(pair.Key), "values" => pair.Value, _ => new JsonObject { ["key"] = pair.Key, ["value"] = pair.Value?.DeepClone() } }), ct))); Add(nodes, "object.count", "对象字段数量", category, "Dictionary.Count;对象的顶层字段数量。", [O()], [Out(WorkflowValueType.Number)], (x, _) => Value((decimal)Object(x).Count)); Add(nodes, "object.merge", "合并对象", category, "浅合并两个对象;overwrite=true 时右侧覆盖同名字段,否则保留左侧。嵌套对象整体作为一个字段,返回新对象。", [O("left", "左对象"), O("right", "右对象"), B("overwrite", "覆盖同名字段", true)], [Out(WorkflowValueType.Object)], (x, ct) => { var result = WorkflowJson.Copy(Object(x, "left")); foreach (var pair in Object(x, "right")) { ct.ThrowIfCancellationRequested(); if (Flag(x, "overwrite") || !result.ContainsKey(pair.Key)) result[pair.Key] = pair.Value?.DeepClone(); } return Value(result); }); Add(nodes, "object.pick", "选择对象字段", category, "从对象复制 keys 数组指定的顶层完整字段名,缺失字段略过,重复名称只保留一次。", [O(), A("keys", "字段名数组")], [Out(WorkflowValueType.Object)], (x, ct) => { var source = Object(x); JsonObject result = []; foreach (var key in Array(x, "keys")) { ct.ThrowIfCancellationRequested(); var name = Key(key); if (source.TryGetPropertyValue(name, out var value)) result[name] = value?.DeepClone(); } return Value(result); }); Add(nodes, "object.fromPairs", "从数组创建对象", category, "将 keys 文本数组与 values 数组逐项对应生成对象。长度必须相等,字段名重复时报错。", [A("keys", "字段名数组"), A("values", "字段值数组")], [Out(WorkflowValueType.Object)], (x, ct) => { var keys = Array(x, "keys"); var values = Array(x, "values"); if (keys.Count != values.Count) throw new ArgumentException("字段名与字段值数量必须相同"); JsonObject result = []; for (var i = 0; i < keys.Count; i++) { ct.ThrowIfCancellationRequested(); var key = Key(keys[i]); if (result.ContainsKey(key)) throw new ArgumentException("字段名重复:" + key); result[key] = values[i]?.DeepClone(); } return Value(result); }); Add(nodes, "object.fromEntries", "从键值对创建对象", category, "将 [{key,value}] 转换为对象;key 必须为文本,同名字段报错,保留 null。", [A("entries", "键值对数组")], [Out(WorkflowValueType.Object)], (x, ct) => { JsonObject result = []; foreach (var entry in Array(x, "entries")) { ct.ThrowIfCancellationRequested(); if (entry is not JsonObject obj || !obj.ContainsKey("value")) throw new ArgumentException("每项需要 key 与 value 字段"); var key = Key(obj["key"]); if (result.ContainsKey(key)) throw new ArgumentException("字段名重复:" + key); result[key] = obj["value"]?.DeepClone(); } return Value(result); }); Add(nodes, "value.clone", "复制数据", category, "DeepClone;深度复制任意 JSON 值,保留类型、中文、表情和字面表达式对象。", [V()], [Out(WorkflowValueType.Any)], (x, _) => Value(x["value"])); Add(nodes, "json.parse", "解析任意 JSON", category, "JsonNode.Parse;显式把 JSON 文本解析为对象、数组、数字、布尔、文本或 null。结构化输出连接具体类型前使用类型转换。", [S("value", "JSON 文本", "{}")], [Out(WorkflowValueType.Any)], (x, _) => Value(JsonNode.Parse(Str(x)))); Add(nodes, "json.stringify", "格式化 JSON 文本", category, "序列化为 JSON,indent 可切换紧凑与缩进形式,完整显示中文与表情,字符串引号和控制字符按 JSON 规则转义。", [V(), B("indent", "缩进", true)], [Out(WorkflowValueType.String)], (x, _) => Value(x["value"].Display(Flag(x, "indent")))); } private static string Key(JsonNode? node) => node is JsonValue scalar && scalar.TryGetValue(out var text) ? text : throw new ArgumentException("字段名必须为文本"); }