1

Is it possible to make Newtonsoft.JSON to chose a class not based on $type property? For example, I want to deserialize the following JSON

{
  "widgets": [
    "circle": {
      "radius": 1
    },
    "rectangle": {
      "width": 1,
      "height": 2
    }
  ]
}

into the following class hierarchy

class WidgetList {
  IList<Widget> Widgets { get; set; }
}

abstract class Widget {}

class Circle : Widget {
  int Radius { get; set; }
}

class Rectangle : Widget {
  int Width { get; set; }
  int Height { get; set; }
}
synapse
  • 5,588
  • 6
  • 35
  • 65
  • Yes, it is possible in general, but not with the JSON you've provided, which is invalid. The typical approach involves the use of a custom JsonConverter. – StriplingWarrior Jun 05 '17 at 16:11

0 Answers0