Answer the question
In order to leave comments, you need to log in
How to parse complex json files using Gson and TypeAdapterFactory?
Hello.
I am developing a rest application on Android. I use Retrofit + Gson + RxJava.
The following responses come from the server:
{"error":false,"json":true,"body":{...JSON object...}}
public class ItemTypeAdapterFactory implements TypeAdapterFactory {
public static final String BODY = "body";
@Override
public <T> TypeAdapter<T> create(Gson gson, final TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter<T>() {
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
Logger.d(this, "dami");
}
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.has(BODY) && jsonObject.get(BODY).isJsonObject()) {
jsonElement = jsonObject.get(BODY);
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
public abstract class AbstractApiService {
private RestAdapter restAdapter;
public AbstractApiService() {
Gson gson = new GsonBuilder()
.registerTypeAdapterFactory(new ItemTypeAdapterFactory())
.create();
restAdapter = new RestAdapter.Builder()
.setEndpoint(ApiConf.API_URL)
.setConverter(new GsonConverter(gson))
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();
}
protected <T> T create(Class<T> service) {
return restAdapter.create(service);
}
}
Answer the question
In order to leave comments, you need to log in
If I'm not mistaken, in order for parsing to work, you must explicitly tell GSON what type of object to parse. Otherwise, he does not know why he should parse with the specified parser. But I really didn't use retrofit.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question