classes deriving from System.Exception do not serialize/deserialize properly
See original GitHub issue#In Newtonsoft.Json v11.0.1, without the [Serializable] attribute on MyExceptionClass, the following test fails. Adding it succeeds.
If I run this test with Newtonsoft.Json v10.0.3 it passes without the [Serializable]
//[Serializable]
public class MyExceptionClass : Exception
{
}
[Test]
public void SerializeException()
{
Exception exception;
try
{
throw new MyExceptionClass();
}
catch (Exception ex)
{
exception = ex;
}
string sex = JsonConvert.SerializeObject(exception);
var dex = JsonConvert.DeserializeObject<Exception>(sex);
Assert.AreEqual(dex.ToString(), exception.ToString());
}
The error generated is:
System.Runtime.Serialization.SerializationException : Member 'ClassName' was not found.
at System.Runtime.Serialization.SerializationInfo.GetElement(String name, Type& foundType)
at System.Runtime.Serialization.SerializationInfo.GetString(String name)
at System.Exception..ctor(SerializationInfo info, StreamingContext context)
at Void .ctor(System.Runtime.Serialization.SerializationInfo, System.Runtime.Serialization.StreamingContext)(Object[] )
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateISerializable(JsonReader reader, JsonISerializableContract contract, JsonProperty member, String id)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
at Connexion.Core.Internal.SerializationEx.Deserialize[T](String value) in D:\Development\Bitbucket\connexion\Connexion.Core\Internal\Json\SerializationEx.cs:line 125
at Connexion.Share.Test.SerializationTests.SerializeException() in D:\Development\Bitbucket\connexion\Connexion.Share.Test\SerializationTests.cs:line 324
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:10 (2 by maintainers)
Top Results From Across the Web
C# Exception not serializing/deserializing correctly
It looks like the object is getting serialized incorrectly, so it won't deserialize into the Exception object.
Read more >How to serialize properties of derived classes with System. ...
In this article, you will learn how to serialize properties of derived classes with the System.Text.Json namespace.
Read more >How to Serialize Exceptions as JSON in .NET
Demonstrate how to serialize exceptions in .NET with elaborate explanation, guidelines and practical examples.
Read more >Migrate from Newtonsoft.Json to System.Text.Json - .NET
The System.Text.Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON).
Read more >Serialization - Archive Exceptions
An attempt has been made to serialize a polymorphic class through a pointer without either registering it or associating it with an export...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
That’s expected behavior. Json.NET previous wasn’t serializing
ISerializable
types correctly. TheSerializableAttribute
is required.See here for more info https://github.com/dotnet/corefx/issues/23415
What happens if you don’t have control over the class? For example, .Net Core 2.2 app new
System.Exception
serializes as:Yet my WebAPI (Net 4.72) using throws the error
System.Runtime.Serialization.SerializationException: 'Member 'ClassName' was not found.'
.Serialize a new
System.Exception
there produces:4.7 has
System.Exception
defined inAssembly = {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089}
where core defines inAssembly = {System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e}
There is no end user control over this.