I have JSON string with different object depends on type of message. Example :
Text
{
"message": {
"name": "sender",
"from": "60123456789",
"id": "ABGGYBJlBxcPAgo6zXukA7i8jnSe",
"timestamp": "1585125726",
"type": "text",
"text": {
"body": "Hi"
}
}
Image
{
"message": {
"name": "sender",
"from": "60123456789",
"id": "ABGGYBJlBxcPAgo6zXukA7i8jnSe",
"timestamp": "1585125726",
"type": "image",
"image": {
"mime_type": "image/jpeg",
"link": "http://lala.com/Files/image.jpeg",
"caption": "your image caption"
}
}
And many more. The key such name
, from
, id
and others is still same.
So what I do in code, I have to create objects for each type :
Object for Text
public class InboundMessageText
{
public Message Message { get; set; }
}
public class Message
{
public string Name { get; set; }
public string From { get; set; }
public string Id { get; set; }
public int Timestamp { get; set; }
public string Type { get; set; }
public TextMessage Text { get; set; }
}
public class TextMessage
{
public string Body { get; set; }
}
Object for Image
public class InboundMessageImage
{
public Message Message { get; set; }
}
public class Message
{
public string Name { get; set; }
public string From { get; set; }
public string Id { get; set; }
public int Timestamp { get; set; }
public string Type { get; set; }
public ImageMessage Image{ get; set; }
}
public class ImageMessage
{
public string Mime_Type { get; set; }
public string Link { get; set; }
public string Caption { get; set; }
}
This how I deserialize
var inboundMessage = JsonConvert.DeserializeObject <InboundMessageText> ("json string");
var addInboundMessage = new Data.Domain.InboundMessage {
From = inboundMessage.Message.From,
Name = inboundMessage.Message.Name,
MessageId = inboundMessage.Message.Id,
MessageTimestamp = DateTimeOffset.FromUnixTimeSeconds(inboundMessage.Message.Timestamp).LocalDateTime,
};
if (inboundMessage.Message.Type == "text") {
addInboundMessage.InboundType = InboundType.Text;
addInboundMessage.Text = inboundMessage.Message.TextMessage.Body;
}
else if (inboundMessage.Message.Type == "image") {
//So here I have to deserialize again for image object
var inboundMessageImage = JsonConvert.DeserializeObject <InboundMessageImage>("json string");
addInboundMessage.InboundType = InboundType.Image;
//Business logic
}
//and many type more...
The code is working, but I have more than 7 types of message so make it my code is to long.
It's that possible to simplify the code?