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
}
CodePudding user response:
Try to use Json opt-in serialization attribute and specify with JsonProperty properties that should be serialized.
[JsonObject(MemberSerialization.OptIn)]
public class BaseException : Exception
{
[JsonProperty]
public bool IsSuccess { get; set; }
[JsonProperty]
public int StatusCode { get; set; }
[JsonProperty]
public string Description { get; set; }
[JsonProperty]
public string Messages { get; set; }
.... another properties
}
test
var ex= new BaseException(404,"message","description");
JsonConvert.SerializeObject(ex, Newtonsoft.Json.Formatting.Indented);
result
{
"IsSuccess": false,
"StatusCode": 404,
"Description": "description",
"Messages": "message"
}
CodePudding user response:
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 = ... });
