Code
·
35 lines
·
1152 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System.Reflection;
using System.Text.Json.Serialization;
using System.Text.Json;
namespace NetproxySolution.Web.Converters;
public class DetailExceptionConverter : JsonConverter<Exception>
{
public override Exception? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return new NotImplementedException();
}
public override void Write(Utf8JsonWriter writer, Exception value, JsonSerializerOptions options)
{
writer.WriteStartObject();
var exceptionType = value.GetType();
writer.WriteString("ClassName", exceptionType.FullName);
var properties = exceptionType.GetProperties()
.Where(e => e.PropertyType != typeof(Type))
.Where(e => e.PropertyType.Namespace != typeof(MemberInfo).Namespace)
.ToList();
foreach (var property in properties)
{
var propertyValue = property.GetValue(value, null);
if (options.DefaultIgnoreCondition == JsonIgnoreCondition.WhenWritingNull && propertyValue == null)
continue;
writer.WritePropertyName(property.Name);
JsonSerializer.Serialize(writer, propertyValue, property.PropertyType, options);
}
writer.WriteEndObject();
}
}