Table of Contents

Class ExceptionConverter

Namespace
Codebelt.Extensions.Newtonsoft.Json.Converters
Assembly
Codebelt.Extensions.Newtonsoft.Json.dll

Converts an Exception to or from JSON.

public class ExceptionConverter : JsonConverter
Inheritance
JsonConverter
ExceptionConverter
Inherited Members
JsonConverter.CanRead
JsonConverter.CanWrite

Examples

Exception details in API responses often need to include stack traces for debugging or be scrubbed for security, while exception serialization must capture inner exceptions and custom data for complete diagnostics. Without specialized handling, exceptions serialize to verbose, implementation-specific output unsuitable for external clients or comprehensive logging. The ExceptionConverter class solves this by providing configurable serialization of exception graphs—including nested inner exceptions, stack traces, and data dictionaries—enabling applications to control which exception details appear in different contexts (internal diagnostics versus client responses). This example demonstrates how to use the ExceptionConverter to serialize and deserialize exceptions with configurable detail levels:

using System;
using Codebelt.Extensions.Newtonsoft.Json.Converters;
using Newtonsoft.Json;

namespace Examples;

class Program
{
    static void Main()
    {
        // Create settings with exception converter
        var settings = new JsonSerializerSettings();
        settings.Converters.Add(new ExceptionConverter(includeStackTrace: true, includeData: true));

        try
        {
            // Simulate an exception with nested inner exception
            throw new InvalidOperationException("Outer exception occurred", 
                new ArgumentException("Inner exception message"));
        }
        catch (Exception ex)
        {
            // Serialize the exception to JSON
            var json = JsonConvert.SerializeObject(ex, settings);
            Console.WriteLine("Serialized Exception:");
            Console.WriteLine(json);
        }
    }
}

The converter produces JSON output that captures the exception type, message, and optionally the stack trace and data dictionary.

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 objectType)

Parameters

objectType Type

Type of the object.

Returns

bool

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

ReadJson(JsonReader, Type, object, JsonSerializer)

Reads the JSON representation of the object.

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)

Parameters

reader JsonReader

The Newtonsoft.Json.JsonReader to read from.

objectType Type

Type of the object.

existingValue object

The existing value of object being read.

serializer JsonSerializer

The calling serializer.

Returns

object

The object value.

WriteJson(JsonWriter, object, JsonSerializer)

Writes the JSON representation of the object.

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)

Parameters

writer JsonWriter

The Newtonsoft.Json.JsonWriter to write to.

value object

The value.

serializer JsonSerializer

The calling serializer.

See Also

JsonConverter