3

I'm currently working on a c# app that is communicating with a node.js server via JSON. I'm using Json.net to do the deserialisation.

My problem is that the JSON messages are of various types (it's not known which until the message comes in), and I need to deserialise to the correct type depending on which type of message it is.

Is there an elegant approach to this? Possible (non-ideal) solutions that have occurred to me include some kind of 'all encompasing' message DTO type which any message can be deserialised to (not nice); or some kind of 'message in a message' where a message type identifier is deserialsed, and then a sub-message is then deserialsed to a specific type, but I feel there must be a better way.

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

Thank you

UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
  • Check http://stackoverflow.com/a/19308474 – haim770 Jan 08 '15 at 09:49
  • you can send some form of a tuple from node.js to the client. first item would be the json itself and a second could be a name of a class you need to deserialize to. on a client side you could use reflection to get a type from the name, or something else. – Nikola.Lukovic Jan 08 '15 at 09:50
  • @haim770 - thanks! That worked great for me (a json.net solution to the problem). Not sure what to do with this question now. If you wanted to add a short generic answer along those lines I will gladly accept it. – UpTheCreek Jan 08 '15 at 11:54

1 Answers1

0

the node server must send Type of json on each response.the other solution is using Dynamic types on this way:

JsonConvert.DeserializeObject<dynamic>(JSONtext)
Peyman Mehrabani
  • 619
  • 6
  • 17