11@namespace MudExtensions
22@using System .Text .Json ;
3- @using System .Text .Json .Nodes
3+ @using System .Text .Json .Nodes ;
44
55@if (Node is JsonObject )
66{
7- @* If sorting is enabled, sort by key *@
8- var nodes = Sorted ? Node .AsObject ().OrderBy (x => x .Key ).ToDictionary (x => x .Key , x => x .Value ) : Node .AsObject ().ToDictionary (x => x .Key , x => x .Value );
9- @* Go through each item *@
10- foreach (var child in nodes )
11- {
12- if (child .Value is JsonValue )
13- {
14- @* Get the type of value in this node * @
15- var valueKind = child .Value .AsValue ().GetValue <JsonElement >().ValueKind ;
16- switch (valueKind )
17- {
18- case JsonValueKind .String :
19- var str = child .Value .AsValue ().GetValue <string >();
20- @* Could be a date * @
21- if (DateTime .TryParse (str , out DateTime date ))
22- {
23- < MudTreeViewItem T = " string" Text = " @child.Key" Icon = " @Icons.Material.Filled.DateRange" EndText = " @date.ToString()" >< / MudTreeViewItem >
24- }
25- @* Could be a GUID * @
26- else if (Guid .TryParse (str , out Guid guid ))
27- {
28- < MudTreeViewItem T = " string" Text = " @child.Key" Icon = " @Icons.Material.Filled.Key" EndText = " @str.ToUpperInvariant()" >< / MudTreeViewItem >
29- }
30- @* Fall back to string * @
31- else
32- {
33- < MudTreeViewItem T = " string" Text = " @child.Key" Icon = " @Icons.Material.Filled.TextSnippet" EndText = " @str" >< / MudTreeViewItem >
34- }
35- break ;
36- case JsonValueKind .Number :
37- JsonValue jsonVal = child .Value .AsValue ();
38- string endText = string .Empty ;
39- @* We try for int first, because an int can always be converted to double but not the other way around*@
40- if (jsonVal.TryGetValue<int>(out int intVal ))
41- {
42- endText = intVal .ToString ();
43- }
44- else if (jsonVal .TryGetValue <double >(out double doubleVal ))
45- {
46- endText = doubleVal .ToString ();
47- }
48- < MudTreeViewItem T = " string" Text = " @child.Key" Icon = " @Icons.Material.Filled.Numbers" EndText = " @endText" >< / MudTreeViewItem >
49- break ;
50- case JsonValueKind .True :
51- < MudTreeViewItem T = " string" Text = " @child.Key" Icon = " @Icons.Material.Filled.CheckBox" EndText = " true" >< / MudTreeViewItem >
52- break ;
53- case JsonValueKind .False :
54- < MudTreeViewItem T = " string" Text = " @child.Key" Icon = " @Icons.Material.Filled.CheckBoxOutlineBlank" EndText = " false" >< / MudTreeViewItem >
55- break ;
56- }
57- }
58- else if (child .Value is JsonArray )
59- {
60- <MudTreeViewItem T =" string" IconColor =" Color.Primary" Icon =" @Icons.Material.Filled.Folder" Text =" @child.Key" EndTextClass =" mud-primary-text" EndText =" (Array)" >
61- @* Iterate each array element *@
62- <MudJsonTreeViewNode Node =" @child.Value" Sorted =" @Sorted" />
63- </MudTreeViewItem >
64- }
65- }
66- }
67- else if (Node is JsonArray )
68- {
69- var count = 0 ;
70- @* Iterate each array element *@
71- foreach (var child in Node .AsArray ())
7+ var node = Sorted ? Node .AsObject ().OrderBy (x => x .Key ).ToDictionary (x => x .Key , x => x .Value ) : Node .AsObject ().ToDictionary (x => x .Key , x => x .Value );
8+
9+ foreach (var item in node )
7210 {
73- count ++ ;
74- <MudTreeViewItem T =" string" IconColor =" Color.Primary" Icon =" @Icons.Material.Filled.Folder" EndTextClass =" mud-primary-text" EndText =" @($" (Item {count}) " )" >
75- <MudJsonTreeViewNode Node =" @child" Sorted =" @Sorted" />
76- </MudTreeViewItem >
11+ @RenderJsonItem(item)
7712 }
7813}
7914else if (Node is JsonNode )
8015{
81- var valueKind = Node .AsValue ().GetValue <JsonElement >().ValueKind ;
82- switch (valueKind )
83- {
84- case JsonValueKind .String :
85- var str = Node .AsValue ().GetValue <string >();
86- @* Could be a date * @
87- if (DateTime .TryParse (str , out DateTime date ))
88- {
89- < MudTreeViewItem T = " string" Icon = " @Icons.Material.Filled.DateRange" EndText = " @date.ToString()" >< / MudTreeViewItem >
90- }
91- @* Could be a GUID * @
92- else if (Guid .TryParse (str , out Guid guid ))
93- {
94- < MudTreeViewItem T = " string" Icon = " @Icons.Material.Filled.Key" EndText = " @str.ToUpperInvariant()" >< / MudTreeViewItem >
95- }
96- @* Fall back to string * @
97- else
98- {
99- < MudTreeViewItem T = " string" Icon = " @Icons.Material.Filled.TextSnippet" EndText = " @str" >< / MudTreeViewItem >
100- }
101- break ;
102- case JsonValueKind .Number :
103- JsonValue jsonVal = Node .AsValue ();
104- string endText = string .Empty ;
105- @* We try for int first, because an int can always be converted to double but not the other way around*@
106- if (jsonVal.TryGetValue<int>(out int intVal ))
107- {
108- endText = intVal .ToString ();
109- }
110- else if (jsonVal .TryGetValue <double >(out double doubleVal ))
111- {
112- endText = doubleVal .ToString ();
113- }
114- < MudTreeViewItem T = " string" Icon = " @Icons.Material.Filled.Numbers" EndText = " @endText" >< / MudTreeViewItem >
115- break ;
116- case JsonValueKind .True :
117- < MudTreeViewItem T = " string" Icon = " @Icons.Material.Filled.CheckBox" EndText = " true" >< / MudTreeViewItem >
118- break ;
119- case JsonValueKind .False :
120- < MudTreeViewItem T = " string" Icon = " @Icons.Material.Filled.CheckBoxOutlineBlank" EndText = " false" >< / MudTreeViewItem >
121- break ;
122- }
16+ @RenderJsonItem(Node)
12317}
0 commit comments