.NET Exception serialization no longer working without legacy pattern, Newtonsoft.Json – intentional?

This question refers to code which still uses Newtonsoft.Json. Following tests also made use of System.Text.Json.

I finally wanted to remove the obsolete pattern, with [SerializableAttribute], protected ctor and GetObjectData(SerializationInfo, StreamingContext) from my exceptions.

Now serializers, both System.Text.Json.JsonSerializer and Newtonsoft.Json.JsonConvert don’t deserialize the exception properly:

although not relying on old IFormatter (really not?), [edit] Newtonsoft.Json serialized and deserialized exceptions implementing the old pattern. Now, deserializing not even reproduces the original exception message, but a new message is created when deserializing SomeIdNotFoundException:

Exception of type ‘….SomeIdNotFoundException’ was thrown.

I see that exception classes, both custom and common .NET types, including the basic System.Exception, don’t fit the usual requirements for (de)serialization, by having private/protected and read-only members, but is it intentional to lose all exception content, unless using special configurations?

Edit: Example

With legacy serialization

using System;
using System.Runtime.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[Serializable]
public class SomeIdNotFoundException : Exception
{
    public SomeIdNotFoundException()
    { }

    public SomeIdNotFoundException(string message)
        : base(message)
    { }

    public SomeIdNotFoundException(string message, Exception innerException)
        : base(message, innerException)
    { }

    protected SomeIdNotFoundException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    { }

    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
    }
}

[TestClass]
public class ExceptionSerializationTests
{
    [TestMethod]
    public void SomeIdNotFoundException_Serialization_RoundTrip()
    {
        var expectedMessage = "Some ID not found.";
        var originalException = new SomeIdNotFoundException(expectedMessage);
        //var originalException = Assert.ThrowsException<SomeIdNotFoundException>(
        //    () => throw new SomeIdNotFoundException(expectedMessage));

        var deserializedException = SerializeAndDeserialize(originalException);

        Assert.AreEqual(expectedMessage, deserializedException.Message);
    }

    private static SomeIdNotFoundException SerializeAndDeserialize(SomeIdNotFoundException exceptionToSerialize)
    {
        //var serializedException = System.Text.Json.JsonSerializer.Serialize(exceptionToSerialize);
        //return System.Text.Json.JsonSerializer.Deserialize<SomeIdNotFoundException>(serializedException)!;
        var serializedException = Newtonsoft.Json.JsonConvert.SerializeObject(exceptionToSerialize);
        return Newtonsoft.Json.JsonConvert.DeserializeObject<SomeIdNotFoundException>(serializedException)!;
    }

    public static void Main(string[] args)
    {
        new ExceptionSerializationTests()
            .SomeIdNotFoundException_Serialization_RoundTrip();
    }
}

Without legacy serialization

Exception class only.

// no Serializable attribute
public class SomeIdNotFoundException : Exception
{
    public SomeIdNotFoundException()
    { }

    public SomeIdNotFoundException(string message)
        : base(message)
    { }

    public SomeIdNotFoundException(string message, Exception innerException)
        : base(message, innerException)
    { }
}

Direct exception instantiation (no throw)

With legacy pattern disabled

Deserialization with both, System and Newtonsoft, serializers generates a new exception message of this kind:

Exception of type ‘[namespace].SomeIdNotFoundException’ was thrown.

With legacy pattern still available

  • System.Text.Json behaves like without legacy pattern, creating a deserialized exception with a new exception message.
  • Newtonsoft.Json performs a full deserialization of the method, preserving the original message.

Thrown exception

Throwing the exception, like in the commented-out part with Assert.ThrowsException<SomeIdNotFoundException>(...). Then, deserialization never works with System.Text.Json, no matter if the legacy pattern is implemented.

Error messages for thrown exception with System.Text.Json:

System.NotSupportedException: Serialization and deserialization of ‘System.Reflection.MethodBase’ instances is not supported. Path: $.TargetSite. —>
System.NotSupportedException: Serialization and deserialization of ‘System.Reflection.MethodBase’ instances is not supported.

Newtonsoft.Json has no problem with thrown exceptions, as long as the legacy pattern is available.

16

Answering my own question:

  • Newtonsoft.Json supports the legacy serialization pattern for exceptions.
  • System.Text.Json never supported exception serialization by default.

Without the legacy pattern, or specific coding, none of these JSON serializations support exception serialization.

See also links from @dbc in the comments under the question!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

.NET Exception serialization no longer working without legacy pattern, Newtonsoft.Json – intentional?

This question refers to code which still uses Newtonsoft.Json. Following tests also made use of System.Text.Json.

I finally wanted to remove the obsolete pattern, with [SerializableAttribute], protected ctor and GetObjectData(SerializationInfo, StreamingContext) from my exceptions.

Now serializers, both System.Text.Json.JsonSerializer and Newtonsoft.Json.JsonConvert don’t deserialize the exception properly:

although not relying on old IFormatter (really not?), [edit] Newtonsoft.Json serialized and deserialized exceptions implementing the old pattern. Now, deserializing not even reproduces the original exception message, but a new message is created when deserializing SomeIdNotFoundException:

Exception of type ‘….SomeIdNotFoundException’ was thrown.

I see that exception classes, both custom and common .NET types, including the basic System.Exception, don’t fit the usual requirements for (de)serialization, by having private/protected and read-only members, but is it intentional to lose all exception content, unless using special configurations?

Edit: Example

With legacy serialization

using System;
using System.Runtime.Serialization;
using Microsoft.VisualStudio.TestTools.UnitTesting;

[Serializable]
public class SomeIdNotFoundException : Exception
{
    public SomeIdNotFoundException()
    { }

    public SomeIdNotFoundException(string message)
        : base(message)
    { }

    public SomeIdNotFoundException(string message, Exception innerException)
        : base(message, innerException)
    { }

    protected SomeIdNotFoundException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    { }

    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
    }
}

[TestClass]
public class ExceptionSerializationTests
{
    [TestMethod]
    public void SomeIdNotFoundException_Serialization_RoundTrip()
    {
        var expectedMessage = "Some ID not found.";
        var originalException = new SomeIdNotFoundException(expectedMessage);
        //var originalException = Assert.ThrowsException<SomeIdNotFoundException>(
        //    () => throw new SomeIdNotFoundException(expectedMessage));

        var deserializedException = SerializeAndDeserialize(originalException);

        Assert.AreEqual(expectedMessage, deserializedException.Message);
    }

    private static SomeIdNotFoundException SerializeAndDeserialize(SomeIdNotFoundException exceptionToSerialize)
    {
        //var serializedException = System.Text.Json.JsonSerializer.Serialize(exceptionToSerialize);
        //return System.Text.Json.JsonSerializer.Deserialize<SomeIdNotFoundException>(serializedException)!;
        var serializedException = Newtonsoft.Json.JsonConvert.SerializeObject(exceptionToSerialize);
        return Newtonsoft.Json.JsonConvert.DeserializeObject<SomeIdNotFoundException>(serializedException)!;
    }

    public static void Main(string[] args)
    {
        new ExceptionSerializationTests()
            .SomeIdNotFoundException_Serialization_RoundTrip();
    }
}

Without legacy serialization

Exception class only.

// no Serializable attribute
public class SomeIdNotFoundException : Exception
{
    public SomeIdNotFoundException()
    { }

    public SomeIdNotFoundException(string message)
        : base(message)
    { }

    public SomeIdNotFoundException(string message, Exception innerException)
        : base(message, innerException)
    { }
}

Direct exception instantiation (no throw)

With legacy pattern disabled

Deserialization with both, System and Newtonsoft, serializers generates a new exception message of this kind:

Exception of type ‘[namespace].SomeIdNotFoundException’ was thrown.

With legacy pattern still available

  • System.Text.Json behaves like without legacy pattern, creating a deserialized exception with a new exception message.
  • Newtonsoft.Json performs a full deserialization of the method, preserving the original message.

Thrown exception

Throwing the exception, like in the commented-out part with Assert.ThrowsException<SomeIdNotFoundException>(...). Then, deserialization never works with System.Text.Json, no matter if the legacy pattern is implemented.

Error messages for thrown exception with System.Text.Json:

System.NotSupportedException: Serialization and deserialization of ‘System.Reflection.MethodBase’ instances is not supported. Path: $.TargetSite. —>
System.NotSupportedException: Serialization and deserialization of ‘System.Reflection.MethodBase’ instances is not supported.

Newtonsoft.Json has no problem with thrown exceptions, as long as the legacy pattern is available.

16

Answering my own question:

  • Newtonsoft.Json supports the legacy serialization pattern for exceptions.
  • System.Text.Json never supported exception serialization by default.

Without the legacy pattern, or specific coding, none of these JSON serializations support exception serialization.

See also links from @dbc in the comments under the question!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật