Deserialize two different objects based on their properties

52 Views Asked by At

I have a Redis subscription in a channel that returns me two type of objects

one is the success that returns me this:

public class Success {
   public string Token {get; set;}
   public object Message {get; set;}
}

and another one for error

public class Error {
  public string ErrorMessage {get; set;}
  public string Name {get; set;}
}

My redis subscriptions on message code is this:

 subscription.OnMessage = (channel, msg) =>
 {
   var success = JsonConvert.DeserializeObject<Success>(msg);
   // do the things with success
 };

but i dont know when the error will be returned, so i need to deserialize to the Success or to the Error model based on the properties, something like this:

   var successOrError = JsonConvert.DeserializeObject<Either<Success, Error>>(msg);

is this possible ? or there is another way to do this

0

There are 0 best solutions below