1

由于 Realm 无法使用包括Strings 在内的促销类型,我正在尝试实现一个JsonDeserializer就像在这个问题中一样。

问题是我对为什么会收到以下错误感到困惑:

W / System.err:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但为STRING

这是 Json 的一部分:"tags": ["GLUTEN FREE", "NUT FREE"],

我的领域字符串:

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 的一部分:

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

编辑这是 FoodDeserializer。之所以如此混乱,是因为我们必须使用组合而不是继承来取悦领域之神:

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

我想使用JsonDeserializerover TypeAdapter,但任何帮助将不胜感激,谢谢!

4

2 回答 2

0

事实证明我的感觉是对的。我必须添加StringRealmListConverterFoodDeserializable构造函数中,如下所示:

    public FoodDeserializer() {
        mHelperGson = new GsonBuilder()
            .registerTypeAdapter(new TypeToken<RealmList<RealmString>>() {
                    }.getType(),
                    new StringRealmListConverter())
            .create();
    }
于 2016-06-09T16:22:48.713 回答
0

您可以使用JsonDeserializationContext(作为第二个参数传递给您的deserialize函数)RealmString正确反序列化。context知道如何反序列化在当前 Gson 实例中注册的所有自定义类型。请参阅JsonDeserializationContext此处的文档:https ://google.github.io/gson/apidocs/com/google/gson/JsonDeserializationContext.html 。

它不适用于您当前代码的原因是因为您正在创建一个的Gson 实例,FoodDeserializer其中不知道RealmString.

于 2016-06-10T19:45:39.463 回答