gpt4 book ai didi

java - 尝试实现自己的反序列化器时出现 Gson + AutoValue 错误

转载 作者:行者123 更新时间:2023-11-29 07:32:52 27 4
gpt4 key购买 nike

我有一个与 RxJava 结合的 Retrofit 接口(interface)。我所有的改造调用都返回 Observable。那些“SomePojo”类,我使用 Schema2Pojo 网站在线生成它们。

我在进行以下 api 调用时遇到问题: https://developers.themoviedb.org/3/search/2Y9y2LReFZdHFHbFA

如您所见,它是一个包含两种不同类型对象的数组,我称之为“Media”和“Credit”。我使用 Google 的自动值生成的这两个类如下:

@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<String> 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<String> originalCountry();


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

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

public static TypeAdapter<Media> typeAdapter(Gson gson) {
return new AutoValue_Media.GsonTypeAdapter(gson);
}
}

和:

@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<Credit> typeAdapter(Gson gson) {
return new AutoValue_Credit.GsonTypeAdapter(gson);
}

}

为了解决由具有两种不同类型对象的数组造成的问题,我让通过此调用返回的 POJO 实现了它自己的 JsonDeserializer:

public class MediaListPojo {

@SerializedName("results")
private List<Media> movies;

private List<Credit> credits;

private Dates dates;

private String page;

private String total_pages;

private String total_results;

public List<Media> getMedia() {
return movies;
}

public void setMovies(List<Media> movies) {
this.movies = movies;
}

public List<Credit> getCredits() {return credits;}

public void setCredits(List<Credit> 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<MediaListPojo> {

@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<Credit> credits = new ArrayList<>();
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;
}
}
}

这个 json 反序列化器背后的主要思想是:“使用这个类的默认类型适配器,然后使用这个 JsonDeserializer 设置 Credit 对象”

但是,由于某些原因,我在反序列化时遇到以下错误:

java.lang.RuntimeException: 调用没有参数的 public Media() 失败...Caused by: java.lang.InstantiationException: 无法实例化抽象类 Media 在 java.lang.reflect.Constructor.newInstance( native 方法)

它不应该尝试实例化抽象父类(super class),而是使用 AutoValue 生成的类型适配器。

这就是我构建改造实例的方式:

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);

你能帮我理解我做错了什么吗?

最佳答案

好吧,我在发布问题 5 分钟后找到了解决方案,但因为我认为其他人也可能对此感到困惑。我将分享解决方案:

基本上,在我的 JsonDeserializer 内部,我使用了一个 Gson 对象的新实例,而事实上,这是一个错误。

我在创建改造实例时注册的 typeadapterfactory 是所有其他 TypeAdapter 所在的地方。

因此,调用

Gson gson = new Gson();

不提供反序列化对象其余部分所需的类型适配器。

希望对你有帮助。

关于java - 尝试实现自己的反序列化器时出现 Gson + AutoValue 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39642774/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com