Search code examples
c#jsonjson.net

How to deserialise JSON back to a class?


Ive got a class Limits. I am creating several instances of this class as I have other objects that each need a their own Limits. So when I am serializing all instances of Limits there are several entries in the json. In my example you will see I have Thermo. Imagine there could be Thermo1, Thermo2 etc all in the json. I deserialise it, navigate to the instance I want and then convert it to Limits. But for some reason all Min & Max values end up being 0.

The class:

public class Limits
{
    Measured_I_ON_struct Measured_I_ON;

    Measured_I_OFF_struct Measured_I_OFF;
    Measured_V_ON_struct Measured_V_ON;
    Measured_V_OFF_struct Measured_V_OFF;

    public Limits()
    {
        Measured_I_ON = new Measured_I_ON_struct();
        Measured_I_OFF = new Measured_I_OFF_struct();
        Measured_V_ON = new Measured_V_ON_struct();
        Measured_V_OFF = new Measured_V_OFF_struct();
    }

    public struct Measured_I_ON_struct
    {
        public double Min;
        public double Max;
    }
    public struct Measured_I_OFF_struct
    {
        public double Min;
        public double Max;
    }
    public struct Measured_V_ON_struct
    {
        public double Min;
        public double Max;
    }
    public struct Measured_V_OFF_struct
    {
        public double Min;
        public double Max;
    }
}

I have a json as such :

  {"Thermo":{
  "Measured_V_ON": 
  {
      "Min" : 4.5,
      "Max" : 5.5
  },
  "Measured_V_OFF": {
      "Min" : 4.5,
      "Max" : 5.5
  },
  "Measured_I_ON": {
      "Min" : 4.5,
      "Max" : 5.5
  },
   "Measured_I_OFF": {
      "Min" : 0.15,
      "Max" : 0.22
  },
  }
  }

I am trying to deserialise the json back to an instance of the class Limits but the values inside each struct come out as 0.

string json = File.ReadAllText("new 2.json");
JToken jsonobject = JsonConvert.DeserializeObject<JToken>(json);
jsonobject = jsonobject["Thermo"];
Limits limits = new Limits();
limits = jsonobject.ToObject<Limits>();

Everything inside limits has a value of 0. What am i missing here?


Solution

  • Your fields are private, which is why they're not being changed.

    I would change your code significantly though:

    • Use a single type for all the limits, given that they're exactly the same.
    • Use a class rather than a struct, to avoid creating a mutable struct. (You could potentially use a custom converter instead, but I think it's simpler to use a class.)
    • Use public properties for the properties in the Limits rather than private fields
    • Rename the properties to follow .NET naming conventions, using [JsonProperty] to specify the name you want in the JSON.

    So something like this:

    public class Limit
    {
        public double Min { get; set; }
        public double Max { get; set; }
    }
    
    public class Limits
    {
        // TODO: Rename these further to expand on "I" and "V" if possible
    
        [JsonProperty("Measured_I_ON")]
        public Limit MeasuredIOn { get; set; } = new Limit();
    
        [JsonProperty("Measured_I_OFF")]
        public Limit MeasuredIOff { get; set; } = new Limit();
    
        [JsonProperty("Measured_V_ON")]
        public Limit MeasuredVOn { get; set; } = new Limit();
    
        [JsonProperty("Measured_V_OFF")]
        public Limit MeasuredVOff { get; set; } = new Limit();
    }