Jump to content

C# | Json.NET Deserialization Question

Go to solution Solved by cluelessgenius,
1 hour ago, EricBP said:

I will check tonight

jesus christ took me half a day to figure this out but i finally got it.

basically the serialization order was wrong and i had a property re-initializing the trigger after it was set

[JsonObject]
    public class Trigger : Param
    {
        private long iD;

        private ITrigger trig;

        private TriggerType triggerType;

        [JsonProperty("ID", Order = 1)]
        public long ID
        {
            get
            {
                return iD;
            }

            set
            {
                iD = value;
            }
        }

        [DetailOrder(Position = 2)]
        [JsonProperty("Trig", Order = 3)]
        public ITrigger Trig
        {
            get
            {
                return trig;
            }

            set
            {
                trig = value;
            }
        }

        [DetailOrder(Position = 1)]
        [JsonProperty("TriggerType", Order = 2)]
        public TriggerType TriggerType
        {
            get
            {
                return triggerType;
            }

            set
            {
                triggerType = value;
                switch (value)
                {
                    case TriggerType.Static:
                        Trig = new StaticTrigger();
                        break;
                    case TriggerType.Interval:
                        Trig = new IntervalTrigger();
                        break;
                    case TriggerType.External:
                        Trig = new ExternalTrigger();
                        break;
                    case TriggerType.Appointment:
                        Trig = new AppointmentTrigger();
                        break;
                    default:
                        break;
                }
            }
        }
    }

so defining the order correctly did the trick. nevertheless thanks for your help.

ok so its been ages since i had to deal with serialization and i have one probably pretty stupid problem,

 

im serializing a list of objects and the result look perfectly fine like this 

[
  {
    "ID": 32,
    "Trig": {
      "$type": "Alfheim_Model.TRIGGERS.StaticTrigger, Alfheim_Model"
    },
    "TriggerType": 0,
    "Name": "Dummy Trigger Static"
  },
  {
    "ID": 33,
    "Trig": {
      "$type": "Alfheim_Model.TRIGGERS.IntervalTrigger, Alfheim_Model",
      "Interval": 15
    },
    "TriggerType": 1,
    "Name": "Dummy Trigger Interval"
  },
  {
    "ID": 34,
    "Trig": {
      "$type": "Alfheim_Model.TRIGGERS.ExternalTrigger, Alfheim_Model"
    },
    "TriggerType": 2,
    "Name": "Dummy Trigger External"
  },
  {
    "ID": 35,
    "Trig": {
      "$type": "Alfheim_Model.TRIGGERS.AppointmentTrigger, Alfheim_Model",
      "Appointment": "2018-03-15T20:00:00+01:00"
    },
    "TriggerType": 3,
    "Name": "Dummy Trigger Appointment"
  }
]

every property i want to save is in there.

 

now when i deserialize it using this

		Newtonsoft.Json.Formatting indented = Newtonsoft.Json.Formatting.Indented;
        JsonSerializerSettings settings = new JsonSerializerSettings()
        {
            TypeNameHandling = TypeNameHandling.Auto
        };

        public string Serialize(List<Task> tasks)
        {
            return JsonConvert.SerializeObject(tasks, indented, settings);
        }

        public void DeSerialize(string xmltasks, out List<Task> list)
        {
            if (String.IsNullOrEmpty(xmltasks))
            {
                list = new List<Task>();
                return;
            }
            list = JsonConvert.DeserializeObject<List<Task>>(xmltasks, settings);
        }

        public string Serialize(List<Trigger> triggers)
        {
            return JsonConvert.SerializeObject(triggers, indented, settings);
        }

        public void DeSerialize(string xmltriggers, out List<Trigger> list)
        {
            if (String.IsNullOrEmpty(xmltriggers))
            {
                list = new List<Trigger>();
                return;
            }
            list = JsonConvert.DeserializeObject<List<Trigger>>(xmltriggers, settings);
        }

it does create the the list of trigger objects but leaves the "Interval" and "Appointment" Porperties empty or default rather. 

heres the data structure

public class Trigger : Param
    {
        private long iD;

        private ITrigger trig;

        private TriggerType triggerType;

        public long ID
        {
            get
            {
                return iD;
            }

            set
            {
                iD = value;
            }
        }
        
        [DetailOrder(Position = 2)]
        public ITrigger Trig
        {
            get
            {
                return trig;
            }

            set
            {
                trig = value;
            }
        }

        [DetailOrder(Position = 1)]
        public TriggerType TriggerType
        {
            get
            {
                return triggerType;
            }

            set
            {
                triggerType = value;
                switch (value)
                {
                    case TriggerType.Static:
                        Trig = new StaticTrigger();
                        break;
                    case TriggerType.Interval:
                        Trig = new IntervalTrigger();
                        break;
                    case TriggerType.External:
                        Trig = new ExternalTrigger();
                        break;
                    case TriggerType.Appointment:
                        Trig = new AppointmentTrigger();
                        break;
                    default:
                        break;
                }
            }
        }
    }
public class IntervalTrigger : ITrigger
    {
        private long interval;
        
        [DetailOrder(Position = 1)]
        public long Interval
        {
            get
            {
                return interval;
            }

            set
            {
                interval = value;
            }
        }
    }
public class ITrigger
    {

    }

 

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to comment
https://linustechtips.com/topic/906148-c-jsonnet-deserialization-question/
Share on other sites

Link to post
Share on other sites

12 hours ago, EricBP said:

Hi,

 

You need to define JsonProperty on ur json object but also removing the setting for handling name automatically.

 

This is a solution for you https://we.tl/iR4WveEBTV

 

Enjoy.

welp... i tried that but to no effect. but since you provided code for the solution maybe i could ask you to just have a look at my code? its all up on github

https://github.com/CluelessGenius/Alfheim

look at the 'SerializationHelper.cs' in the 'Alfheim_Viewmodel' project

if you add a trigger and then change the type to interval and then set the interval to 15 and close the form with the close-button(where serialization is triggered) then open it again and watch interval being reset to 0

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to post
Share on other sites

1 hour ago, EricBP said:

I will check tonight

jesus christ took me half a day to figure this out but i finally got it.

basically the serialization order was wrong and i had a property re-initializing the trigger after it was set

[JsonObject]
    public class Trigger : Param
    {
        private long iD;

        private ITrigger trig;

        private TriggerType triggerType;

        [JsonProperty("ID", Order = 1)]
        public long ID
        {
            get
            {
                return iD;
            }

            set
            {
                iD = value;
            }
        }

        [DetailOrder(Position = 2)]
        [JsonProperty("Trig", Order = 3)]
        public ITrigger Trig
        {
            get
            {
                return trig;
            }

            set
            {
                trig = value;
            }
        }

        [DetailOrder(Position = 1)]
        [JsonProperty("TriggerType", Order = 2)]
        public TriggerType TriggerType
        {
            get
            {
                return triggerType;
            }

            set
            {
                triggerType = value;
                switch (value)
                {
                    case TriggerType.Static:
                        Trig = new StaticTrigger();
                        break;
                    case TriggerType.Interval:
                        Trig = new IntervalTrigger();
                        break;
                    case TriggerType.External:
                        Trig = new ExternalTrigger();
                        break;
                    case TriggerType.Appointment:
                        Trig = new AppointmentTrigger();
                        break;
                    default:
                        break;
                }
            }
        }
    }

so defining the order correctly did the trick. nevertheless thanks for your help.

"You know it'll clock down as soon as it hits 40°C, right?" - "Yeah ... but it doesnt hit 40°C ... ever  😄"

 

GPU: MSI GTX1080 Ti Aero @ 2 GHz (watercooled) CPU: Ryzen 5600X (watercooled) RAM: 32GB 3600Mhz Corsair LPX MB: Gigabyte B550i PSU: Corsair SF750 Case: Hyte Revolt 3

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×