0

How do I deserialize json string to object depends on json content? I would like to do this with Newtonsoft.Json, since I am using it in the whole project and it is simple for use.

Let me explain by example: I have json string which can have to be different property object. Content depends on the tool that generates the json files, and I cannot predict the content base on the filename or something like that.

For example, I can have json file:

{  
   "FileCreationDate":"29-08-2018 14:56:30",
   "MessageType":2,
   "Messages":[  
      {  
         "MessageSequenceNumber":1,
         "ModalType":5,
         "Message":{  
            "TransactionValue":5,
            "ProductAmount":5
         }
      }
   ]
}

Or I can have something like this:

{  
   "FileCreationDate":"29-08-2018 14:56:30",
   "MessageType":1,
   "Messages":[  
      {  
         "MessageSequenceNumber":1,
         "ModalType":5,
         "Message":{  
            "TransactionBusinessDate":"29-08-2018 14:54:29",
            "TransactionStatus":5,
            "TicketNumber":5,
         }
      }
   ]
}

You can see that both json strings have same properties except message object in messages list. I want to deserialize to this data structure:

public class EventFileDto
{
    public string FileCreationDate { get; set; }
    public MessageType MessageType { get; set; }
    public IEnumerable<MessageDetailsDto> Messages { get; set; }
}

public class MessageDetailsDto
{
    public int MessageSequenceNumber { get; set; }
    public int ModalType { get; set; }
    public EventMessageDto EventMessage { get; set; }
    public TransactionMessage TransactionMessage { get; set; }
}

If json string is from the first example I want deserialize message object to EventMessage property, and TransactionMessage property should be null.

In the case of second json string, I want the opposite.

I don't want use dynamic type, since mapping to the entity would be more complicated.

How can this be done?

Thank you for your help.

Bizhan
  • 16,157
  • 9
  • 63
  • 101
Raskolnikov
  • 3,791
  • 9
  • 43
  • 88
  • How do you determine, looking at the json, which one to deserialize? Is one of the "type" fields used for this? – Lasse V. Karlsen Sep 21 '18 at 06:46
  • MessageType can be used for this. If you think that this can help. – Raskolnikov Sep 21 '18 at 06:48
  • Type `Object` will do a work but not sure how you are going use that on UI side – Prashant Pimpale Sep 21 '18 at 06:51
  • 1
    There are already similar questions such as [Json.Net Serialization of Type with Polymorphic Child Object](https://stackoverflow.com/a/29531372/3744182) (possibly closest), [How to implement custom JsonConverter in JSON.NET to deserialize a List of base class objects?](https://stackoverflow.com/q/8030538/3744182) and [Deserializing polymorphic json classes without type information using json.net](https://stackoverflow.com/q/19307752/3744182). Do those answer your question? – dbc Sep 21 '18 at 06:52
  • You could use reflection to get the message type, and then pass it to a function to convert it to an object of that type. Not very efficient, but certainly the easiest. – Grimson Sep 21 '18 at 06:54
  • In addition to what @dbc is linking to, you can create a property you deserialize this into and type it as `JObject`, then inspect the objects and use `.ToObject()` when you've decided what it should be. – Lasse V. Karlsen Sep 21 '18 at 06:54
  • The question is why, just use one class with the whole schema and be done to it, figure the rest out after the fact – TheGeneral Sep 21 '18 at 07:01
  • 1
    Derive both from a common base class and enable TypeNameHandling on the JsonSerializerSettings. JSON.net will then automatically take care to deserialize to the proper class. – ckuri Sep 21 '18 at 07:24

0 Answers0