square / retrofit

A type-safe HTTP client for Android and the JVM
https://square.github.io/retrofit/
Apache License 2.0
42.97k stars 7.3k forks source link

Custom TypeAdapter(Retrofit2.x) #1590

Closed Manu-Bhardwaj closed 8 years ago

Manu-Bhardwaj commented 8 years ago

Is there any way to add multiple custom type adapters to a single client builder using addConverterFactory

dampcake commented 8 years ago

Not much information to go off of, but if you are asking about registering multiple TypeAdapters in Gson and then using it in Retrofit you could do the following:

Gson gson = new GsonBuilder()
            .registerTypeAdapter(Type1.class, new Type1Adapter())
            .registerTypeAdapter(Type2.class, new Type2Adapter())
            .build();
Api api =  new Retrofit.Builder()
            .addConverterFactory(GsonConverterFactory.create(gson))
            .baseUrl(url)
            .build()
            .create(Api.class);
Manu-Bhardwaj commented 8 years ago

I am parsing a json response whose structure is as follows: ->query(object)

results(object) scorecard(can be a list of objects or a single object) pastIngs(can be list or object)

Here are the POJO classes and custom type adapters.Plz check if anything is wrong

`public class Results {

private List<com.test.cric.live_model.all_matches_summary.Scorecard> Scorecard= new ArrayList<com.test.cric.live_model.all_matches_summary.Scorecard>();
/**
 *
 * @return
 *     The Scorecard
 */
public List<com.test.cric.live_model.all_matches_summary.Scorecard> getScorecard() {
    return Scorecard;
}

/**
 *
 * @param Scorecard
 *     The Scorecard
 */
public void setScorecard(List<com.test.cric.live_model.all_matches_summary.Scorecard> Scorecard) {
    this.Scorecard = Scorecard;
}

} public class Scorecard {

public void set_past_ing(Past_ing ... p) {
    past_ings = Arrays.asList(p);
}
public void set_teams(Team ... t) {
    teams = Arrays.asList(t);
}

private String mid;
private Series series;
private String mn;
private List<Team> teams = new ArrayList<Team>();
private List<Past_ing> past_ings = new ArrayList<Past_ing>();
private Result result;

//Getters and setters

}`

`public class ResultsTypeAdapter extends TypeAdapter {

private Gson gson = new Gson();

@Override
public void write(JsonWriter jsonWriter, Results results) throws IOException {
    gson.toJson(results, Results.class, jsonWriter);
}

@Override
public Results read(JsonReader jsonReader) throws IOException {
    Results results=new Results();

    jsonReader.beginObject();
    jsonReader.nextName();

    if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) {
        results = new Results((Scorecard[]) gson.fromJson(jsonReader, Scorecard[].class));
    } else if(jsonReader.peek() == JsonToken.BEGIN_OBJECT) {
        results = new Results((Scorecard) gson.fromJson(jsonReader, Scorecard.class));
    } else {
        throw new JsonParseException("Unexpected token " + jsonReader.peek());
    }

    jsonReader.endObject();
    return results;
}

} `

`public class ScorecardTypeAdapter extends TypeAdapter {

private Gson gson = new Gson();

@Override
public void write(JsonWriter jsonWriter, Scorecard scorecard) throws IOException {
    gson.toJson(scorecard, Scorecard.class, jsonWriter);
}

@Override
public Scorecard read(JsonReader jsonReader) throws IOException {
    Scorecard scorecard=new Scorecard();

    jsonReader.beginObject();
    while(jsonReader.hasNext()) {
        jsonReader.nextName();
        String name=jsonReader.nextName();
        if(name.equals("mid")){
            scorecard.setMid(jsonReader.nextString());
        }
       else if(name.equals("mn")){
            scorecard.setMn(jsonReader.nextString());
        }
       else if(name.equals("series")){
            if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) {
                scorecard.setSeries((Series) gson.fromJson(jsonReader, Series.class));
            } else {
                throw new JsonParseException("Unexpected token " + jsonReader.peek());
            }

        }
       else if(name.equals("teams")){
            scorecard.set_teams((Team[]) gson.fromJson(jsonReader, Team[].class));

        }
       else if(name.equals("past_ings")) {
            if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) {
                scorecard.set_past_ing((Past_ing[]) gson.fromJson(jsonReader, Past_ing[].class));
            } else if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) {
                scorecard.set_past_ing((Past_ing) gson.fromJson(jsonReader, Past_ing.class));
            } else {
                throw new JsonParseException("Unexpected token " + jsonReader.peek());
            }
        }

       else if(name.equals("result")){

            if (jsonReader.nextString().equals("0")) {
                scorecard.setResult(null);
            } else if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) {
                scorecard.setResult((Result) gson.fromJson(jsonReader, Result.class));
            } else {
                throw new JsonParseException("Unexpected token " + jsonReader.peek());
            }
        }

        else
        {
            jsonReader.skipValue();
        }

    }

    jsonReader.endObject();
    return scorecard;
}

} `

Manu-Bhardwaj commented 8 years ago

Link to json file http://spmysql.site88.net/test.json

JakeWharton commented 8 years ago

Plz check if anything is wrong

This issue tracker is for bug reports and feature requests only. If you have usage questions you can ask on StackOverflow with the 'retrofit' tag.