'Is there a way to serialize certain properties that are not property of the base class? using Newtonsoft

I want to serialize this class properties(only IsSuccess,StatusCode,Description,Messages) But Newtonsoft serialized all properties base and BaseException properties

public class BaseException :  Exception
{
    public bool IsSuccess { get; set; }
    public int StatusCode { get; set; }
    public string Description { get; set; }
    public string Messages { get; set; }
    public BaseException(int statusCode, string message, string description)
    {
        StatusCode = statusCode;
        IsSuccess = false;
        Description = description;
        Messages = message;
    }
    public BaseException()
    {

    }

} 

serialize method that i used in exception middleware

    private string ConvertJsonData(Exception e, int statusCode)
    {
        string json="";
        var type = e.GetType();
        if (e.GetType() == typeof(BaseException))
        {
            json = JsonConvert.SerializeObject(new BaseException
            {
                IsSuccess = (bool)type.GetProperty("IsSuccess").GetValue(e),
                StatusCode = statusCode,
                Messages = (string)type.GetProperty("Messages").GetValue(e),
                Description = (string)type.GetProperty("Description").GetValue(e),
                
            });
        }
        return json;
    }

I throw BaseException:

throw new BaseException(404, "Not Found", "Kullanıcı Bulunamadı"); 

json response from controller :

{
  "StatusCode": 404,
  "IsSuccess": false,
  "Messages": "Not Found",
  "Description": "Kullanıcı Bulunamadı",
  "StackTrace": null,
  "Message": "Exception of type 'SharedNote.Application.Exceptions.BaseException' was thrown.",
  "Data": {},
  "InnerException": null,
  "HelpLink": null,
  "Source": null,
  "HResult": -2146233088
}


Solution 1:[1]

You're essentially mixing two different concerns here. An exception is just something that indicates an error and that is ment to be thrown to some error-handler. It does not really have any data despite its message and maybe an error-code or something similar. In particular it won't have something called IsSuccess.

The other thing here is data, which you just pass around, but that itself has no meaning. So you should seperate those two concerns here by either not inheriting Exception in the first place, or by creating some data-exchange-class with those four properties:

class MyExchangeClass // does NOT inherit Exception
{
    public bool IsSuccess { get; set; }
    public int StatusCode { get; set; }
    public string Description { get; set; }
    public string Messages { get; set; }
}

Now it's easy to serialize that one. Just wrap your exception into that exchange-class:

json = JsonConvert.SerializeObject(new MyExchangeClass { IsSuccess = ... });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1