Jochen Kühner
Jochen Kühner

Reputation: 1411

Json Net Exception when Deserialized Type does not match Base Type

If have the Abstract type TransportsystemActionDTO, now I serialize a Object and get the following JSON:

  {
  "$id": "1",
  "$type": "MCC.MFCV2.Services.TransportService2Interfaces.Actions.GenerateGlobalTaskActionDTO, MCC.MFCV2.Services.TransportService2Interfaces",
  "Destination": "BA41",
  "Source": "BA11",
  "Description": "BA11->BA41",
  "UseCurrentResource": false,
  "ResourceType": null,
  "Priority": 0,
  "TypeOfTask": 1,
  "NextTransportsystemAction": {
    "$id": "2",
    "$type": "MCC.MFCV2.Services.TransportService2Interfaces.Actions.TriggerGenerateLocalTaskActionDTO, MCC.MFCV2.Services.TransportService2Interfaces",
    "Trigger": true,
    "ReTrigger": true,
    "NextTransportsystemAction": null,
    "ConfiguratorPosition": {
      "X": 110.0,
      "Y": 235.0,
      "Width": 187.42666666666668,
      "Height": 41.0,
      "Z": 0,
      "Layer": 0
    }
  },
  "ConfiguratorPosition": {
    "X": 105.0,
    "Y": 125.0,
    "Width": 192.23666666666682,
    "Height": 62.000000000000014,
    "Z": 0,
    "Layer": 0
  }
}

now the Problem is, when I desrialize with TransportsystemActionDTO as base Type, I got the following exception:

Could not create an instance of type MCC.MFCV2.Services.TransportService2Interfaces.Actions.TransportsystemActionDTO. Type is an interface or abstract class and cannot be instantiated.

It is ok, but I don't know what the base Type is, that can be different Types, but the Type is stored in the $type field, so the Deserialisation should be possible...

Upvotes: 0

Views: 233

Answers (1)

Jochen Kühner
Jochen Kühner

Reputation: 1411

Found my error. I Added TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto to Serialisation, but not to Deserialisation. Now it's working!

Upvotes: 1

Related Questions