问题描述
由于Realm无法使用促销类型(包括String
),因此我试图实现JsonDeserializer
,就像.
Due to Realm inability to work with promotive types, which include String
s, I'm trying to implement a JsonDeserializer
just like in this question.
问题在于,我对为什么出现以下错误感到困惑:
The issue is that I'm baffled on to why I'm getting the following error:
这是Json的一部分:"tags": ["GLUTEN FREE", "NUT FREE"],
This is part of the Json: "tags": ["GLUTEN FREE", "NUT FREE"],
我的RealmString:
My RealmString:
public class RealmString extends RealmObject {
private String value;
public RealmString() {
}
public RealmString(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
改装的Pojo的一部分:
Part of the retrofited Pojo:
public class Entity extends RealmObject {
@SerializedName("tags")
private RealmList<RealmString> tags = null;
}
..和反序列化器:
public class StringRealmListConverter implements JsonDeserializer<RealmList<RealmString>> {
@Override
public RealmList<RealmString> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
RealmList<RealmString> realmStrings = new RealmList<>();
JsonArray ja = json.getAsJsonArray();
for (JsonElement je : ja) {
realmStrings.add((RealmString) context.deserialize(je, RealmString.class));
}
return realmStrings;
}
}
我正在这里注册:
public Gson provideGson() {
return new GsonBuilder()
.registerTypeAdapter(Food.class, new FoodDeserializer())
.registerTypeAdapter(new TypeToken<RealmList<RealmString>>() {}.getType(),
new StringRealmListConverter())
.create();
}
edit 这是FoodDeserializer.之所以陷入混乱,是因为我们不得不使用继承而不是继承"来取悦领域之神:
edit Here's the FoodDeserializer. It's in this mess because we had to use Composition over Inheritance in order to please the Realm gods:
public class FoodDeserializer implements JsonDeserializer<Food> {
public static final String TAG = FoodDeserializer.class.getSimpleName();
Gson mHelperGson;
public FoodDeserializer() {
mHelperGson = new GsonBuilder().create();
}
@Override
public Food deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String type = json.getAsJsonObject().get("responseType").getAsString();
switch (type) {
case "Platter":
return parsePlatter(json);
case "FoodItem":
return parseFoodItem(json);
default:
return null;
}
}
private PlatterEntity parsePlatter(JsonElement json) {
FoodEntity food = mHelperGson.fromJson(json, new TypeToken<FoodEntity>() {
}.getType());
ArrayList<FoodItemEntity> items = new ArrayList<>();
JsonElement je1 = json.getAsJsonObject().get("items");
if (je1 != null) {
JsonArray list = je1.getAsJsonArray();
if (list != null) {
items = mHelperGson.fromJson(list.toString(), new TypeToken<List<FoodItemEntity>>() {
}.getType());
}
}
return new PlatterEntity(food, items);
}
private FoodItemEntity parseFoodItem(JsonElement json) {
FoodEntity food = mHelperGson.fromJson(json, new TypeToken<FoodEntity>() {
}.getType());
Boolean readyToEat = null;
JsonElement je1 = json.getAsJsonObject().get("readyToEat");
if (je1 != null) {
readyToEat = je1.getAsBoolean();
}
String heatingInstructions = null;
JsonElement je2 = json.getAsJsonObject().get("heatingInstructions");
if (je2 != null) {
heatingInstructions = je2.getAsString();
}
ArrayList<IngredientEntity> ingredients = new ArrayList<>();
JsonElement je3 = json.getAsJsonObject().get("ingredients");
if (je3 != null) {
JsonArray list = je3.getAsJsonArray();
if (list != null) {
ingredients = mHelperGson.fromJson(list.toString(), new TypeToken<List<IngredientEntity>>() {
}.getType());
}
}
NutritionEntity foodNutritions = mHelperGson.fromJson(json, new TypeToken<NutritionEntity>() {
}.getType());
return new FoodItemEntity(food, readyToEat, heatingInstructions, ingredients, foodNutritions);
}
}
我想在TypeAdapter
上使用JsonDeserializer
,但是任何帮助将不胜感激,谢谢!
I would like to use a JsonDeserializer
over TypeAdapter
, but any help will be appreciated, thanks!
推荐答案
原来我的感觉是对的.我必须将StringRealmListConverter
添加到FoodDeserializable
构造函数中,如下所示:
Turns out my feelings were right. I had to add the StringRealmListConverter
to the FoodDeserializable
constructor, like this:
public FoodDeserializer() {
mHelperGson = new GsonBuilder()
.registerTypeAdapter(new TypeToken<RealmList<RealmString>>() {
}.getType(),
new StringRealmListConverter())
.create();
}
这篇关于反序列化RealList< RealmString>时出错.进入列表< String>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!