1

In my app, I've got responses from a server. These responses can be :

{
    "cmd":"read_ack",
    "model":"sensor_ht",
    "sid":"158d0001a2ddac",
    "short_id":36192,
    "data":"{\"voltage\":3005,\"temperature\":\"2741\",\"humidity\":\"5828\"}"
}

Or it can be :

{  
    "cmd":"read_ack",
    "model":"magnet",
    "sid":"158d000159febe",
    "short_id":40805,
    "data":"{\"voltage\":3045,\"status\":\"open\"}"
}

Can I use something like "dynamic" class ?

My class look like this :

    public class RootObject
    {
        public string cmd { get; set; }
        public string model { get; set; }
        public string sid { get; set; }
        public int short_id { get; set; }
        public Data data { get; set; }
    }

And my data class looks like :

    public class Data
    {
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public int voltage { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string status { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string temperature { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string humidity { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string no_close { get; set; }
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public string no_motion { get; set; }
    }

Is it possible to have different classes for each type of response I get ? Is it possible to change the data type in RootObject class dynamically ?

Thank a lot

Hydro
  • 83
  • 1
  • 10
  • 1
    Try inheritance (I think that's what you want). If all your classes need the stuff in RootObject then get them to inherit from it. See https://learn.microsoft.com/en-us/dotnet/csharp/tutorials/inheritance – David Christopher Reynolds Jun 27 '17 at 13:29
  • 3
    https://stackoverflow.com/questions/20432166/how-to-deserialize-a-json-property-that-can-be-two-different-data-types-using-js – CodeCaster Jun 27 '17 at 13:29

1 Answers1

0

You'll need a custom converter as explained in How to deserialize a JSON property that can be two different data types using Json.NET, and inheritance to change the data type.

You could approach it with an empty base class that more derived types implement:

public class RootObject
{
    public string cmd { get; set; }
    public string model { get; set; }
    public string sid { get; set; }
    public int short_id { get; set; }
    public SensorData data { get; set; }
}

public abstract class SensorData
{       
}

public class MagnetData : SensorData
{
    public int Voltage { get; set; }
}

public class SomeOtherSensorData : SensorData
{
    public bool AnotherProperty { get; set; }
}

And then in your converter, either deserialize into a MagnetData or SomeOtherSensorData or whatever other classes you want.

That is, granted that you actually want to create a class per type of data. There are pros and cons to that.

Alternatively, just deserialize your data into a Dictionary<string, object>, where you pull out the relevant fields by their (string) name.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thank it works. However my main program use the type inside a class to build documentation for the function. So it's empty because SensorData is empty. But I think there is no solution to this minor problem. – Hydro Jun 30 '17 at 10:01