Gson + AutoValue error while trying to implement own deserializer
<p>I have a Retrofit interface that I combine with RxJava. All my retrofit calls return Observable. Those "SomePojo" classes, I generate them online using Schema2Pojo sites.</p>

<p>I have a problem when making the following api call: 
<a href="https://developers.themoviedb.org/3/search/2Y9y2LReFZdHFHbFA" rel="nofollow">https://developers.themoviedb.org/3/search/2Y9y2LReFZdHFHbFA</a></p>

<p>As you can see, it is an array with two different types of objects, that I called "Media" and "Credit". These two classes I generated using Google's autovalue as follows:</p>

<pre><code>@AutoValue
public abstract class Media implements Parcelable {

@SerializedName(value = "title", alternate = {"name"})
public abstract String title();

@Nullable
@SerializedName("vote_average")
public abstract String voteAverage();

@Nullable
@SerializedName("backdrop_path")
public abstract String backdropPath();

@Nullable
public abstract String adult();

public abstract String id();

@Nullable
public abstract String overview();

@Nullable
@SerializedName("original_language")
public abstract String originalLanguage();

@Nullable
@SerializedName("genre_ids")
public abstract List&lt;String&gt; genreIds();

@Nullable
@SerializedName(value = "release_date", alternate = {"first_air_date"})
public abstract String releaseDate();

@Nullable
@SerializedName(value = "original_title", alternate = {"original_name"})
public abstract String originalTitle();

@Nullable
@SerializedName("vote_count")
public abstract String voteCount();

@Nullable
@SerializedName("poster_path")
public abstract String posterPath();

@Nullable
public abstract String video();

@Nullable
@SerializedName("media_type")
public abstract String mediaType();

@Nullable
public abstract String popularity();

@Nullable
@SerializedName("origin_country")
public abstract List&lt;String&gt; originalCountry();


public static Media create(String title, String voteAverage, String backdropPath,
                           String adult, String id, String overview, String originalLanguage,
                           List&lt;String&gt; genreIds, String releaseDate, String originalTitle,
                           String voteCount, String posterPath, String video, String mediaType,
                           String popularity, List&lt;String&gt; originalCountry) {

    return new AutoValue_Media(title, voteAverage, backdropPath, adult, id, overview,
            originalLanguage, genreIds, releaseDate, originalTitle, voteCount, posterPath,
            video, mediaType, popularity, originalCountry);
}

public static TypeAdapter&lt;Media&gt; typeAdapter(Gson gson) {
    return new AutoValue_Media.GsonTypeAdapter(gson);
}
}
</code></pre>

<p>And:</p>

<pre><code>@AutoValue
public abstract class Credit implements Parcelable {
public abstract String id();

@SerializedName("credit_id")
public abstract String creditId();

@Nullable
public abstract String department();

public abstract String name();

@Nullable
@SerializedName(value = "job", alternate = {"character"})
public abstract String job();

@Nullable
@SerializedName("profile_path")
public abstract String profilePath();

@Nullable
public abstract String order();

@Nullable
@SerializedName("cast_id")
public abstract String castId();


public static Credit create(String id, String creditId, String department, String name, String job,
                            String profilePath, String order, String castId) {

    return new AutoValue_Credit(id, creditId, department, name, job, profilePath, order, castId);
}

public static TypeAdapter&lt;Credit&gt; typeAdapter(Gson gson) {
    return new AutoValue_Credit.GsonTypeAdapter(gson);
}

}
</code></pre>

<p>To resolve the problem created by the array with two different kind of objects, I made the POJO return by this call implement its own JsonDeserializer:</p>

<pre><code>public class MediaListPojo {

    @SerializedName("results")
    private List&lt;Media&gt; movies;

    private List&lt;Credit&gt; credits;

    private Dates dates;

    private String page;

    private String total_pages;

    private String total_results;

    public List&lt;Media&gt; getMedia() {
        return movies;
    }

    public void setMovies(List&lt;Media&gt; movies) {
        this.movies = movies;
    }

    public List&lt;Credit&gt; getCredits() {return credits;}

    public void setCredits(List&lt;Credit&gt; credits) {this.credits = credits;}

    public Dates getDates() {
        return dates;
    }

    public void setDates(Dates dates) {
        this.dates = dates;
    }

    public String getPage() {
        return page;
    }

    public void setPage(String page) {
        this.page = page;
    }

    public String getTotal_pages() {
        return total_pages;
    }

    public void setTotal_pages(String total_pages) {
        this.total_pages = total_pages;
    }

    public String getTotal_results() {
        return total_results;
    }

    public void setTotal_results(String total_results) {
        this.total_results = total_results;
    }

    @Override
    public String toString() {
        return "MediaListPojo{" +
                "movies=" + movies +
                ", credits=" + credits +
                ", dates=" + dates +
                ", page='" + page + '\'' +
                ", total_pages='" + total_pages + '\'' +
                ", total_results='" + total_results + '\'' +
                '}';
    }

    public static class MediaListPojoDeserializer implements JsonDeserializer&lt;MediaListPojo&gt; {

        @Override
        public MediaListPojo deserialize(JsonElement json, Type typeOfT,
                                         JsonDeserializationContext context) throws JsonParseException {

            MediaListPojo mediaListPojo = new Gson().fromJson(json, MediaListPojo.class);
            JsonObject  jsonObject = json.getAsJsonObject();

            if (jsonObject.has("results")) {
                JsonArray jsonArray = jsonObject.getAsJsonArray("results");
                List&lt;Credit&gt; credits = new ArrayList&lt;&gt;();
                Credit credit;
                for (JsonElement element : jsonArray) {
                    JsonObject current = element.getAsJsonObject();

                    if (current.get("media_type").getAsString().equals("person")) {
                        credit = new Gson().fromJson(current, Credit.class);
                        credits.add(credit);
                    }
                }
                mediaListPojo.setCredits(credits);
            }
            return mediaListPojo;
        }
    }
}
</code></pre>

<p>The main idea behind this json deserializer is: "Use the default type adapter for this class and then set the Credit objects using this JsonDeserializer"</p>

<p>However, for some reason, I get the following error while deserializing:</p>

<p>java.lang.RuntimeException: Failed to invoke public Media() with no args
...
Caused by: java.lang.InstantiationException: Can't instantiate abstract class Media
                                                                                          at java.lang.reflect.Constructor.newInstance(Native Method)</p>

<p>It shouldn't try to instantiate the abstract superclass, but use AutoValue's generated Type Adapter.</p>

<p>This is how I built my retrofit instance:</p>

<pre><code>class Creator {

    public static MovieService newMovieService() {
        Gson gson = new GsonBuilder()
                .registerTypeAdapterFactory(new AutoValueGson_MyAdapterFactory())
                .registerTypeAdapter(MediaListPojo.class, new MediaListPojo.MediaListPojoDeserializer())
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                .create();
        OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(NetworkUtil.makeQueryInterceptor("api_key", BuildConfig.MY_API_KEY))
                .build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(client)
                .baseUrl(MovieService.ENDPOINT)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return retrofit.create(MovieService.class);
</code></pre>

<p>Can you help me understand what I did wrong?</p>