1
I’m working on an API integration with JSON. I have not implemented anything yet in my code C#, I am for now studying the API but I have already come up with a question.
I have a Login method that if it succeeds returns me a type of answer, and if it fails returns me a totally different type.
Example:
If successful the return is :
{
   "Autenticacao": {
          "tokenAutenticacao": "9f6530dad90c7f8eda5670"
          "idUsuario": "c7f8eda5670"
    }
}
Using json2charp create the following class:
public class Autenticacao
{
    public string tokenAutenticacao { get; set; }
    public string idUsuario { get; set; }
}
public class LoginSucesso
{
    public Autenticacao Autenticacao { get; set; }
}
And if the login fails, the return is:
{
  "status": {
    "code": 403,
    "name": "Forbidden"
  },
"mensagem": "Login falhou",
"exception": "Blah Blah blah",
"dataHora": "18/06/2015 10:47:47"
}
Converting to a C# class have:
public class Status
{
    public int code { get; set; }
    public string name { get; set; }
}
public class LoginFalha
{
    public Status status { get; set; }
    public string mensagem { get; set; }
    public string exception { get; set; }
    public string dataHora { get; set; }
}
Note: I exchanged Rootobject for Loginsucesso and Loginfalha.
I intend to use the Json.NET to deserialize the JSON object, doing something like this:
LoginSucesso resultadoJson = JsonConvert.DeserializeObject<LoginSucesso>(meuJson);
But what if the login fails?
How should I treat code C#?
I treat an Exception and try LoginFalha resultadoJson = JsonConvert.DeserializeObject<LoginFalha>(meuJson); ????