Not really sure how to phrase the title sorry.
I have two classes I generated from a JSON to c# online converter. These classes are so I can deserialise some JSON objects and turn them into objects I can use elsewhere in my program. The converter I used gave me these two classes:
namespace testing
{
public partial class stopEvent
{
[JsonProperty("event")]
public Event1 Event { get; set; }
}
public partial class Event1
{
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("ID")]
public string Id { get; set; }
[JsonProperty("sessID")]
public long SessId { get; set; }
[JsonProperty("startTime")]
public DateTime StartTime { get; set; }
[JsonProperty("endTime")]
public DateTime EndTime { get; set; }
[JsonProperty("Number")]
public string Number { get; set; }
[JsonProperty("Percentage")]
public string Percentage { get; set; }
}
}
This is my second object which represents a starting event.
using System;
using Newtonsoft.Json;
namespace testing
{
public partial class startEvent
{
[JsonProperty("event")]
public Event Event { get; set; }
}
public partial class Event
{
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("ID")]
public string Id { get; set; }
[JsonProperty("sessID")]
public long SessId { get; set; }
[JsonProperty("startTime")]
public DateTime StartTime { get; set; }
[JsonProperty("Number")]
public long Number { get; set; }
[JsonProperty("rfID")]
public string RfId { get; set; }
[JsonProperty("startPercentage")]
public string startPercentage { get; set; }
}
}
When I use these in my code and deserialise the json and convert it into objects, it works fine. I had to rename the Event from the second class to Event1 as otherwise it would have the same name as the first partial class.
However, I noticed there were quite a few repeated fields in both classes, so I thought I could make a super class and then both can inherit from there. This was my super class:
public class Event
{
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("ID")]
public string Id { get; set; }
[JsonProperty("sessID")]
public long SessId { get; set; }
[JsonProperty("startTime")]
public DateTime StartTime { get; set; }
[JsonProperty("Number")]
public long Number { get; set; }
}
I then changed the classes so they looked like this:
public class stopEvent : Event
{
[JsonProperty("Percentage")]
public string Percentage { get; set; }
}
With the same for the startEvent. However, when I try to implement this in the deserialise part of the program it doesn't work - all the values come back as null and I'm not sure why.
EDIT:
Deserialising is as follows:
stopEvent StopEvent = JsonConvert.DeserializeObject<stopEvent>(message);