AndyN
AndyN

Reputation: 1754

Invalid value for field exception while deserializing object using Gson

I have a List of Non-Generic type which i want to store in SharedPreferences. For this, I am using Gson to convert the List to String and then storing it to SharedPreferences like this :

Gson gson = new Gson();
Type fooType = new TypeToken<ArrayList<UserFeedMaster>>() {}.getType();
// feedTempList is the ArrayList of type UserFeedMaster class
String gsonFeeds = gson.toJson(feedTempList,fooType);
prefs.edit().putString("recent_feedlist", gsonFeeds).commit();

And later on I am trying to get back the values as ArrayList<UserFeedMaster> in this way :

String gsonFeed = prefs.getString("recent_feedlist", null);
Gson gson = new Gson();
Type type = new TypeToken<ArrayList<UserFeedMaster>>(){}.getType();
List<UserFeedMaster> historyFeeds = gson.fromJson(gsonFeed,type);

For Serializing the data, it is working fine. But the problem here is when I am De-serializing it, I get error java.lang.IllegalArgumentException: invalid value for field at line List<UserFeedMaster> historyFeeds = gson.fromJson(gsonFeed,type);

Error Log :

java.lang.IllegalArgumentException: invalid value for field
at java.lang.reflect.Field.setField(Native Method)
at java.lang.reflect.Field.set(Field.java:588)
at com.google.api.client.util.FieldInfo.setFieldValue(FieldInfo.java:245)
at com.google.api.client.util.FieldInfo.setValue(FieldInfo.java:206)
at com.google.api.client.util.GenericData.put(GenericData.java:103)
at com.google.api.client.util.GenericData.put(GenericData.java:47)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:189)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:146)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
at com.google.gson.Gson.fromJson(Gson.java:755)
at com.google.gson.Gson.fromJson(Gson.java:721)
at com.google.gson.Gson.fromJson(Gson.java:670)
at com.myapp.sample.RecentFeedsFragment.onActivityCreated(RecentFeedsFragment.java:178)`

UserFeedMaster.java

 public class UserFeedMaster {

    @Id
    private String feedBlobKey;
    private Date feedDateTime;
    private int feedLikes;
    private boolean feedIsPrivate;
    private boolean userIsAnonymous;
    private String feedTags;
    private boolean isFeedDeleted;
    private double latitude;
    private double longitude;

    public double getLatitude() {
        return latitude;
    }

    public void setLatitude(double latitude) {
        this.latitude = latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    public void setLongitude(double longitude) {
        this.longitude = longitude;
    }

    public String getFeedBlobKey() {
        return feedBlobKey;
    }

    public void setFeedBlobKey(String feedBlobKey) {
        this.feedBlobKey = feedBlobKey;
    }

    public Date getFeedDateTime() {
        return feedDateTime;
    }

    public void setFeedDateTime(Date feedDateTime) {
        this.feedDateTime = feedDateTime;
    }


    public int getFeedLikes() {
        return feedLikes;
    }

    public void setFeedLikes(int feedLikes) {
        this.feedLikes = feedLikes;
    }

    public boolean isFeedIsPrivate() {
        return feedIsPrivate;
    }

    public void setFeedIsPrivate(boolean feedIsPrivate) {
        this.feedIsPrivate = feedIsPrivate;
    }

    public boolean isUserIsAnonymous() {
        return userIsAnonymous;
    }

    public void setUserIsAnonymous(boolean userIsAnonymous) {
        this.userIsAnonymous = userIsAnonymous;
    }

    public String getFeedTags() {
        return feedTags;
    }

    public void setFeedTags(String feedTags) {
        this.feedTags = feedTags;
    }

    public boolean isFeedDeleted() {
        return isFeedDeleted;
    }

    public void setFeedDeleted(boolean isFeedDeleted) {
        this.isFeedDeleted = isFeedDeleted;
    }

}

JSON response :

[
    {
        "feedBlobKey": "AMIfv944GCtKglU0zOhVTq6F0dG9Aj1LxtIN5Qz0d3CuaRWO3MWIXd_1eCBxVJA_T6FNjx83hq-ORmnAoivTz2IxL120iQYtePBUPoTru6sxKj5iLZmkRxqaIodwEgknUQPvrkEG_37rlUIoycRHUwnPJlmc_6lmtN32tw9-b5NW60wP7u5AHZY",
        "feedDateTime": {
            "value": 1396953609433,
            "tzShift": 0,
            "dateOnly": false
        },
        "feedDeleted": false,
        "feedIsPrivate": false,
        "feedLikes": 0,
        "kind": "userfeedmasterendpoint#resourcesItem"
    },
    {
        "feedBlobKey": "AMIfv944GCtKglU0zOhVTq6F0dG9Aj1LxtIN5Qz0d3CuaRWO3MWIXd_1eCBxVJA_T6FNjx83hq-ORmnAoivTz2IxL120iQYtePBUPoTru6sxKj5iLZmkRxqaIodwEgknUQPvrkEG_37rlUIoycRHUwnPJlmc_6lmtN32tw9-b5NW60wP7u5AHZY",
        "feedDateTime": {
            "value": 1396953609433,
            "tzShift": 0,
            "dateOnly": false
        },
        "feedDeleted": false,
        "feedIsPrivate": false,
        "feedLikes": 0,
        "kind": "userfeedmasterendpoint#resourcesItem"
    }
]

I don't know what is going wrong. Please help me on why I am getting this error and how can I resolve it. Thanks in advance.

Upvotes: 3

Views: 4319

Answers (1)

Brian Roach
Brian Roach

Reputation: 76918

Your JSON has this:

"feedDateTime": {
    "value": 1396953609433,
    "tzShift": 0,
    "dateOnly": false
}

Your Java class has this:

private Date feedDateTime;

Which one of these is not like the other? That is what is producing the error.

GSON isn't going to magically convert that JSON into a Date object. You either need to create a Java class (FeedDateTime) that matches that JSON object or write a custom deserializer for your UserFeedMaster class.

Upvotes: 2

Related Questions