3

I trying to populate a response object that looks like this using Gson:

public class RegisterUserResponse extends BaseResponse {

  @SerializedName("tokens")
  private Tokens tokens;

  public RegisterUserResponse() { }

  public Tokens getTokens() {
      return tokens;
  }
}

My Gson builder looks like this:

RegisterUserResponse response = getGson().fromJson(jsonResponse, RegisterUserResponse.class);

When I receive a JSON that contains the object everything's fine. The problem begins when "tokens" return empty, like in this JSON:

{"tokens":""}

and I get this exception:

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line # column #

Which I do understand, but don't know how to avoid.

Aviv Ben Shabat
  • 1,073
  • 2
  • 13
  • 33
  • 1
    json basics ... if "tokens" property "is a Tokens class" then `null` should be used `{"tokens":null}`... and error is self explanatory: *Expected BEGIN_OBJECT but was STRING* – Selvin Feb 05 '15 at 09:45
  • @Selvin Since i'm not controling the server, I can't control the response. I wish I could get null. Don't see a reason to downgrade. I'm trying to deal with a situation. – Aviv Ben Shabat Feb 05 '15 at 09:48
  • 2
    then you have to use custom deserializer – Selvin Feb 05 '15 at 09:49

0 Answers0