1

I'm using RetroFit in order to communicate with my API. The response is JSON and one of JSON objects ('user') is a string. I'd like to parse that string into JSON.

I have a class for the response:

public class TokenModel {
   @SerializedName("access_token")
   private String accessToken;

   @SerializedName(".expires")
   private String expiryDate;

   private UserModel user;


   public String getAccessToken() {
       return accessToken;
   }

   public void setAccessToken(String accessToken) {
       this.accessToken = accessToken;
   }

   public String getExpiryDate() {
       return expiryDate;
   }

   public void setExpiryDate(String expiryDate) {
       this.expiryDate = expiryDate;
   }

   public UserModel getUser() {
       return user;
   }

   public void setUser(UserModel user) {
       this.user = user;
   }
}

and a class for the user:

public class UserModel {
   private int id;

   private String email;

   private String firstName;

   private String lastName;

   private String profileImageUrl; etc...

However, because the 'user' object in response is a string it needs to be first parsed into JSON. I'm unsure how to do this and still make it work with the model. Is there a way to tell RetroFit to first parse it into JSON before applying it to the model?

Thank you, Daniel

4
  • So change the response format to use a JSON object? Commented Feb 7, 2015 at 15:18
  • @corsair992 Unfortunately it can't be changed, needs to be parsed by the client. Commented Feb 7, 2015 at 22:41
  • 1
    Well, you can customize GSON desensitization by registering custom TypeAdapters, and then set a custom GsonConverter on your RestAdapter. Commented Feb 7, 2015 at 22:45
  • Search around, you'll find lots of examples and tutorials on how to do it. This question should really be marked as a duplicate, as I'm sure this has already been posted before on Stack Overflow. Commented Feb 7, 2015 at 22:49

1 Answer 1

1

Turns out to be quite simple using the advice of @corsair992.

Create a custom deserializer to parse string into Json:

public class UserDeserializer implements JsonDeserializer<UserModel> {
    @Override
    public UserModel deserialize(JsonElement jsonElement, Type typeOF,
                                 JsonDeserializationContext context)
            throws JsonParseException {
        String userString = jsonElement.getAsString();
        JsonElement userJson = new JsonParser().parse(userString);

        return new Gson().fromJson(userJson, UserModel.class);
    }
}

then set it as a converter on your rest adapter:

RestAdapter restAdapter = new RestAdapter.Builder()
                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setConverter(new GsonConverter(new GsonBuilder().registerTypeAdapter(UserModel.class, new UserDeserializer()).create()))
                .setEndpoint(getString(R.string.url_base))
                .build();

That will now convert the string into Json and make it function with the model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.