[.Net 5.0] 8.异常的Json序列化
我也是醉了,System.Exception居然不能序列化!
巴拉巴拉一堆英文,意思是说TargetSite.DeclaringType是System.Type类型,无法序列化,真真的扯淡。非要我写个转换器
public class MethodBaseConverter : JsonConverter<MethodBase> { public override MethodBase Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => null; public override void Write(Utf8JsonWriter writer, MethodBase value, JsonSerializerOptions options) => writer.WriteStringValue(value.ToString()); }
然后:
try { throw new Exception("test"); } catch (Exception ex) { var jso = new JsonSerializerOptions(); jso.Converters.Add(new MethodBaseConverter()); var jsonStr = JsonSerializer.Serialize(ex,jso); Console.WriteLine(jsonStr); }
没道理!!!
赞 (0)