How to parse Json using retrofit and parceler library

879 views Asked by At

I have read that better way is to parse with parcelable instead of serialisable according to this blog post http://blog.robinchutaux.com/blog/a-smart-way-to-use-retrofit/

But how can I customize parsing using Parceler library (https://github.com/johncarl81/parceler)?

In GSON library we could do it in this way:

public class GuestEntityDeserializer implements JsonDeserializer<GuestEntity> {

    private static final String ID = "id";
    private static final String FIRST_NAME = "firstname";
    // other fields...

    @Override
    public GuestEntity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
            throws JsonParseException {
        final GuestEntity guestEntity = new GuestEntity();
        final JsonObject jsonObject = json.getAsJsonObject();

        guestEntity.setId(jsonObject.get(ID).getAsString());
        guestEntity.setFirstName(jsonObject.get(FIRST_NAME).getAsString());
        guestEntity.setLastName(jsonObject.get(LAST_NAME).getAsString());
        // and so on...

        return guestEntity;
    }

And configuring GsonBuilder like this:

        final Gson gson = new GsonBuilder()
                .registerTypeAdapter(GuestEntity.class, new GuestEntityDeserializer())
                .create();

So, the question is: How to parse json string using Retrofit and Parceler library? For example, I need two objects with following model.

@Parcel
public class MatchModel {

    @SerializedName("id")
    private String mId;
    @SerializedName("first")
    private String mFirst;
    @SerializedName("last")
    private String mLast;
    @SerializedName("ilike")
    private String mIlike;
    @SerializedName("created_at")
    private String mCreatedAt;
    @SerializedName("liketo")
    private String mLiketo;
    @SerializedName("firstname")
    private String mFirstName;
    @SerializedName("lastname")
    private String mLastName;
    @SerializedName("birthday")
    private String mBirthday;

    //  getters and setters
}

I need to parse this:

{
  "1277": {
    "id": "322",
    "first": "1294",
    "last": "1277",
    "ilike": "1",
    "created_at": "2015-07-31 18:51:47",
    "liketo": "1277",
    "firstname": "Alex",
    "lastname": "Alexov",
    "birthday": null
  },
  "1312": {
    "id": "951",
    "first": "1294",
    "last": "1312",
    "ilike": "1",
    "created_at": "2015-08-06 15:12:29",
    "liketo": "1312",
    "firstname": "Roman",
    "lastname": "Romanov",
    "birthday": "1990-06-23"
  }
}
1

There are 1 answers

0
steevoo On BEST ANSWER

parsing using retrofit

RestAdapter restAdapter = new RestAdapter.Builder()
   .setEndpoint("url")
   .setClient(new OkClient(new OkHttpClient()))
   .build();

getMatchModel mMatchModel= restAdapter.create(getMatchModel.class);
Map<String, MatchModel > mMatchModelMaps = mMatchModel.MatchModel(params..);


public interface getMatchModel {
   @GET("/{params}")
   Map<String, MatchModel > MatchModel(@Path("params") String params);
}