Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,62 @@ namespace MudExtensions;
public partial class MudJsonTreeView : MudComponentBase
{
private string? _json;
private JsonNode? _root;

/// <summary>
/// Gets or sets the JSON to be displayed.
/// The JSON to be displayed.
/// </summary>
/// <remarks>
/// Use the <see cref="Root"/> parameter instead if you have a <see cref="JsonNode"/> available.
/// </remarks>
[Parameter]
[EditorRequired]
public string? Json
{
get => _json;
set => SetJson(value);
}

/// <summary>
/// The root node of the JSON to display.
/// </summary>
/// <remarks>
/// Use the <see cref="Json"/> parameter instead if you only have JSON available as a string.
/// </remarks>
[Parameter]
public JsonNode? Root
{
get => _root;
set => SetJson(value);
}

/// <summary>
/// Sets the <see cref="Json"/> property and raises the <see cref="OnJsonChanged"/> event.
/// </summary>
/// <param name="json">The new JSON to use.</param>
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();
}

/// <summary>
/// Occurs when the JSON has changed.
/// Sets the <see cref="Json"/> property and raises the <see cref="OnJsonChanged"/> event.
/// </summary>
public EventCallback<string> OnJsonChanged { get; set; }
/// <param name="json">The new JSON to use.</param>
protected void SetJson(JsonNode? json)
{
_json = json?.ToJsonString();
_root = json;
OnJsonChanged.InvokeAsync(Root);
StateHasChanged();
}

/// <summary>
/// Gets or sets the root node of the JSON to display.
/// Occurs when the JSON has changed.
/// </summary>
public JsonNode? Root { get; set; }
public EventCallback<JsonNode> OnJsonChanged { get; set; }

/// <summary>
/// Gets or sets a value indicating whether the tree contents are compacted.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,49 @@ else if (Node is JsonArray)
<MudJsonTreeViewNode Node="@child" Sorted="@Sorted" />
</MudTreeViewItem>
}
}
else if (Node is JsonNode)
{
var valueKind = Node.AsValue().GetValue<JsonElement>().ValueKind;
switch (valueKind)
{
case JsonValueKind.String:
var str = Node.AsValue().GetValue<string>();
@* Could be a date *@
if (DateTime.TryParse(str, out DateTime date))
{
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.DateRange" EndText="@date.ToString()"></MudTreeViewItem>
}
@* Could be a GUID *@
else if (Guid.TryParse(str, out Guid guid))
{
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.Key" EndText="@str.ToUpperInvariant()"></MudTreeViewItem>
}
@* Fall back to string *@
else
{
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.TextSnippet" EndText="@str"></MudTreeViewItem>
}
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<int>(out int intVal))
{
endText = intVal.ToString();
}
else if (jsonVal.TryGetValue<double>(out double doubleVal))
{
endText = doubleVal.ToString();
}
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.Numbers" EndText="@endText"></MudTreeViewItem>
break;
case JsonValueKind.True:
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.CheckBox" EndText="true"></MudTreeViewItem>
break;
case JsonValueKind.False:
<MudTreeViewItem T="string" Icon="@Icons.Material.Filled.CheckBoxOutlineBlank" EndText="false"></MudTreeViewItem>
break;
}
}
Loading