Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrofit map json variable to keyword

So I'm working with retrofit with an API that has a variable called "public". How would I go about getting it to automatically map like all the other variables do.

Example:

@GET("/?filter=my_images")
void getMyImages(
        @Query("client_id") String id,
        @Query("api_key") String key,
        Callback<ImageList> callback
);
public static class Image{
    int id;
    String name;
    String distribution;
    String slug;
    // Can't do this:
    boolean public;
}

public static class ImageList{
    String status;
    List<Image> images;
}

Example API results (json):

{
  "status": "OK",
  "images": [
    {
      "id": 1,
      "name": "My first snapshot",
      "distribution": "Ubuntu",
      "slug": "ubuntu-12.10-x32",
      "public": true
    },
    {
      "id": 2,
      "name": "Automated Backup",
      "distribution": "Ubuntu"
    }
  ]
}
like image 680
JaySee Avatar asked Jun 20 '14 16:06

JaySee


1 Answers

Retrofit uses Gson for serialization to and from JSON.

Gson provides a @SerializedName annotation in order to change the key to which a field or method is mapped. You can use this for handling your reserved word:

@SerializedName("public")
public String isPublic;
like image 127
Jake Wharton Avatar answered Oct 12 '22 19:10

Jake Wharton