question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

7reactions
JamesNKcommented, Feb 23, 2018

That’s expected behavior. Json.NET previous wasn’t serializing ISerializable types correctly. The SerializableAttribute is required.

See here for more info https://github.com/dotnet/corefx/issues/23415

6reactions
alexhiggins732commented, May 28, 2019

What happens if you don’t have control over the class? For example, .Net Core 2.2 app new System.Exception serializes as:

{
  "Message": "Exception of type 'System.Exception' was thrown.",
  "Data": {},
  "InnerException": null,
  "TargetSite": null,
  "StackTrace": null,
  "HelpLink": null,
  "Source": null,
  "HResult": -2146233088
}

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:

{
  "ClassName": "System.Exception",
  "Message": null,
  "Data": null,
  "InnerException": null,
  "HelpURL": null,
  "StackTraceString": null,
  "RemoteStackTraceString": null,
  "RemoteStackIndex": 0,
  "ExceptionMethod": null,
  "HResult": -2146233088,
  "Source": null,
  "WatsonBuckets": null
}

4.7 has System.Exception defined in Assembly = {mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089} where core defines in Assembly = {System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e}

There is no end user control over this.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found