Search code examples
c#jsonjson-deserializationobject-type

Deserialize JSON to different Object Types based on JSON content in C#


So, I have developed an Azure Function triggered by IoT Hub in order to receive messages from devices. And I want to deserialize them (messages received from devices as JSON) to different object types (obs. I am not allowed to change the syntax of messages). Is there a way I can deserialize into the correct object type, based on which message I receive?

  • Scenario 1: If the message has the following syntax, then deserialization should be of type ReadResponseMessage.

    string JsonMessageFromDevice = "{"device_id":"DeviceTest","message_id":0,"port":3,"portValue":false,"time":"2021-08-25 10:18:51","response_status":"Send"}";
    ReadResponseMessage MessageReceivedType1 = JsonConvert.DeserializeObject<ReadResponseMessage>(JsonMessageFromDevice);
    
  • Scenario 2: If the message has the following syntax, then deserialization should be of type EventMessage.

    string JsonMessageFromDevice = "{"device_id":"DeviceTest","message_id":1501,"port1":false,"port2":false,"time":"2021-08-25 10:22:02","response_status":"OK"}";
    EventMessage MessageReceivedType2 = JsonConvert.DeserializeObject<EventMessage>(JsonMessageFromDevice);
    

Where:

class ReadResponseMessage  {
     
      [JsonProperty("device_id")]
      public string DeviceID {get; set;}

      [JsonProperty("message_id")]
      public int MessageID {get; set;}

      [JsonProperty("port")]
      public int Port{get; set;}

      [JsonProperty("portValue")]
      public bool PortValue {get; set;}

      [JsonProperty("time")]
      public DateTime Time {get; set;}

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

And:

class EventMessage {

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

      [JsonProperty("message_id")]
      public int MessageID {get; set;}

      [JsonProperty("port1")]
      public bool Port1{get; set;}

      [JsonProperty("port2")]
      public bool Port2 {get; set;}

      [JsonProperty("time")]
      public DateTime Time {get; set;}

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

}

Solution

  • string JsonMessageFromDevice = "{"device_id":"DeviceTest","message_id":0,"port":3,"portValue":false,"time":"2021-08-25 10:18:51","response_status":"Send"}";
    
    var oMycustomclassname = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(JsonMessageFromDevice);
    
    

    or

    var jobject = JObject.Parse(JsonMessageFromDevice);
       var result = jobject["port"];
       if (result != null)
       {
           ReadResponseMessage readResponseMessage = jobject.ToObject<ReadResponseMessage>();
       }
       else
       {
           EventMessage eventMessage = jobject.ToObject<EventMessage>();
       }
    
    

    OR Change model

    class ReadResponseMessage  {
         
          [JsonProperty("device_id")]
          public string DeviceID {get; set;}
    
          [JsonProperty("message_id")]
          public int MessageID {get; set;}
    
          [JsonProperty("port")]
          public Dictionary<String,bool> Port{get; set;} // or List<string> port
    
          [JsonProperty("time")]
          public DateTime Time {get; set;}
    
          [JsonProperty("response_status")]
          public string ResponseStatus{get; set;}
     
    }