1

I have the following classes:

public partial class ReservationVersion
{
    public ReservationVersion()
    {
        this.Reservations = new HashSet<Reservation>();
        this.ReservationItems = new HashSet<ReservationItem>();
    }

    public int ReservationVersionID { get; set; }
    public Nullable<int> ReservationID { get; set; }
    public string GUID { get; set; }
    public string StartDate { get; set; }
    public string StartTime { get; set; }
    public string Duration { get; set; }    

    public virtual ICollection<Reservation> Reservations { get; set; }
    public virtual Reservation Reservation { get; set; }
    public virtual ICollection<ReservationItem> ReservationItems { get; set; }

}


public abstract partial class ReservationItem
{
    public ReservationItem()
    {
    }

    public int ReservationItemID { get; set; }
    public Nullable<int> ReservationVersionID { get; set; }
    public string GUID { get; set; }
    public Nullable<int> ParentID { get; set; }
    public Nullable<bool> NeedsConfirmation { get; set; }
    public string ReservationSummary { get; set; }
    public string Comment { get; set; }
    public Nullable<int> ObjectStateID { get; set; }
    public string StartDate { get; set; }
    public string StartTime { get; set; }
    public string Duration { get; set; }

    public virtual ReservationVersion ReservationVersion { get; set; }
}

public abstract partial class Appointment : ReservationItem
{
    public Appointment()
    {
    }

    public string AppointmentDescription { get; set; }
}

public partial class AppointedActivity : Appointment
{
    public Nullable<int> AppointedActivityID { get; set; }

    public virtual Activity Activity { get; set; }
}

public partial class AppointedDevice : Appointment
{
    public Nullable<int> AppointedDeviceID { get; set; }

    public virtual Device Device { get; set; }
}

I'm using Entity Framework and I'm having trouble understanding why I can't correctly parse a complex object from a JSON post.

Here's the JSON being sent (which is a valid ReservationVersion):

{
    "ObjectStateID": 1,
    "StartDate": "2017-10-31",
    //Other props
    "ReservationItems": [
      {
        "ReservationSummary": "Act",
        "AppointedActivityID": 95874,
        "AppointmentDescription": "ACT"
      },
      {
        "ReservationSummary": "Device",
        "AppointedDeviceID": 99040,
        "AppointmentDescription": "DEV"
      }
    ]
}

I can correctly read the ReservationVersion but the collection of ReservationItems is empty every time.

My guess is that there's a problem with ReservationItems being an abstract class and the items I'm sending should correspond to AppointedSomething, which inherits from Appointment, which inherits from ReservationItems

Any help would be much appreciated

BravoZulu
  • 1,140
  • 11
  • 24
  • You are correct in your last paragraph. What serializer are you using? Maybe you can add 'hints' to the serializer on which derived types to use for reservationItems. – SpruceMoose Oct 31 '17 at 21:25
  • How would I do that? The thing is I actually need different types of `ReservationItems`. There could be an `AppointedDevice` and an `AppointedActivity` in the same Enumerable – BravoZulu Nov 01 '17 at 13:02
  • Well, if you are in control of the json data and using JSON.Net library, [this post](https://stackoverflow.com/questions/6348215/how-to-deserialize-json-into-ienumerablebasetype-with-newtonsoft-json-net) looks like it may offer a solution. If not, sorry for distracting you from your true purpose! – SpruceMoose Nov 01 '17 at 13:11
  • The serialization happens on the controller: `public ReservationVersion Post([FromBody]ReservationVersion item)` Do I have control over that? – BravoZulu Nov 01 '17 at 13:17
  • Looks like it - see [How to set custom JsonSerializerSettings for Json.NET in MVC 4 Web API?](https://stackoverflow.com/questions/13274625/how-to-set-custom-jsonserializersettings-for-json-net-in-mvc-4-web-api). By the way - is this a WebAPI project? If so, it might be worth adding that tag to your question. – SpruceMoose Nov 01 '17 at 13:20
  • Thanks, I'll check that out – BravoZulu Nov 01 '17 at 13:24

0 Answers0