14

C# Exceptions are ISerialisable so they can't also be DataContracts so I can't use JsonDataContractSerializer.

What are alternatives to serialising Exceptions to JSON?

user1919249
  • 327
  • 1
  • 3
  • 10
  • 1
    possible duplicate http://stackoverflow.com/questions/486460/how-to-serialize-an-exception-object-in-c – Mainul Feb 12 '16 at 08:51
  • 1
    [JSON Serialization Using Newtonsoft JSON Serialize](http://www.c-sharpcorner.com/UploadFile/dacca2/json-serialization-using-newtonsoft-json-serialize/) – Khan Abdulrehman Feb 12 '16 at 08:53
  • If you're willing to switch to [tag:json.net], you could use *Solution 2: Embed type information using TypeNameHandling.* from [How to (de)serialize a XmlException with Newtonsoft JSON?](https://stackoverflow.com/questions/35015357/how-to-deserialize-a-xmlexception-with-newtonsoft-json/35023491#35023491). – dbc Feb 12 '16 at 15:19
  • Khan - I was going to stay away from extra libraries but I think I will go with this solution. Mainul - I thought since the that question was 7 years old there might be out of date but I did consider creating a wrapping class. – user1919249 Feb 12 '16 at 22:19

2 Answers2

22

Since this has not really been answered yet: Just create a Dictionary containing the error properties you want, serialize it using JSON.NET and put it into a HttpResponseMessage:

catch (Exception e)
{
    var error = new Dictionary<string, string>
    {
        {"Type", e.GetType().ToString()},
        {"Message", e.Message},
        {"StackTrace", e.StackTrace}
    };

    foreach (DictionaryEntry data in e.Data)
        error.Add(data.Key.ToString(), data.Value.ToString());

    string json = JsonConvert.SerializeObject(error, Formatting.Indented);

    HttpResponseMessage response = new HttpResponseMessage();
    response.Content = new StringContent(json);

    return response;
}

I hope this can help some people out.

Johan Wintgens
  • 529
  • 7
  • 15
4

This is the solution that I use in my projects that has some desired plus (IMHO):

  • Pretty clean JSON format
  • Recursive dumping of the inner exceptions
using System.Text.Json.Serialization;

namespace MyAwesomeroject.Shared.Utils;

public static class ExceptionExtensions
{
    public class ExceptionInfo
    {
        public ExceptionInfo() { }

        internal ExceptionInfo(Exception exception, bool includeInnerException = true, bool includeStackTrace = false)
        {
            if (exception is null)
            {
                throw new ArgumentNullException(nameof(exception));
            }

            Type = exception.GetType().FullName;
            Message = exception.Message;
            Source = exception.Source;
            StackTrace = includeStackTrace ? exception.StackTrace : null;
            if (includeInnerException && exception.InnerException is not null)
            {
                InnerException = new ExceptionInfo(exception.InnerException, includeInnerException, includeStackTrace);
            }
        }

        public string Type { get; set; }
        public string Message { get; set; }
        public string Source { get; set; }
        public string StackTrace { get; set; }
        public ExceptionInfo InnerException { get; set; }
    }

    private static readonly JsonSerializerOptions _defaultJsonSerializerOptions = new()
    {
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
        WriteIndented = true,
    };

    /// <summary>
    /// Serialize the <see cref="Exception"/> to a JSON string.
    /// </summary>
    /// <param name="ex">The exception</param>
    /// <param name="includeInnerException">Control if to include inner exception</param>
    /// <param name="includeStackTrace">Control if to include stack trace</param>
    /// <param name="options">JSON options. By default nulls are not serialized and the string is indented</param>
    /// <returns></returns>
    public static string ToJson(
        this Exception ex,
        bool includeInnerException = true,
        bool includeStackTrace = false,
        JsonSerializerOptions options = null)
    {
        ArgumentNullException.ThrowIfNull(ex);
        var info = new ExceptionInfo(ex, includeInnerException, includeStackTrace);
        
        return JsonSerializer.Serialize(info, options ?? _defaultJsonSerializerOptions);
    }
}

That produces outputs like this:

{
  "Type": "System.InvalidOperationException",
  "Message": "MyMessage",
  "Source": "MySource",
  "InnerException": {
    "Type": "System.ArgumentException",
    "Message": "MyInnerMessage",
    "Source": "MyAwesomeProject.Utils.Tests",
    "StackTrace": "   at MyAwesomeProject.Utils.Tests.ExceptionExtensionsTests.ShouldInclude_StackTrace_if_required() in /Users/jc/git/MyAwesomeProject/tests/Shared/Utils/ExceptionExtensionsTests.cs:line 41"
  }
}

Lord of the Goo
  • 1,214
  • 15
  • 31