JsonText
IJsonText renders a JSON string with consistent indentation and syntax-highlighting for keys, strings, numbers, booleans, and null.
When to use
- API response viewers in CLIs.
- Config dump commands.
- Inspecting structured logs.
Basic usage
pascal
var
jsonText : IJsonText;
begin
jsonText := Widgets.Json(
'{"name":"Vincent","skills":["Delphi","Pascal"],"active":true}');
AnsiConsole.Write(jsonText);
end;1
2
3
4
5
6
7
2
3
4
5
6
7
Renders (in colour):
{
"name": "Vincent",
"skills": ["Delphi", "Pascal"],
"active": true
}1
2
3
4
5
2
3
4
5
with keys in one colour, strings in another, numbers/booleans/null in a third, etc.
Configuration
| Method | Purpose |
|---|---|
WithBracesStyle(value) | Style for {, }, [, ]. |
WithMemberStyle(value) | Style for object keys (the strings before :). |
WithStringStyle(value) | Style for string values. |
WithNumberStyle(value) | Style for numeric literals. |
WithBooleanStyle(value) | Style for true / false. |
WithNullStyle(value) | Style for null. |
WithCommaStyle(value) | Style for ,. |
pascal
AnsiConsole.Write(
Widgets.Json(payload)
.WithMemberStyle(TAnsiStyle.Plain.WithForeground(TAnsiColor.Aqua))
.WithStringStyle(TAnsiStyle.Plain.WithForeground(TAnsiColor.Lime)));1
2
3
4
2
3
4
Composition
Wrap in a panel for context:
pascal
AnsiConsole.Write(
Widgets.Panel(Widgets.Json(payload))
.WithHeader('Response'));1
2
3
2
3
API reference
Widgets.Json(source)IJsonText— interface.- Demo:
demos/snippets/Json.
See also
- Styles reference — building
TAnsiStylevalues.