C # de-serializing JSON for different types

I am currently working on a C # application that communicates with a node.js server via JSON. I am using Json.net to perform deserialization.

My problem is that JSON messages are of different types (unknown until the message appears) and I need to deserialize the correct type depending on what type of message it is.

Is there an elegant approach to this? Possible (non-ideal) solutions that happened to me include some kind of "all encompasing" DTO message that any message can be deserialized (not good); or some kind of "message in message" where the message type id is deserialized and then the subsequent message is then deserialized to a specific type, but I believe there must be a better way.

I'm limited to .Net 3.5 so can't use the keyword dynamic

.

thank

+3


source to share


1 answer


the node server has to send a json type for every response. Another solution uses dynamic types as follows:



JsonConvert.DeserializeObject<dynamic>(JSONtext)

      

0


source







All Articles