0

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);
3
  • 1
    "when I try to implement this in the deserialise part of the program it doesn't work" -- please include this code in your question Commented May 5, 2021 at 13:21
  • @canton7 added in to the bottom Commented May 5, 2021 at 13:25
  • Don't inherit StopEvent:Event that is not correct. Use Event as a property of StopEvent public Event Event{get;set;}. Commented May 5, 2021 at 13:30

3 Answers 3

2

Before your change, your startEvent class had a single property called event, which then contained further data. The corresponding JSON would look something like:

{
    "startEvent":
    {
        "Name": "...",
        "ID": "...",
        ...
    }
}

After your change, your startEvent class now inherits from Event, rather than having Event as a property. This means that the corresponding JSON would look something like:

{
    "Name": "...",
    "ID": "...",
    ...
}

See how you've lost the "startEvent" member?

You need to keep the structure of the objects the same, but you can still introduce a base class which is shared by Event and Event1.

You probably meant to do something like this. I've taken the liberty of renaming your types to be a bit clearer:

// Common properties between all event data types
public abstract class EventDataBase
{
    [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; }
}

// Fields which just the start event data has
public class StartEventData : EventDataBase
{
    [JsonProperty("rfID")]
    public string RfId { get; set; }

    [JsonProperty("startPercentage")]
    public string startPercentage { get; set; }
}

// Start event, which uses the start event data
public class StartEvent
{
    [JsonProperty("event")]
    public StartEventData Event { get; set; }
}

// Fields which just the stop event data has
public class StopEventData : EventDataBase
{
    [JsonProperty("Number")]
    public long Number { get; set; }
}

// Stopevent, which uses the start event data
public class StopEvent
{
    [JsonProperty("event")]
    public StopEventData Event { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You changed the structure of the object, so the JSON does not match up anymore. stopEvent used to have an property Event, but you changed it to inherit from Event, so it no longer has that property - instead it has all the properties directly on itself, which is going to not sync with the JSON anymore.

You can see this if you create an instance of stopEvent yourself and serialize it, then compare to the JSON you are trying to deserialize from. They'll have a different structure.

The fix is either to revert your changes, or to update the incoming JSON to match your new object, if you have control over that.

1 Comment

How would I change the stopEvent so that it does sync with the JSON?
0

In order for considering the inherited properties. You can try adding the "MemberSerialisation" attribute to the sub class

[JsonObject(MemberSerialization = MemberSerialization.Fields)]

1 Comment

MemberSerialization controls whether fields or properties are serialized. It has nothing to do with inheritance at all?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.