1

Realm が s を含むプロモーション型を操作できないため、この質問のようなものStringを実装しようとしています。JsonDeserializer

問題は、次のエラーが発生する理由に困惑していることです。

W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: BEGIN_OBJECT が必要でしたが、STRING でした

これは Json の一部です。"tags": ["GLUTEN FREE", "NUT FREE"],

私の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 の一部:

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

JsonDeserializationContext(関数の 2 番目のパラメーターとして渡される) を使用して、正しくdeserialize逆シリアル化できます。RealmStringcontext、現在の Gson インスタンスに登録されているすべてのカスタム型を逆シリアル化する方法を知っています。https://google.github.io/gson/apidocs/com/google/gson/JsonDeserializationContext.htmlのドキュメントを参照してJsonDeserializationContextください。

現在のコードで動作しない理由は、 のカスタム デシリアライザーを認識しない新しいGson インスタンスを作成しているためです。FoodDeserializerRealmString

于 2016-06-10T19:45:39.463 に答える
0

私の気持ちは正しかったことがわかりました。StringRealmListConverter次のように、FoodDeserializableコンストラクターに追加する必要がありました。

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