1+ using System . Text . Json ;
2+
3+ namespace Diffusion . ComfyUI ;
4+
5+ public class SimpleWorkflowParser : IWorkflowParser
6+ {
7+ public IReadOnlyCollection < Node > Parse ( string workflowId , JsonElement rootElement )
8+ {
9+ var rootProperties = rootElement
10+ . EnumerateObject ( ) . ToDictionary ( n => n . Name , n => n . Value ) ;
11+
12+
13+ var nodes = new List < Node > ( ) ;
14+
15+ foreach ( var element in rootProperties )
16+ {
17+ var node = new Node ( ) ;
18+ node . Id = element . Key ;
19+
20+ foreach ( var props in element . Value . EnumerateObject ( ) )
21+ {
22+ if ( props . Name == "inputs" )
23+ {
24+ node . Inputs = new List < Input > ( ) ;
25+
26+ foreach ( var prop2 in props . Value . EnumerateObject ( ) )
27+ {
28+ var name = prop2 . Name ;
29+
30+ string path = $ "[{ node . Id } ].inputs[\" { prop2 . Name } \" ]";
31+
32+ switch ( prop2 . Value . ValueKind )
33+ {
34+ case JsonValueKind . Undefined :
35+ break ;
36+ case JsonValueKind . Object :
37+ break ;
38+ case JsonValueKind . Array :
39+ break ;
40+ case JsonValueKind . String :
41+ node . Inputs . Add ( new Input ( workflowId , path , name , prop2 . Value . GetString ( ) ) ) ;
42+ break ;
43+ case JsonValueKind . Number :
44+ node . Inputs . Add ( new Input ( workflowId , path , name , prop2 . Value . GetDouble ( ) ) ) ;
45+ break ;
46+ case JsonValueKind . True :
47+ node . Inputs . Add ( new Input ( workflowId , path , name , prop2 . Value . GetBoolean ( ) ) ) ;
48+ break ;
49+ case JsonValueKind . False :
50+ node . Inputs . Add ( new Input ( workflowId , path , name , prop2 . Value . GetBoolean ( ) ) ) ;
51+ break ;
52+ case JsonValueKind . Null :
53+ break ;
54+ default :
55+ throw new ArgumentOutOfRangeException ( ) ;
56+ }
57+ }
58+ }
59+ else if ( props . Name == "class_type" )
60+ {
61+ node . Name = props . Value . GetString ( ) ;
62+ }
63+ }
64+ nodes . Add ( node ) ;
65+ }
66+
67+ return nodes ;
68+ }
69+ }
0 commit comments