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
-
JsonConverterExceptionConverter
- Inherited Members
-
JsonConverter.CanReadJsonConverter.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
includeStackTraceboolA value that indicates if the stack of an exception is included in the converted result.
includeDataboolA 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
trueif 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
trueif 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
objectTypeTypeType of the object.
Returns
- bool
trueif 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
readerJsonReaderThe Newtonsoft.Json.JsonReader to read from.
objectTypeTypeType of the object.
existingValueobjectThe existing value of object being read.
serializerJsonSerializerThe 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
writerJsonWriterThe Newtonsoft.Json.JsonWriter to write to.
valueobjectThe value.
serializerJsonSerializerThe calling serializer.