I am working with an API that always responds like so:
{
\"stuff_i_need\": [
{
\"title\": \"Hello\"
},
{
\"title\": \"World!\"
}
With some help from Gowtham, I ended up doing the following:
private static class ItemTypeAdapterFactory implements TypeAdapterFactory {
@Override
public TypeAdapter create(Gson gson, final TypeToken type) {
final TypeAdapter delegate = gson.getDelegateAdapter(this, type);
final TypeAdapter elementAdapter = gson.getAdapter(JsonElement.class);
return new TypeAdapter() {
@Override
public void write(JsonWriter out, T value) throws IOException {
delegate.write(out, value);
}
@Override
public T read(JsonReader in) throws IOException {
JsonElement jsonElement = elementAdapter.read(in);
if (jsonElement.isJsonObject()) {
JsonObject jsonObject = jsonElement.getAsJsonObject();
if (jsonObject.entrySet().size() == 2) {
jsonObject.remove("status");
jsonElement = jsonObject.entrySet().iterator().next().getValue();
}
}
return delegate.fromJsonTree(jsonElement);
}
}.nullSafe();
}
}
and this is set on the RestAdapter.Builder:
.setConverter(new GsonConverter(new GsonBuilder()
.registerTypeAdapterFactory(new ItemTypeAdapterFactory())
.create()))
I really just ended up removing the "status" JsonObject.