JSON Syntax Highlighting

The AnsiConsoleJsonExtensions class provides extension methods for writing JSON content to the console with syntax highlighting.

Methods

WriteJsonAsync (Stream, default styles)

Writes JSON content from a stream to the console with default styling asynchronously.

Task<string> WriteJsonAsync(this IAnsiConsole ansiConsole, Stream stream)

Parameters:

Returns: The JSON content as a string.

WriteJsonAsync (Stream, custom styles)

Writes JSON content from a stream to the console with custom styling asynchronously.

Task<string> WriteJsonAsync(this IAnsiConsole ansiConsole, Stream stream, JsonStyles jsonStyles)

Parameters:

Returns: The JSON content as a string.

WriteJson (Stream, default styles)

Writes JSON content from a stream to the console with default styling synchronously.

string WriteJson(this IAnsiConsole ansiConsole, Stream stream)

Parameters:

Returns: The JSON content as a string.

WriteJson (Stream, custom styles)

Writes JSON content from a stream to the console with custom styling synchronously.

string WriteJson(this IAnsiConsole ansiConsole, Stream stream, JsonStyles jsonStyles)

Parameters:

Returns: The JSON content as a string.

WriteJson (string, default styles)

Writes JSON content from a string to the console with default styling.

void WriteJson(this IAnsiConsole ansiConsole, string value)

Parameters:

WriteJson (string, custom styles)

Writes JSON content from a string to the console with custom styling.

void WriteJson(this IAnsiConsole ansiConsole, string value, JsonStyles jsonStyles)

Parameters:

Example Usage

Basic Usage with String

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;

var jsonString = """
    {
        "glossary": {
            "title": "example glossary",
            "GlossDiv": {
                "title": "S",
                "GlossList": {
                    "GlossEntry": {
                        "ID": "SGML",
                        "SortAs": "SGML",
                        "GlossTerm": "Standard Generalized Markup Language",
                        "Acronym": "SGML",
                        "Abbrev": "ISO 8879:1986",
                        "GlossDef": {
                            "para": "A meta-markup language, used to create markup languages such as DocBook.",
                            "GlossSeeAlso": ["GML", "XML"]
                        },
                        "GlossSee": "markup"
                    }
                }
            }
        }
    }
    """;

AnsiConsole.Console.WriteJson(jsonString);

Custom Styles

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;
using NTokenizers.Extensions.Spectre.Console.Styles;

var customJsonStyles = JsonStyles.Default;
customJsonStyles.PropertyName = new Style(Color.Orange1);

AnsiConsole.Console.WriteJson(jsonString, customJsonStyles);

Async with Stream

using Spectre.Console;
using NTokenizers.Extensions.Spectre.Console;

await AnsiConsole.Console.WriteJsonAsync(stream);