Table of Contents

Class ExceptionConverter

Namespace
Cuemon.Extensions.Text.Json.Converters
Assembly
Cuemon.Extensions.Text.Json.dll

Provides an Exception converter that can be configured like the Newtonsoft.JSON equivalent.

public class ExceptionConverter : JsonConverter<Exception>
Inheritance
ExceptionConverter
Inherited Members
Extension Methods

Examples

ExceptionConverter serializes Exception instances to JSON, including optional stack trace and Data dictionary content. This example configures JsonSerializerOptions with WriteIndented = true and adds the converter with includeStackTrace: true and includeData: true, then creates a nested InvalidOperationException("Outer operation failed.") with an inner InvalidOperationException("Inner operation failed.") and a CorrelationId data entry. The resulting JSON includes top-level fields (Type, Source, Message, Stack, Data) and a nested Inner section for the inner exception with its own Type and Message. Console output displays the complete JSON structure.

using System;
using System.Text.Json;
using Cuemon.Extensions.Text.Json.Converters;

namespace MyApp.Examples;

public class ExceptionConverterExample
{
    public void Demonstrate()
    {
        var options = new JsonSerializerOptions
        {
            WriteIndented = true
        };

        // Include both stack trace and exception data
        options.Converters.Add(new ExceptionConverter(includeStackTrace: true, includeData: true));

        var inner = new InvalidOperationException("Inner operation failed.");
        var ex = new InvalidOperationException("Outer operation failed.", inner);
        ex.Data["CorrelationId"] = "abc-123";

        string json = JsonSerializer.Serialize(ex, options);
        Console.WriteLine(json);
        // {
        //   "Type": "System.InvalidOperationException",
        //   "Source": "...",
        //   "Message": "Outer operation failed.",
        //   "Stack": [ "   at ..." ],
        //   "Data": { "CorrelationId": "abc-123" },
        //   "Inner": {
        //     "Type": "System.InvalidOperationException",
        //     "Message": "Inner operation failed."
        //   }
        // }

}
}

Constructors

ExceptionConverter(bool, bool)

Initializes a new instance of the ExceptionConverter class.

public ExceptionConverter(bool includeStackTrace = false, bool includeData = false)

Parameters

includeStackTrace bool

A value that indicates if the stack of an exception is included in the converted result.

includeData bool

A value that indicates if the data of an exception is included in the converted result.

Properties

IncludeData

Gets a value indicating whether the data of an exception is included in the converted result.

public bool IncludeData { get; }

Property Value

bool

true if the data of an exception is included in the converted result; otherwise, false.

IncludeStackTrace

Gets a value indicating whether the stack of an exception is included in the converted result.

public bool IncludeStackTrace { get; }

Property Value

bool

true if the stack of an exception is included in the converted result; otherwise, false.

Methods

CanConvert(Type)

Determines whether this instance can convert the specified object type.

public override bool CanConvert(Type typeToConvert)

Parameters

typeToConvert Type

Type of the object.

Returns

bool

true if this instance can convert the specified object type; otherwise, false.

Read(ref Utf8JsonReader, Type, JsonSerializerOptions)

Reads and converts the JSON to type Exception.

public override Exception Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

Parameters

reader Utf8JsonReader

The Utf8JsonReader to read from.

typeToConvert Type

The Type being converted.

options JsonSerializerOptions

The JsonSerializerOptions being used.

Returns

Exception

The value that was converted.

Write(Utf8JsonWriter, Exception, JsonSerializerOptions)

Writes the value as JSON.

public override void Write(Utf8JsonWriter writer, Exception value, JsonSerializerOptions options)

Parameters

writer Utf8JsonWriter

The Utf8JsonWriter to write to.

value Exception

The value to convert.

options JsonSerializerOptions

The JsonSerializerOptions being used.

See Also