0

I'm getting following error in deserializing a json object : (Using JSON.NET)

Cannot create an abstract class.
at System.Runtime.Serialization.FormatterServices.nativeGetUninitializedObject(RuntimeType type)
at ReadDataSourceBaseFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString[] )

And exact line giving the error is:

return _serializer.ReadObject(stream); // _serializer is DataContractJsonSerializer

I can not reproduce this issue in my local machine, or in the stage machine in hosted environment. JSON is deserialized perfectly

However the issue is reproducible consistently in the QA and production environment. I've tried with

  • QA database
  • QA code in release mode, including all DLLs
  • QA web configs

But still can't produce. JSON object is stored in database and I'm trying to serialize it into classes.

What else I can do to reproduce the issue?

EDIT

Code leading up to the error line is:

var json = JObject.Parse(definition);
var foo = json.GetValue("outerRoot").ToString();
var dataDefinition = _serializer.Deserialize(foo);

And Deserialize function is

public returnType Deserialize(string serializedData)
        {
            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(serializedData)))
            {
                return (returnType)_serializer.ReadObject(stream);
            }
        }

So I am using JSON.Net to strip out the outer most node and deserializing what's inside.

dbc
  • 104,963
  • 20
  • 228
  • 340
Imran
  • 656
  • 1
  • 7
  • 13
  • 1
    Doesn't look like NewtonSoft – hoang Mar 23 '16 at 14:12
  • 2
    Are you saying that the objects are being serialized with JSON.NET, and deserialized with DataContractJsonSerializer? If so, that's likely your problem. Also see http://stackoverflow.com/questions/5824696/datacontractjsonserializer-doesnt-work-with-formatted-json – Ian Kemp Mar 23 '16 at 14:13
  • 1
    Following @IanKemp's suggestion and question, http://stackoverflow.com/questions/5824696/datacontractjsonserializer-doesnt-work-with-formatted-json, try generating the inner json without formatting: `var foo = json.GetValue("outerRoot").ToString(Formatting.None);` – dbc Mar 24 '16 at 04:20

3 Answers3

1

When I encounter something that only happens on PROD I will set the application to run on Local IIS instead of IIS Express. This requires that you have IIS installed on your development machine, however this allows you to debug locally and hit the site from a remote computer to cause the error.

Joe
  • 37
  • 4
  • Thanks. I have IIS installed locally, and running against latest copy of QA code (where the error is reproducible) and code in release mode, using same web configs with only modification to connection string. – Imran Mar 23 '16 at 16:18
0

In your code where you instantiate the DataContractJsonSerializer, you will be specifying the deserializing type. The code will look something like this:

_serializer = new DataContractJsonSerializer(typeof(MyType));

It appears that the type that you have specified for deserialization has the abstract keyword.

public abstract class MyType

In C# you can't instatiate abstract classes, only non-abstract classes, i.e. classes that inherit from them.

Glen Thomas
  • 10,190
  • 5
  • 33
  • 65
0

This was Framework 4.0 issue, and fixed in Framework 4.5 WCF JSON Deserialization Issues with Whitespace

Solution was to upgrade Framework. Or what dbc said in the comments

Community
  • 1
  • 1
Imran
  • 656
  • 1
  • 7
  • 13