I send JSON objects from a client to a server. In the server I need to do some action based on the object type. I could send a filed type
in every request then try to parse JSON against base Message
class:
public class Message
{
string type { get; set; }
}
...
JsonSerializer.Deserialize<Message>(data);
then read the type and deserialize again:
public class SpecificMessage : Message
{
public string command;
}
var message = JsonSerializer.Deserialize<SpecificMessage>(data);
But it requires the data to be parsed twice.
I thought about solution like adding message type in front of JSON message but then thought that maybe it's not worth to reinvent the wheel.