mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-06-18 21:08:15 +08:00
Add JSON text renderer (#1086)
* Add JsonText widget to render highlighted JSON Closes #1051
This commit is contained in:
69
docs/input/widgets/json.md
Normal file
69
docs/input/widgets/json.md
Normal file
@ -0,0 +1,69 @@
|
||||
Title: JSON
|
||||
Order: 70
|
||||
Description: "Use *ImageSharp* to parse images and render them as Ascii art to the console."
|
||||
Reference: T:Spectre.Console.Json.JsonText
|
||||
---
|
||||
|
||||
To add JSON superpowers to
|
||||
your console application to render JSON text, you will need to install
|
||||
the [Spectre.Console.Json](https://www.nuget.org/packages/Spectre.Console.Json) NuGet package.
|
||||
|
||||
```text
|
||||
> dotnet add package Spectre.Console.Json
|
||||
```
|
||||
|
||||
## Loading images
|
||||
|
||||
Once you've added the `Spectre.Console.Json` NuGet package,
|
||||
you can start rendering JSON to the console.
|
||||
|
||||
```csharp
|
||||
using Spectre.Console.Json;
|
||||
|
||||
var json = new JsonText(
|
||||
"""
|
||||
{
|
||||
"hello": 32,
|
||||
"world": {
|
||||
"foo": 21,
|
||||
"bar": 255,
|
||||
"baz": [
|
||||
0.32, 0.33e-32,
|
||||
0.42e32, 0.55e+32,
|
||||
{
|
||||
"hello": "world",
|
||||
"lol": null
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
AnsiConsole.Write(
|
||||
new Panel(json)
|
||||
.Header("Some JSON in a panel")
|
||||
.Collapse()
|
||||
.RoundedBorder()
|
||||
.BorderColor(Color.Yellow));
|
||||
```
|
||||
|
||||
### Result
|
||||
|
||||
<?# AsciiCast cast="json" /?>
|
||||
|
||||
## Styling
|
||||
|
||||
All the different JSON parts can be customized to have unique styles.
|
||||
|
||||
```csharp
|
||||
AnsiConsole.Write(
|
||||
new JsonText(json)
|
||||
.BracesColor(Color.Red)
|
||||
.BracketColor(Color.Green)
|
||||
.ColonColor(Color.Blue)
|
||||
.CommaColor(Color.Red)
|
||||
.StringColor(Color.Green)
|
||||
.NumberColor(Color.Blue)
|
||||
.BooleanColor(Color.Red)
|
||||
.NullColor(Color.Green));
|
||||
```
|
Reference in New Issue
Block a user