2

I've the following response from server:

“data”: {
    “key1”: 11
    “key2”: 89
    “key3”: {
        “key7”: 1
        “key8”: -1
        “key11”: 1
    },
    “key5”: “test”
}

I've made a parcelable model:

class Model implements Parcelable {
    private int key1;
    private int key2;
    private Bundle key3;
    private int key5;

    [...]

    protected Model(Parcel in) {
        key1 = in.readInt();
        [...]
        key3 = in.readBundle();
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(key1);
        [...]
        dest.writeBundle(key3);
    }
}

I'm initializing GSON this way:

private static final Gson GSON = new GsonBuilder()
        .registerTypeAdapterFactory(new ItemTypeAdapterFactory())
        .create();

My ItemTypeAdapterFactory:

class ItemTypeAdapterFactory implements TypeAdapterFactory {

    public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) {

        final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
        final TypeAdapter<JsonElement> elementAdapter = gson.getAdapter(JsonElement.class);

        return new TypeAdapter<T>() {

            public void write(JsonWriter out, T value) throws IOException {
                delegate.write(out, value);
            }

            public T read(JsonReader in) throws IOException {

                JsonElement jsonElement = elementAdapter.read(in);
                if (jsonElement.isJsonObject()) {
                    JsonObject jsonObject = jsonElement.getAsJsonObject();

                    // JSON key "data"
                    if (jsonObject.has("data")) {

                        JsonElement jsonData = jsonObject.get("data");

                        // JSON primitive
                        if (jsonData.isJsonPrimitive()) {
                            jsonElement = jsonData.getAsJsonPrimitive();
                        }

                        // JSON object
                        else if (jsonData.isJsonObject()) {
                            jsonElement = jsonData;
                        }

                        // JSON object array
                        else if (jsonData.isJsonArray()) {
                            jsonElement = jsonData.getAsJsonArray();
                        }
                    }
                }

                return delegate.fromJsonTree(jsonElement);
            }
        }.nullSafe();
    }
}

The resulting bundle for key3 is always null, why?

Jumpa
  • 4,319
  • 11
  • 52
  • 100
  • Make a key3 class. key3 should be an object of a type which contains keys 7,8,11 – Rick Sanchez Dec 17 '15 at 16:01
  • I'd like to avoid making a model class for something that is basically an HashMap or Bundle. Isn't that automatic in Gson? – Jumpa Dec 17 '15 at 16:03
  • Did you tried typing with java.util.Map instead of Bundle? Thus, Gson should marshall it automatically – Gervasio Amy Dec 17 '15 at 16:29
  • I've tried Map and HashMap with no luck... – Jumpa Dec 17 '15 at 17:03
  • Gson supports parsing JSON objects into `Map` data structures. If you've had "no luck" with that please include the error message you're running into. As mentioned making `key3` a `Map` works out of the box, so if it's failing for you it's likely an issue with your deserializer. Try parsing your JSON first with a standard Gson instance, then adding back custom deserialization tasks step by step. – dimo414 Apr 04 '17 at 16:36

0 Answers0