Skip to content

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;

Renders (in colour):

{
  "name": "Vincent",
  "skills": ["Delphi", "Pascal"],
  "active": true
}

with keys in one colour, strings in another, numbers/booleans/null in a third, etc.

Configuration

MethodPurpose
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)));

Composition

Wrap in a panel for context:

pascal
AnsiConsole.Write(
  Widgets.Panel(Widgets.Json(payload))
    .WithHeader('Response'));

API reference

See also

Released under the MIT License.