Blazor.JsonEditor
1.1.1
dotnet add package Blazor.JsonEditor --version 1.1.1
NuGet\Install-Package Blazor.JsonEditor -Version 1.1.1
<PackageReference Include="Blazor.JsonEditor" Version="1.1.1" />
<PackageVersion Include="Blazor.JsonEditor" Version="1.1.1" />
<PackageReference Include="Blazor.JsonEditor" />
paket add Blazor.JsonEditor --version 1.1.1
#r "nuget: Blazor.JsonEditor, 1.1.1"
#:package Blazor.JsonEditor@1.1.1
#addin nuget:?package=Blazor.JsonEditor&version=1.1.1
#tool nuget:?package=Blazor.JsonEditor&version=1.1.1
Blazor.JsonEditor
Json Editor and Viewer for Blazor Server App and Wasm. Rewrited and working version of Blazoring.JsonEditor
Demo:
Json Editor and Viewer tool
- To install the package run following command:
Install-Package Blazor.JsonEditor
or search Blazor.JsonEditor in Nuget gallery.
This will install Blazor.JsonEditor in your project. You also need to add in _Imports.razor:
@using Blazor.JsonEditor.Component
Also, you need to add javascript file in _Host.cshtml ( Server app) or index.html ( Wasm) file:
<script src="_content/Blazor.JsonEditor/Blazor.JsonEditor.js"></script>
For icons suppor JsonEditor uses Font-Awesome icons library. You need to add link to _Host.cshtml ( Server app) or index.html ( Wasm) file:
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v6.4.2/css/all.css">
Using in code to have an editor:
<EditForm Model="DemoJson">
<JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)"></JsonEditor>
</EditForm>
Using in code to have a viewer:
<EditForm Model="DemoJson">
<JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)" AllowEdit="false"></JsonEditor>
</EditForm>
Blazor.JsonEditor doesn't work without EditForm. Also, validation is required.
Customization
Editor template:
You can customize and pass your own editor template as dynamic component. Editor template is everything that you see after property name and value like buttons and editor window itself. For this you need to set parameter CustomEditor. As an example CustomEditor="typeof(JsonItemCustomEditor)"
<EditForm Model="DemoJson">
<JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)" CustomEditor="typeof(JsonItemCustomEditor)"></JsonEditor>
</EditForm>
In your custom component you need to have parameters:
//Main Json object
[Parameter]
public JsonObject? JsonObject { get; set; }
//Event that you need to invoke on every object change
[Parameter]
public EventCallback<JsonObject> JsonObjectChanged { get; set; }
//Values for dropdown
[Parameter]
public Dictionary<string, string>? KeyValues { get; set; }
//Edit item in case of edit
[Parameter]
public KeyValuePair<string, JsonNode?>? JsonItemToEdit { get; set; }
Also on add and edit you need to pass data to JsonHelper. We have two methods for this: AddNodeValue and EditNodeValue
public static void AddNodeValue(JsonObject jsonObject, JsonItem jsonItem)
public static void EditNodeValue(JsonObject jsonObject, JsonItem jsonItem, string editPropertyName)
After Add/Edit/Delete do not forget to Invoke JsonObjectChanged
await JsonObjectChanged.InvokeAsync(JsonObject);
View templates:
You can customize view template for an item and an object. Item is key: value template. Object view template is an object template that contains a lot of items template in it.
{
"Nullable" : null, //item view template
"String" : "random", //item view template
"Number" : 1, //item view template
"Array" : [1,2,3], //item view template
"True" : true, //item view template
"False" : false, //item view template
"Object" : { //!OBJECT! view template
"Nullable" : null, //item view template
"String" : "random", //item view template
"Number" : 1, //item view template
"Array" : [1,2,3], //item view template
"True" : true, //item view template
"False" : false //item view template
}
}
Item view template:
<EditForm Model="DemoJson">
<JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)" CustomItemView="typeof(JsonItemCustomView)"></JsonEditor>
</EditForm>
In your custom component you need to have parameters:
[Parameter]
public KeyValuePair<string, JsonNode?> JsonItem { get; set; }
You can find example in repository.
Object view template:
<EditForm Model="DemoJson">
<JsonEditor @bind-Value="DemoJson.Json" FieldName="@nameof(IndexModel.Json)" ValidationFor="@(() => DemoJson.Json)" CustomObjectView="typeof(JsonObjectCustomView)"></JsonEditor>
</EditForm>
In your custom component you need to have parameters:
[Parameter]
public KeyValuePair<string, JsonNode?> JsonItem { get; set; }
[Parameter]
public EventCallback<string?> ValueChanged { get; set; }
[Parameter]
public Dictionary<string, string>? KeyValues { get; set; }
[Parameter]
public bool AllowEdit { get; set; } = true;
[Parameter]
public Type? CustomEditor { get; set; }
[Parameter]
public Type? CustomItemView { get; set; }
[Parameter]
public Type? CustomObjectView { get; set; }
Don't forget to include InternalJsonEditor inside you custom template to allow build lower object level.
Example:
<InternalJsonEditor Value="@JsonItem.Value.ToJsonString()"
ValueChanged="ValueChanged"
KeyValues="KeyValues"
AllowEdit="AllowEdit"
CustomEditor="CustomEditor"
CustomItemView="CustomItemView"
CustomObjectView="CustomObjectView">
</InternalJsonEditor>
You can find example in repository.
Next steps
- Refactor
If you like it please support:
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- Microsoft.AspNetCore.Components.Web (>= 7.0.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Item view and object view customization added