diff --git a/CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeView.razor.cs b/CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeView.razor.cs index 7d0b7b6d..b106b321 100644 --- a/CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeView.razor.cs +++ b/CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeView.razor.cs @@ -12,18 +12,34 @@ namespace MudExtensions; public partial class MudJsonTreeView : MudComponentBase { private string? _json; + private JsonNode? _root; /// - /// Gets or sets the JSON to be displayed. + /// The JSON to be displayed. /// + /// + /// Use the parameter instead if you have a available. + /// [Parameter] - [EditorRequired] public string? Json { get => _json; set => SetJson(value); } + /// + /// The root node of the JSON to display. + /// + /// + /// Use the parameter instead if you only have JSON available as a string. + /// + [Parameter] + public JsonNode? Root + { + get => _root; + set => SetJson(value); + } + /// /// Sets the property and raises the event. /// @@ -31,20 +47,27 @@ public string? Json protected void SetJson(string? json) { _json = json; - Root = string.IsNullOrEmpty(_json) ? null : JsonNode.Parse(_json); - OnJsonChanged.InvokeAsync(_json); + _root = string.IsNullOrEmpty(_json) ? null : JsonNode.Parse(_json); + OnJsonChanged.InvokeAsync(Root); StateHasChanged(); } /// - /// Occurs when the JSON has changed. + /// Sets the property and raises the event. /// - public EventCallback OnJsonChanged { get; set; } + /// The new JSON to use. + protected void SetJson(JsonNode? json) + { + _json = json?.ToJsonString(); + _root = json; + OnJsonChanged.InvokeAsync(Root); + StateHasChanged(); + } /// - /// Gets or sets the root node of the JSON to display. + /// Occurs when the JSON has changed. /// - public JsonNode? Root { get; set; } + public EventCallback OnJsonChanged { get; set; } /// /// Gets or sets a value indicating whether the tree contents are compacted. diff --git a/CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeViewNode.razor b/CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeViewNode.razor index a744e506..37764b7d 100644 --- a/CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeViewNode.razor +++ b/CodeBeam.MudBlazor.Extensions/Components/JsonTreeView/MudJsonTreeViewNode.razor @@ -75,4 +75,49 @@ else if (Node is JsonArray) } +} +else if (Node is JsonNode) +{ + var valueKind = Node.AsValue().GetValue().ValueKind; + switch (valueKind) + { + case JsonValueKind.String: + var str = Node.AsValue().GetValue(); + @* Could be a date *@ + if (DateTime.TryParse(str, out DateTime date)) + { + + } + @* Could be a GUID *@ + else if (Guid.TryParse(str, out Guid guid)) + { + + } + @* Fall back to string *@ + else + { + + } + break; + case JsonValueKind.Number: + JsonValue jsonVal = Node.AsValue(); + string endText = string.Empty; + @* We try for int first, because an int can always be converted to double but not the other way around*@ + if (jsonVal.TryGetValue(out int intVal)) + { + endText = intVal.ToString(); + } + else if (jsonVal.TryGetValue(out double doubleVal)) + { + endText = doubleVal.ToString(); + } + + break; + case JsonValueKind.True: + + break; + case JsonValueKind.False: + + break; + } } \ No newline at end of file